Hi Community,
Short term:
I need to monitor the state of six buttons and illuminate an LED if one of the buttons is pressed.
Long term:
I am designing a game controller with a particular force feedback feature. I will be using two arduino boards. The master (uno r3) will receive game telemetry and control a motor and two electric solenoids. The slave board (pro micro) will be seen by the PC as a game controller with six buttons. I need to monitor the state of the six buttons and transmit this state over i2c.
Before I delve into the i2c part of the programming I wanted to test what I have so far using an LED to monitor button state.
I've completed the code for the slave board and it works as a game controller but the LED is not doing what I expect. The LED illuminates while the button on pin 10 is pressed but for the other buttons the LED just flashes once (very dimly).
Can anyone identify what I have done wrong?
#include <Joystick.h>
//declare joystick
Joystick_ Joystick;
//Led monitoring pin
const int ledPin = 15;
// first pin mapped to a button
const int pinToButtonMap = 5;
// array of last state of the buttons
int lastButtonState[6] = {0,0,0,0,0,0};
void setup() {
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// Initialize Joystick Library
Joystick.begin();
}
void loop()
{
for (int index = 0; index < 6; index++)
{
int currentButtonState = !digitalRead(index + pinToButtonMap);
if (currentButtonState == HIGH){
digitalWrite(ledPin, HIGH);}
else{
digitalWrite(ledPin,LOW);}
if (currentButtonState != lastButtonState[index]){
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;}
}
delay(50);
}