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;
}
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);
}
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
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;
}
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 ?
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