Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: If statement help

  1. #1
    Junior Member
    Join Date
    Mar 2017
    Posts
    2

    If statement help

    I have been trying to make this diy h-shifter project

    i used the code from the post but when I'm not activating any switches it types "0123456701234567" and on and on. Even when all switches are pressed, it still types it. I've tried changing
    Code:
    if (digitalRead(gearR) == 0) // gear engaged (the '0' means the pin has been shorted to the ground, causing a drop in voltage on that pin)
    {
    Keyboard.press('0'); // send a keypress (and hold) '0' to the computer via Keyboard HID
    
    }
    
    if (digitalRead(gearR) == 1) //gear disengaged (the '1' means the pin is no longer shorted to the ground, which increases voltage on that pin)
    {
    Keyboard.release('0'); //tells the PC to let go of the key (otherwise, it would continue to hold it forever, even after another gear is selected)
    }
    to
    Code:
    if (digitalRead(gearR) == 1) // gear engaged (the '0' means the pin has been shorted to the ground, causing a drop in voltage on that pin)
    {
    Keyboard.press('0'); // send a keypress (and hold) '0' to the computer via Keyboard HID
    
    }
    
    if (digitalRead(gearR) == 0) //gear disengaged (the '1' means the pin is no longer shorted to the ground, which increases voltage on that pin)
    {
    Keyboard.release('0'); //tells the PC to let go of the key (otherwise, it would continue to hold it forever, even after another gear is selected)
    }
    , I've tried changing the second "if"s to "else"s but that gives me an error message.
    I checked the wiring and it is all correctly wired with the microswitches. could it be a problem with the teensy 2.0's pins?

    Code:
    //Pin assignments for each gear
    
    int gearR = 0; // Reverse gear set to pin 0
    int gear1 = 1;
    int gear2 = 2;
    int gear3 = 3;
    int gear4 = 4;
    int gear5 = 5;
    int gear6 = 6;
    int gear7 = 7;
    
    void setup()
    {
    
    //Gear Inputs
    pinMode(gearR, INPUT); // Set the gear as an input
    digitalWrite(gearR, HIGH); 
    
    pinMode(gear1, INPUT);
    digitalWrite(gear1, HIGH);
    
    pinMode(gear2, INPUT);
    digitalWrite(gear2, HIGH);
    
    pinMode(gear3, INPUT);
    digitalWrite(gear3, HIGH);
    
    pinMode(gear4, INPUT);
    digitalWrite(gear4, HIGH);
    
    pinMode(gear5, INPUT);
    digitalWrite(gear5, HIGH);
    
    pinMode(gear6, INPUT);
    digitalWrite(gear6, HIGH);
    
    pinMode(gear7, INPUT);
    digitalWrite(gear7, HIGH);
    }
    
    
    // Below is where the functions for each keypress and release are assigned.
    // If you currently have any of the number keys 0-7 mapped to a function, you can change them out below.
    // Be sure to change the key for both the "Press" function, and the "Release" function for each gear.
    // Note, these are the number keys above the letter keys on a keyboard, NOT the number pad keys.
    
    void loop()
    {
    //////////*** Reverse GEAR ***\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    
    if (digitalRead(gearR) == 0) // gear engaged (the '0' means the pin has been shorted to the ground, causing a drop in voltage on that pin)
    {
    Keyboard.press('0'); // send a keypress (and hold) '0' to the computer via Keyboard HID
    
    }
    
    if (digitalRead(gearR) == 1) //gear disengaged (the '1' means the pin is no longer shorted to the ground, which increases voltage on that pin)
    {
    Keyboard.release('0'); //tells the PC to let go of the key (otherwise, it would continue to hold it forever, even after another gear is selected)
    }
    //////////*** 1ST GEAR ***\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 
    if (digitalRead(gear1) == 0)
    {
    Keyboard.press('1');
    
    }
    
    if (digitalRead(gear1) == 1) //gear disengaged
    {
    Keyboard.release('1');
    }
    
    //////////*** 2ND GEAR ***\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    if (digitalRead(gear2) == 0)
    {
    Keyboard.press('2');
    
    }
    
    if (digitalRead(gear2) == 1) //gear disengaged
    {
    Keyboard.release('2');
    }
    
    //////////*** 3RD GEAR ***\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    if (digitalRead(gear3) == 0)
    {
    Keyboard.press('3');
    
    }
    
    if (digitalRead(gear3) == 1) //gear disengaged
    {
    Keyboard.release('3');
    }
    
    //////////*** 4TH GEAR ***\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    if (digitalRead(gear4) == 0)
    {
    Keyboard.press('4');
    
    }
    
    if (digitalRead(gear4) == 1) //gear disengaged
    {
    Keyboard.release('4');
    }
    
    //////////*** 5TH GEAR ***\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    if (digitalRead(gear5) == 0)
    {
    Keyboard.press('5');
    
    }
    
    if (digitalRead(gear5) == 1) //gear disengaged
    {
    Keyboard.release('5');
    }
    
    //////////*** 6TH GEAR ***\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    if (digitalRead(gear6) == 0)
    {
    Keyboard.press('6');
    
    }
    
    if (digitalRead(gear6) == 1) //gear disengaged
    {
    Keyboard.release('6');
    }
    
    //////////*** 7TH GEAR ***\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    if (digitalRead(gear7) == 0)
    {
    Keyboard.press('7');
    
    }
    
    if (digitalRead(gear7) == 1) //gear disengaged
    {
    Keyboard.release('7');
    }
    
    
    }

  2. #2
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,935
    This is code intended for a Uno isn't it?

    Try deleting those
    digitalWrite(gear1, HIGH);

    lines and changing each pinmode line

    pinMode(gear1, INPUT);
    to
    pinMode(gear1, INPUT_PULLUP);

    Edit: the code was using an old way of enabling pullups to make the switches work which works on a Uno due to some oddities in the hardware but the latter chips get things specifically set:
    https://www.arduino.cc/en/Reference/pinMode
    Last edited by GremlinWrangler; 03-11-2017 at 10:53 PM.

  3. #3
    Junior Member
    Join Date
    Mar 2017
    Posts
    2
    if the code is intended for an uno, then i guess I am using the wrong board. (edit:spelling)

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,458
    Use the Bounce library.

    Look in File > Examples > Teensy > USB_Keyboard > Buttons.

  5. #5
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,935
    Re Uno code it should work, was just using a very old way to get pullups enabled. And the next problem you will hit when you do would be switch bounce, hence Paul's suggestion to go straight to the bounce library.

    Something else to do as well would be to look at examples->teensy->tutorial1->blink and see how your drive the onboard LED. While getting this working might be worth adding two digital writes to the reverse IF statements to turn on and off the LED to make it a bit clearer what your switches are up to.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •