# Trying to program multiple buttons for a keyboard

**URL:** <https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330>\
**Category:** Programming\
**Created:** [March 15, 2023, 3:41pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330 "2023-03-15T15:41:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 3:41pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/1 "2023-03-15T15:41:54Z")

</div>

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.

```cpp
#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;
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 15, 2023, 3:44pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/2 "2023-03-15T15:44:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 3:51pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/3 "2023-03-15T15:51:34Z")

</div>

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

```cpp
#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);
  }

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 15, 2023, 3:53pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/4 "2023-03-15T15:53:57Z")

</div>

> [@mbishopfof](#):
>
> now I am getting a redefinition error of the button pin

```arduino
const int buttonPin = 1;  

```

```arduino
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 ?

---

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 3:55pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/5 "2023-03-15T15:55:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 15, 2023, 3:56pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/6 "2023-03-15T15:56:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 3:59pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/7 "2023-03-15T15:59:09Z")

</div>

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

```cpp
buttonPin2

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 15, 2023, 4:01pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/8 "2023-03-15T16:01:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 4:08pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/9 "2023-03-15T16:08:24Z")

</div>

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.

```cpp
#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;
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 15, 2023, 4:12pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/10 "2023-03-15T16:12:43Z")

</div>

```cpp
  previousButtonState2 = buttonState;

```

Are you confusing your previousButtonState variables with one another ?

---

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 4:17pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/11 "2023-03-15T16:17:00Z")

</div>

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.

```cpp
#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;
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 15, 2023, 4:18pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/12 "2023-03-15T16:18:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 4:21pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/13 "2023-03-15T16:21:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 15, 2023, 4:24pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/14 "2023-03-15T16:24:51Z")

</div>

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

```cpp
  previousButtonState2 = buttonState;

```

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

---

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 4:27pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/15 "2023-03-15T16:27:25Z")

</div>

So that would be something like this,

```cpp
#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.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 15, 2023, 4:31pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/16 "2023-03-15T16:31:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 4:41pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/17 "2023-03-15T16:41:18Z")

</div>

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

```cpp
#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;

}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 15, 2023, 5:00pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/18 "2023-03-15T17:00:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [March 15, 2023, 5:01pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/19 "2023-03-15T17:01:43Z")

</div>

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

Try this:

```cpp
#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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![mbishopfof](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/mbishopfof/32/933106_2.png) [@mbishopfof](https://forum.arduino.cc/u/mbishopfof)\
**Post date:** [March 15, 2023, 5:09pm UTC](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330/20 "2023-03-15T17:09:34Z")

</div>

Awesome thanks that cleans it up so nicely

[Next page](https://forum.arduino.cc/t/trying-to-program-multiple-buttons-for-a-keyboard/1102330.md?page=2)
