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

Thread: Problem with digitalRead for simple buttons

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    4

    Problem with digitalRead for simple buttons

    Hello!

    I try to make a controller for a CNC machine, for that i need to emulate Numpad keys, i wired up a simple project box, used some knobs, switches etc. and soldered them together. all components are wired to the same ground together, and each other terminal goes to a different pin, from PIN1 - Pin 10, so total of 10 keys i like to use.

    This is how my code looks like:

    Code:
    #include <Keyboard.h>
    void setup() {
      pinMode(1, INPUT);
      pinMode(2, INPUT);
      pinMode(3, INPUT);
      pinMode(4, INPUT);
      pinMode(5, INPUT);
      pinMode(6, INPUT); 
      pinMode(7, INPUT);
      pinMode(8, INPUT);
      pinMode(9, INPUT);
      pinMode(10, INPUT); 
      Keyboard.begin();
    }
    void loop() {
      if (digitalRead(0) == HIGH) {Keyboard.press(KEYPAD_2); Keyboard.release(KEYPAD_2);}
      if (digitalRead(1) == HIGH) {Keyboard.press(KEYPAD_1); Keyboard.release(KEYPAD_1);}
      if (digitalRead(2) == HIGH) {Keyboard.press(KEYPAD_3); Keyboard.release(KEYPAD_3);}
      if (digitalRead(3) == HIGH) {Keyboard.press(KEYPAD_7);  Keyboard.release(KEYPAD_7);}
      if (digitalRead(4) == HIGH) {Keyboard.press(KEYPAD_9);  Keyboard.release(KEYPAD_9);}
      if (digitalRead(5) == HIGH) {Keyboard.press(KEY_PAGE_UP);  Keyboard.release(KEY_PAGE_UP);}
      if (digitalRead(6) == HIGH) {Keyboard.press(KEY_PAGE_DOWN);  Keyboard.release(KEY_PAGE_DOWN);}
      if (digitalRead(7) == HIGH) {Keyboard.press(KEY_3); Keyboard.release(KEY_3);}
      if (digitalRead(8) == HIGH) {Keyboard.press(KEY_4); Keyboard.release(KEY_4);}
      if (digitalRead(9) == HIGH) {Keyboard.press(KEY_5); Keyboard.release(KEY_5);}
     
    }

    now the issue is that i get a irradical behavior, like the buttons keep pushing. I tried to change high to low too, same result. It should be Low for shortcircute between ground and pin?
    I have removed all the pins once, and still it happens with both read states....

    did i fry my board? it worked when i last did it for a flight controller, and now it doesn't work anymore.

    Thanks for any suggestions :/

  2. #2
    Junior Member
    Join Date
    Mar 2019
    Posts
    4
    oh i see, INPUT_PULLUP is better as it uses the build in pullup resistors.... and than using fallingEdge() instead of digitalRead....

  3. #3
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,385
    And still then, there is a risk of multiple events with one button press, due to mechanical contact bounce/chatter. You need a de-bounce algorithm for each button. The Bounce library could be of great help.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,932
    Highly recommend you start from the Buttons example.

    In Arduino, click File > Examples > Teensy > USB_Keyboard > Buttons

  5. #5
    Junior Member
    Join Date
    Mar 2019
    Posts
    4
    It worked, alltho the relais do not work, it seams that he don't trigger the pins.... but i'll have a look tomorrow when my new multimeter arrives...

    thanks, here is the code, for anyone who looks this up later by searching:

    Code:
    #include <Bounce.h>
    
    Bounce button0 = Bounce(0, 20);
    Bounce button1 = Bounce(1, 20);
    Bounce button2 = Bounce(2, 20);
    Bounce button3 = Bounce(3, 20);
    Bounce button4 = Bounce(4, 20);
    Bounce button5 = Bounce(5, 20); 
    Bounce button6 = Bounce(6, 20);
    Bounce button7 = Bounce(7, 20); 
    Bounce button8 = Bounce(8, 20);
    Bounce button9 = Bounce(9, 20);
    Bounce button10 = Bounce(10, 20);
    Bounce button13 = Bounce(13, 100);
    Bounce button14 = Bounce(14, 100);
    
    
    void setup() {
    
      pinMode(0, INPUT_PULLUP);
      pinMode(1, INPUT_PULLUP);
      pinMode(2, INPUT_PULLUP);
      pinMode(3, INPUT_PULLUP);
      pinMode(4, INPUT_PULLUP);
      pinMode(5, INPUT_PULLUP);
      pinMode(6, INPUT_PULLUP);
      pinMode(7, INPUT_PULLUP);
      pinMode(8, INPUT_PULLUP);
      pinMode(9, INPUT_PULLUP);
      pinMode(10, INPUT_PULLUP);
      pinMode(13, OUTPUT);
      pinMode(14, OUTPUT);
    }
    
    void loop() {
      button0.update();
      button1.update();
      button2.update();
      button3.update();
      button4.update();
      button5.update();
      button6.update();
      button7.update();
      button8.update();
      button9.update();
      button10.update();
    
      if (button1.fallingEdge()) {
        Keyboard.press(KEY_2);
      }
      if (button2.fallingEdge()) {
        Keyboard.press(KEYPAD_4);
      }
      if (button3.fallingEdge()) {
        Keyboard.press(KEYPAD_6);
      }
      if (button4.fallingEdge()) {
        Keyboard.press(KEYPAD_2);
      }
      if (button5.fallingEdge()) {
        Keyboard.press(KEYPAD_8);
      }
    
    
      if (button7.fallingEdge()) {
        Keyboard.press(KEY_PAGE_DOWN);
      }
      if (button8.fallingEdge()) {
        Keyboard.press(KEY_PAGE_UP);
      }
      if (button9.fallingEdge()) {
        Keyboard.press(KEY_4);
      }
        if (button10.fallingEdge()) {
        Keyboard.press(KEY_3);
      }
      
      if (button13.fallingEdge()) {        //spindle active
      digitalWrite(13, HIGH);
      }
      if (button14.fallingEdge()) {
      digitalWrite(14, HIGH);                       //LED ON
      }
    
      // ---------------------------------------
    
      if (button1.risingEdge()) {
        Keyboard.release(KEY_2);
      }
      if (button2.risingEdge()) {
        Keyboard.release(KEYPAD_4);
      }
      if (button3.risingEdge()) {
        Keyboard.release(KEYPAD_6);
      }
      if (button4.risingEdge()) {
        Keyboard.release(KEYPAD_2);
      }
      if (button5.risingEdge()) {
        Keyboard.release(KEYPAD_8);
      }
      if (button7.risingEdge()) {
        Keyboard.release(KEY_PAGE_DOWN);
      }
      if (button8.risingEdge()) {
        Keyboard.release(KEY_PAGE_UP);
      }
      if (button9.risingEdge()) {
        Keyboard.release(KEY_4);
      }
      if (button10.risingEdge()) {
        Keyboard.release(KEY_3);
      }
    
      if (button13.risingEdge()) {        //spindle inactive
      digitalWrite(13, LOW);
      }
      if (button14.risingEdge()) {
      digitalWrite(14, LOW);                      //LED ON
      }
    
    }

  6. #6
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,069
    Code:
    Bounce button13 = Bounce(13, 100);
    Bounce button14 = Bounce(14, 100);
    .
    .
    .
      pinMode(13, OUTPUT);
      pinMode(14, OUTPUT);
    You've declared pins 13 and 14 to be outputs so they can't be buttons as well.

    Pete

  7. #7
    Junior Member
    Join Date
    Mar 2019
    Posts
    4
    Quote Originally Posted by el_supremo View Post
    Code:
    Bounce button13 = Bounce(13, 100);
    Bounce button14 = Bounce(14, 100);
    .
    .
    .
      pinMode(13, OUTPUT);
      pinMode(14, OUTPUT);
    You've declared pins 13 and 14 to be outputs so they can't be buttons as well.

    Pete
    *facepalm*

    thanks, that was it

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    19,932
    The LED on pin 13 sometimes interferes with buttons. The internal pullups are too weak to reliably pull it high, due to the loading of the LED. This can work if you use a real (and rather strong) pullup resistor, with a value like 470 ohms. But best to just avoid pin 13 and use other pins.

Posting Permissions

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