Trying to program multiple buttons for a keyboard

Hi all I am brand new to arduino coding and I am trying to get a button array setup. I have 4 buttons but I am taking baby steps towards all 4. I am using the Keyboard Emulator and trying to send keystrokes. But I keep getting errors when sending to the board the main one I am getting now is a "expected primary-expression before 'const'" I have included my code as it sits now any help would be greatly appreciated.

#include "Keyboard.h"

//declaring button pins
const int buttonPin = 1;          

int previousButtonState = HIGH; 

void setup() {
  //declare the buttons as input_pullup
  pinMode(buttonPin, INPUT_PULLUP);  
  Keyboard.begin();
}

void loop() {
  //checking the state of the button
  int buttonState = digitalRead(buttonPin);
  
 //replaces button press with UP arrow
  if (buttonState == LOW && previousButtonState == HIGH) {
      // and it's currently pressed:
    Keyboard.press(97);
    delay(50);
  }

  if (buttonState == HIGH && previousButtonState == LOW) {
      // and it's currently released:
    Keyboard.release(97);
    delay(50);
  }
 
  previousButtonState = buttonState;
----------------------------------------------------------------------------

//declaring button pins
const int buttonPin = 2;          

int previousButtonState = HIGH; 

void setup() {
  //declare the buttons as input_pullup
  pinMode(buttonPin, INPUT_PULLUP);  
  Keyboard.begin();
}

void loop() {
  //checking the state of the button
  int buttonState = digitalRead(buttonPin);
  
 //replaces button press with UP arrow
  if (buttonState == LOW && previousButtonState == HIGH) {
      // and it's currently pressed:
    Keyboard.press(98);
    delay(50);
  }

  if (buttonState == HIGH && previousButtonState == LOW) {
      // and it's currently released:
    Keyboard.release(98);
    delay(50);
  }
 
  previousButtonState = buttonState;
}

One obvious problem is that you have 2 loop() and setup() functions in your sketch

Ok I have gotten rid of those and combined my code to look like this but now I am getting a redefinition error of the button pin

#include "Keyboard.h"

//declaring button pins
const int buttonPin = 1;          

int previousButtonState = HIGH;

const int buttonPin = 2;          

int previousButtonState = HIGH;

void setup() {
  //declare the buttons as input_pullup
  pinMode(buttonPin, INPUT_PULLUP);  
  Keyboard.begin();
}

void loop() {
  //checking the state of the button
  int buttonState = digitalRead(buttonPin);
  
 //replaces button press with UP arrow
  if (buttonState == LOW && previousButtonState == HIGH) {
      // and it's currently pressed:
    Keyboard.press(97);
    delay(50);
  }

  if (buttonState == HIGH && previousButtonState == LOW) {
      // and it's currently released:
    Keyboard.release(97);
    delay(50);
  }
const int buttonPin = 1;  
const int buttonPin = 2;  

Not really surprising

Which single value do you want the buttonPin variable to have ? Which pin the the button actually connected to ?

Well I am trying to get multiple buttons working I have a button on both 1 and 2 pins.

Then you either need 2 separate variables with different names or you need to use a single variable but don't use its type specifier when you use it for the second time

To start with I suggest that you use 2 different variables

OK so then what would be that second variable I am very new to this. Would this work as my button Pin 2 variable?

buttonPin2

As long as the second variable has a different name then you will not get the "redefinition" error but there may be other problems

Give it a try and report back. If you still get an error then please post your revised code and the full error message copied using the "Copy error message" button in the IDE

I have it able to compile but when I press the button on Pin 1 it types an A then starts sending the Letter B and wont stop without disconnecting. Button 2 does nothing.

#include "Keyboard.h"

//declaring button pins
const int buttonPin = 1;          

int previousButtonState = HIGH;

const int buttonPin2 = 2;          

int previousButtonState2 = HIGH;

void setup() {
  //declare the buttons as input_pullup
  pinMode(buttonPin, INPUT_PULLUP);  
  Keyboard.begin();
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  
  if (buttonState == LOW && previousButtonState == HIGH) {
    Keyboard.press(97);
    delay(50);
  }

  if (buttonState == HIGH && previousButtonState == LOW) {
    Keyboard.release(97);
    delay(50);
  }

  int buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState == LOW && previousButtonState == HIGH) {
    Keyboard.press(98);
    delay(50);
  }

  if (buttonState == HIGH && previousButtonState == LOW) {
    Keyboard.release(98);
    delay(50);
  }
 
  previousButtonState2 = buttonState;
}
  previousButtonState2 = buttonState;

Are you confusing your previousButtonState variables with one another ?

Yes they were confusing each other but now Button 1 types both A and B and not just A and button 2 still does not work.

#include "Keyboard.h"

//declaring button pins
const int buttonPin = 1;          

int previousButtonState1 = HIGH;

const int buttonPin2 = 2;          

int previousButtonState2 = HIGH;

void setup() {
  //declare the buttons as input_pullup
  pinMode(buttonPin, INPUT_PULLUP);  
  Keyboard.begin();
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  
  if (buttonState == LOW && previousButtonState1 == HIGH) {
    Keyboard.press(97);
    delay(50);
  }

  if (buttonState == HIGH && previousButtonState1 == LOW) {
    Keyboard.release(97);
    delay(50);
  }
  previousButtonState1 = buttonState;

  int buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState == LOW && previousButtonState2 == HIGH) {
    Keyboard.press(98);
    delay(50);
  }

  if (buttonState == HIGH && previousButtonState2 == LOW) {
    Keyboard.release(98);
    delay(50);
  }
 
  previousButtonState2 = buttonState;
}

Do you want one button to type an A and the other to type a B ?

Correct once I have it working I am wanting 4 different buttons that type 4 different letters

Start with a single button and a single set of variables such as its pin number, current state and previous state. Remove all references to the second button from the sketch

As it is you have statements such as

  previousButtonState2 = buttonState;

What has the previous state of the second button got to do with the current state of the first one ?

So that would be something like this,

#include "Keyboard.h"

int buttonPin = 1;          
int previousButtonState = HIGH;


void setup() {
  pinMode(buttonPin, INPUT_PULLUP);  
  Keyboard.begin();
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  
  if (buttonState == LOW && previousButtonState == HIGH) {
    Keyboard.press(97);
    delay(50);
  }

  if (buttonState == HIGH && previousButtonState == LOW) {
    Keyboard.release(97);
    delay(50);
  }
 
  previousButtonState = buttonState;

}

I know that this works as this is where I started originally now I am just trying to building off of it for the other buttons.

If that works then the easy way, but not the best, way to add more buttons is to repeat the code for the second an subsequent buttons but use completely different variables

Better way to do it can come later

Alright I have all 4 buttons working now and a better understanding of how to go forward in adding more. Thanks so much.

#include "Keyboard.h"

int buttonPin1 = 1;          
int previousButtonState1 = HIGH;

int buttonPin2 = 2;          
int previousButtonState2 = HIGH;

int buttonPin3 = 3;          
int previousButtonState3 = HIGH;

int buttonPin4 = 4;          
int previousButtonState4 = HIGH;


void setup() {
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(buttonPin4, INPUT_PULLUP);  
  Keyboard.begin();
}

void loop() {
  int buttonState1 = digitalRead(buttonPin1);
  
  if (buttonState1 == LOW && previousButtonState1 == HIGH) {
    Keyboard.press(97);
    delay(50);
  }

  if (buttonState1 == HIGH && previousButtonState1 == LOW) {
    Keyboard.release(97);
    delay(50);
  }
 
  previousButtonState1 = buttonState1;

  int buttonState2 = digitalRead(buttonPin2);
  
  if (buttonState2 == LOW && previousButtonState2 == HIGH) {
    Keyboard.press(98);
    delay(50);
  }

  if (buttonState2 == HIGH && previousButtonState2 == LOW) {
    Keyboard.release(98);
    delay(50);
  }
 
  previousButtonState2 = buttonState2;

  int buttonState3 = digitalRead(buttonPin3);
  
  if (buttonState3 == LOW && previousButtonState3 == HIGH) {
    Keyboard.press(99);
    delay(50);
  }

  if (buttonState3 == HIGH && previousButtonState3 == LOW) {
    Keyboard.release(99);
    delay(50);
  }
 
  previousButtonState3 = buttonState3;

  int buttonState4 = digitalRead(buttonPin4);
  
  if (buttonState4 == LOW && previousButtonState4 == HIGH) {
    Keyboard.press(100);
    delay(50);
  }

  if (buttonState4 == HIGH && previousButtonState4 == LOW) {
    Keyboard.release(100);
    delay(50);
  }
 
  previousButtonState4 = buttonState4;

}

I am glad to hear that it is working, but needless to say there is a better way of doing it rather than repeating the same code over and over again

Cool, it works. But now it is 4 times longer!

Try this:

#include "Keyboard.h"

const int buttonCount = 4;

const int buttonPin[buttonCount] = {1, 2, 3, 4};          
const int buttonKey[buttonCount] = {97, 98, 99, 100};          
int previousButtonState[buttonCount];

void setup() {
  for (int b=0; b<buttonCount; b++) {
    pinMode(buttonPin[b], INPUT_PULLUP);
    previousButtonState[b] = HIGH;
  }
  Keyboard.begin();
}

void loop() {
  for (int b=0; b<buttonCount; b++) {
    int buttonState = digitalRead(buttonPin[b]);
  
    if (buttonState == LOW && previousButtonState[b] == HIGH) {
      Keyboard.press(buttonKey[b]);
      delay(50);
    }

    if (buttonState == HIGH && previousButtonState[b] == LOW) {
      Keyboard.release(buttonKey[b]);
      delay(50);
    }
 
    previousButtonState[b] = buttonState;
  }
}

Awesome thanks that cleans it up so nicely