Newbie needs some programming help!

Here is the code I am working with. This code works great, BUT I am wanting to add more than 1 input - each time I do so the computer receives the actions and loops them over and over not waiting on the switch.

What I am trying to accomplish is 4 buttons connect to a Teensy 2.0 board. Each button controls different keyboard action. See code below for my 1 button example.

THIS CODE WORKS GREAT:

void setup() {
  Serial.begin(9600);
  pinMode(21, INPUT_PULLUP); 
  delay(4000);   
}
void loop() {
  if (digitalRead(21) == LOW) 
  {
    Keyboard.set_key1(KEY_ENTER);
    Keyboard.send_now();    
    
    Keyboard.set_key1(0);
    Keyboard.send_now();
    delay(2500);
  }
  
  delay(10);
  
}

IF I ADD A SECOND INPUT IT DOESN'T WORK - THIS IS HOW I AM CODING:

void setup() {
  Serial.begin(9600);
  pinMode(21, INPUT_PULLUP); 
  pinMode(20, INPUT_PULLUP);
  delay(4000);   
}
void loop() {
  if (digitalRead(21) == LOW) 
  {
    Keyboard.set_key1(KEY_ENTER);
    Keyboard.send_now();    
    
    Keyboard.set_key1(0);
    Keyboard.send_now();
    delay(2500);
  }

   if (digitalRead(20) == LOW)
{
   Keyboard.set_key2(KEY_SPACE);
   Keyboard.send_now();

   Keyboard.set_key2(0);
   Keyboard.send_now();
   delay(1000)
}

  
  delay(10);
  
}

What am I doing wrong by adding the second input?

Update: I used the original (1 button) code from a tutorial and it works great. KEY_ENTER doesn't need defined when I press my button it sends an 'ENTER' on the screen/computer. I have played with the delays quite a bit and if I remove them is sends the actions over and over again.

Thanks

  1. Please use # to place the code.
// need a include here for the keyboard
#include <keyboard.h> // <-- I think ...

// missing some declaration here

void setup() {
  Serial.begin(9600);
  pinMode(21, INPUT_PULLUP);  // <-- should be pinMode(21, INPUT) 
  pinMode(20, INPUT_PULLUP);
  delay(4000);   // <--- you need 4 seconds here ?
}
void loop() {
  if (digitalRead(21) == LOW) 
  {
    Keyboard.set_key1(KEY_ENTER);  // <- I don't see any declaration for KEY_ENTER
    Keyboard.send_now();    
    
    Keyboard.set_key1(0);
    Keyboard.send_now();
    delay(2500); // <-- you need 2.5 second here ?
  }

   if (digitalRead(20) == LOW)
{
   Keyboard.set_key2(KEY_SPACE); // <--  Also I don't see any declaration for KEY_SPACE
   Keyboard.send_now();

   Keyboard.set_key2(0);
   Keyboard.send_now();
   delay(1000)  // <-- you need 1 second here ?
}

  
  delay(10); // <-- short time , only 10 mS ..that is 10 milisecond or 0.01 second , you need that ?
  
}

What am I doing wrong

  1. You are not posting your code in a code box.
    Go back to modify that post. Select the code and hit the # icon, then save it.

  2. You are not posting all you code so we can't see why you think the first one works. That to will repeat over and over, maybe something down the line is suppressing the repeat, but without all the code we can't be sure.

  3. You are using the delay function, that means when pin 21 is held low the code won't look at pin 20 for 2.1 seconds. You need to remember the past state of the input in a variable and only do the action when the current state is low and the past state was high and get rid of all those delays.

I was able to get the code to work. Seems as though I had soldered my board wrong! I will re-solder board tomorrow and we will see what happens.

Here is the final code (should work):

// The inputs you're using for button presses
const int ENTER = 21;     // ENTER
const int SPACE = 20;     // SPACE
const int UP = 19;        // UP
const int DOWN = 18;      //DOWN
 
int ENTERStatus = 0;
int SPACEStatus = 0;
int UPStatus = 0;
int DOWNStatus = 0;
 
void setup() {
  pinMode(ENTER, INPUT);
  pinMode(SPACE, INPUT);  
  pinMode(UP, INPUT);
  pinMode(DOWN, INPUT); 
}
 
void loop(){
  // Check the buttons
  ENTERStatus = digitalRead(ENTER);
  SPACEStatus = digitalRead(SPACE);
  UPStatus = digitalRead(UP);
  DOWNStatus = digitalRead(DOWN);
  
 
// If ENTER button is pressed  
  if (ENTERStatus == HIGH) {
     // Change the following two lines to change the keys sent      
    Keyboard.set_key1(KEY_ENTER);
    Keyboard.send_now(); 
    Keyboard.set_key1(0);
    Keyboard.send_now();
    delay(500);  
  } 
 
// If SPACE button is pressed
  if (SPACEStatus == HIGH) { 
     // Change the following two lines to change the keys sent     
    Keyboard.set_key1(KEY_SPACE);
    Keyboard.send_now();  
    Keyboard.set_key1(0);
    Keyboard.send_now();
  delay(500);  
  } 
  
  // If UP button is pressed  
  if (UPStatus == HIGH) {
     // Change the following two lines to change the keys sent      
    Keyboard.set_key1(KEY_UP);
    Keyboard.send_now(); 
    Keyboard.set_key1(0);
    Keyboard.send_now();
    delay(500);  
  } 
 
// If DOWN button is pressed
  if (DOWNStatus == HIGH) { 
     // Change the following two lines to change the keys sent     
    Keyboard.set_key1(KEY_DOWN);
    Keyboard.send_now();  
    Keyboard.set_key1(0);
    Keyboard.send_now();
  delay(500);  
  } 
}

This code will still repeat the action when the button is held down, is that what you want?

If not you have to do the action only when the current status is HIGH and the previous status was LOW.

Grumpy_Mike:
This code will still repeat the action when the button is held down, is that what you want?

If not you have to do the action only when the current status is HIGH and the previous status was LOW.

What would that code look like with the previously LOW statement in it?

Thank you

int previousState;
void setup(){
   previousState = digitalRead(myPin);
}

void loop(){
int currentState = digitalRead(myPin);
if( currentState == HIGH && previousState == LOW) { // only do this stuff once and on a rising edge
  // do stuff
}
previousState = currentState;
}