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

Thread: Simple Keyboard + pushbutton question

  1. #1
    Junior Member
    Join Date
    May 2020
    Posts
    8

    Simple Keyboard + pushbutton question

    Hi all,

    I'm new to thisóbear with me! I've spent the last hour puzzling over two very small programs which each work on their own, but don't work when combined.

    Program 1 is a proof-of-concept that my pushbutton works. It behaves as expected: I push the button, the light turns off:
    Code:
    void setup() {
      pinMode(13, OUTPUT);
    }
    
    void loop() {
    
      if (digitalRead(4) == LOW) {  //Pushbutton on pin 4
        analogWrite(13, 200); 
      } else {
        analogWrite(13, 0);  
      }
      
      delay(5);  //Slow things down a bit
    }
    Program 2 is a proof-of-concept that the keyboard works. It works as expected: every 6 seconds, it prints "hello":
    Code:
    void setup() {}
    
    void loop() {
      Keyboard.print("hello");
      delay(6000);
    }
    But when I combine the two, and put my keyboard print line in Program 1, the LED portion still works but it never prints:
    Code:
    void setup() {
      pinMode(13, OUTPUT);
    }
    
    void loop() {
    
      if (digitalRead(4) == LOW) {  //Pushbutton on pin 4
        analogWrite(13, 200); 
      } else {
        analogWrite(13, 0);
        Keyboard.print("hello");
      }
      
      delay(5);  //Slow things down a bit
    }
    Clearly, I'm missing something. What gives?

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,368
    Try adding this to setup()
    Code:
      pinMode(4,INPUT_PULLUP);
    and be prepared for a lot of "hello". You would be better off sending "hello" only when the pin changes state from HIGH to LOW, rather than continuously when the pin is LOW.



    Pete

  3. #3
    Junior Member
    Join Date
    May 2020
    Posts
    8
    Hi Pete, thanks so much for the quick reply! Unfortunately, it didn't make any difference Here's what I've got:

    Code:
    void setup() {
      pinMode(13, OUTPUT);
      pinMode(4, INPUT_PULLUP);
    }
    
    void loop() {
    
      if (digitalRead(4) == LOW) {  //Pushbutton on pin 4
        analogWrite(13, 200); 
      } else {
        analogWrite(13, 0);
        Keyboard.print("hello");
      }
      
      delay(5);  //Slow things down a bit
    }
    LED is still responsive (on until I press, off when I press). No keyboard output though! If I reload my old keyboard program, that works fine. Any other thoughts?

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,368
    Your code works for me on a Teensy 3.2 with Arduino 1.8.12 and Teensyduino 1.52

    Pete

  5. #5
    Junior Member
    Join Date
    May 2020
    Posts
    8
    Well that's even more frustrating! I'm on a Teensy 3.2, Arduino 1.8.12, Teensyduino 1.52. So same configuration. MacOS. Seems like it wouldn't be a driver thing because my keyboard tester program works. So is this a dead-end? Not sure where to go from here :/

  6. #6
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,368
    I'm using a PC and haven't used a MAC for nearly 40 years so I can't help with that. I can't think of anything that would cause this.
    One shot in the dark is to try using pin 13 as digital rather than analog - but I doubt it'll make any difference.
    Code:
    void setup() {
      pinMode(13, OUTPUT);
      pinMode(4, INPUT_PULLUP);
    }
    
    void loop() {
    
      if (digitalRead(4) == LOW) {  //Pushbutton on pin 4
        digitalWrite(13, 1); 
      } else {
        digitalWrite(13, 0);
        Keyboard.print("hello");
      }
      
      delay(5);  //Slow things down a bit
    }
    Pete

  7. #7
    Junior Member
    Join Date
    May 2020
    Posts
    8
    Unfortunately not At this point I'm thinking of returning my Teensy—normally I wouldn't jump to something like that so quick, but if you're telling me your program works with the same config, it seems like something banging my head against won't fix.

  8. #8
    Junior Member
    Join Date
    May 2020
    Posts
    8
    Actually, here's a somewhat interesting update:

    void setup() {
    pinMode(13, OUTPUT);
    pinMode(4, INPUT_PULLUP);
    }

    void loop() {

    if (digitalRead(4) == LOW) { //Pushbutton on pin 4
    digitalWrite(13, 1);
    Keyboard.print("test 1");
    } else {
    digitalWrite(13, 0);
    Keyboard.print("test 2");
    }

    Keyboard.print("test 3");
    delay(5000); //Slow things down a bit
    }


    This will print "test 1test3" on loop, until I press the button. When I press & hold the button, the LED goes off, but neither test2 nor test3 are printed. It seems like holding the pushbutton suspends the program? Is that possible? Can you think of a wiring mistake that might cause that? Thank you so much for your help!

  9. #9
    Junior Member
    Join Date
    May 2020
    Posts
    8
    It's almost like the digitalWrite(13,0); line breaks out of the loop, or skips the remainder of it. I tried this:
    Code:
    int counter = 0;
    int buttonState = 0;
    
    void setup() {
      pinMode(13, OUTPUT);
      pinMode(4, INPUT_PULLUP);
    }
    
    void loop() {
    
      if (digitalRead(4) == LOW) {  //Pushbutton on pin 4
        digitalWrite(13, 200);
      } else {
        digitalWrite(13, 0);
        counter++;
      }
    
      Keyboard.print(counter);
      delay(4000);  //Slow things down a bit
    }
    If I don't press my button, ever 4 seconds it prints "0". If I press (and hold) the button, it stops printing until I release it. No matter what I do, I can't get it to print anything above 0. It's like it just freezes when the pushbutton is depressed. Commending out digitalWrite(13,0) doesn't do anything, so this is definitely about the input, not about the LED.

    Any thoughts?

  10. #10
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,368
    That last sketch works for me.
    Take a photo of the wiring and the board and post it here.

    BTW Your original two sketches worked, which shows that there's nothing wrong with the T3.2 so returning it isn't going to help.

    Pete

  11. #11
    Junior Member
    Join Date
    May 2020
    Posts
    8
    Click image for larger version. 

Name:	IMG_2769.jpg 
Views:	5 
Size:	104.1 KB 
ID:	20304

    Thanks again for your help. I really appreciate it.

  12. #12
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,368
    OK. I think your wiring is wrong. You have pin 4 wired directly to ground and one side of the switch. The other side of the switch is apparently wired to pin 6 and 3V3. I think what that will do is short the 3V3 regulator to ground when you push the switch.
    Pin 4 should go to one side of the switch and the other side of the switch goes to ground. No other connections. To do that you will have to put the switch to the right of the Teensy and use longer wires.

    Pete

  13. #13
    Junior Member
    Join Date
    May 2020
    Posts
    8
    That did it. Thank you so much!!! You're a lifesaver.

  14. #14
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,368
    You're welcome. Glad it's sorted

    Pete

Posting Permissions

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