Simple Keyboard + pushbutton question

Status
Not open for further replies.
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?
 
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
 
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?
 
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 :/
 
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
 
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.
 
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!
 
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?
 
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
 
IMG_2769.jpg

Thanks again for your help. I really appreciate it.
 
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
 
Status
Not open for further replies.
Back
Top