Hello, I am trying to program my Arduino so that when a certain colored button is pushed it lights an LED accordingly. I am modifying a sample sketch called debounce and it does what i need but, not what I want. I am having trouble assigning multiple buttons and multiple LEDs. Please review my coding and help would be very much appreciated.
const int buttonPinR = 2;
const int buttonPinG = 3;
const int buttonPinB = 4;
const int ledPinR = 11;
const int ledPinG = 10;
const int ledPinB = 9;
;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
It would have been helpful if Grag38 had used the # button around that code. It would then have looked like this:
for (i=0; i<3; i++)
{
if (reading[i] != lastButtonState[i])
{
lastDebounceTime[i] = millis();
}
}
The idea is that you have 3 switches on 3 pins (which could/should also be stored in an array), so you need three readings, three states, and three times - one for each switch.
While you could have created readingR, readingG, and readingB, and similar variables for the times and states, the arrays allow you to loop through the elements. The individual variables do not.
In fact, your code could be quite short if you used arrays for the switch and LED pins, too. A loop in setup() to set the pin mode (and enable the pullup resistors), and another loop in loop() to read and act upon the switch states.