Thank you Paul & Tom, I've corrected the wiring mistakes which has stabilized it and fixed the "staying pressed" issues, however I'm still noticing that the code seems to be running twice because I'm seeing "Button X was pressed" twice in the serial monitor for each push. I'm not even sure it's a problem, but since my intent is to build a button box, I don't want to record 2 button presses if it was only pressed once. Also, I'm trying to take advantage of the internal pullup resistor, so I rewired as such. Any ideas?
Thank so much!
#include <Joystick.h>
#define joyButton1 9
#define joyButton2 8
#define joyButton3 7
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 3, 0, false, false, false, false,
false, false, false, false, false, false, false);
int lastButton1State = 0;
int lastButton2State = 0;
int lastButton3State = 0;
void setup() {
pinMode(joyButton1, INPUT_PULLUP);
pinMode(joyButton2, INPUT_PULLUP);
pinMode(joyButton3, INPUT_PULLUP);
Joystick.begin();
}
void loop() {
int currentButton1State = !digitalRead(joyButton1);
if(currentButton1State != lastButton1State)
{
Joystick.setButton(0, currentButton1State);
lastButton1State = currentButton1State;
Serial.println("Button 1 was pressed");
}
int currentButton2State = !digitalRead(joyButton2);
if(currentButton2State != lastButton2State)
{
Joystick.setButton(1, currentButton2State);
lastButton2State = currentButton2State;
Serial.println("Button 2 was pressed");
}
int currentButton3State = !digitalRead(joyButton3);
if(currentButton3State != lastButton3State)
{
Joystick.setButton(2, currentButton3State);
lastButton3State = currentButton3State;
Serial.println("Button 3 was pressed");
}
delay(50);
}
