I keep the following sketch as a template.
Because it's delay()-less (ok, well apart from a tiny ersatz debounce) you can press and hold a button then press another while first is held and see that the two are pressed, then release one, press and hold a third etc etc (fingers permitting) in any sequence. Bwod on L13 to prove there's no delay()s.
No need to specify how many buttons there are, it uses sizeof() to find out for itself.
If you act on the fact that a button is newly pressed or released, remember to clear its flag.
buttonIsNewlyPressed[i] = false;
buttonIsNewlyReleased[i] = false;
// state change detect on a button array
// 30 august 2019
// has blink-without-delay on pin13 to prove no blocking
// the buttons
byte buttonPins[] = {8, 9, 10}; //the buttons must be wired from pin to ground, pinmodes are input_pullup
const byte howManyButtons(sizeof(buttonPins) / sizeof(byte));
bool buttonStates[howManyButtons]; // current state of the button
bool lastButtonStates[howManyButtons]; // previous state of the button
bool buttonIsNewlyPressed[howManyButtons];
bool buttonIsNewlyReleased[howManyButtons];
//the bwod led
int bwodLedInterval = 500;
unsigned long previousMillisBwod;
bool bwodState = false;
void setup()
{
// initialize serial communication:
Serial.begin(9600);
Serial.println("setup() ... ");
Serial.println(".... state change detect on a button array ....");
Serial.print("Compiler: ");
Serial.print(__VERSION__);
Serial.print(", Arduino IDE: ");
Serial.println(ARDUINO);
Serial.print("Created: ");
Serial.print(__TIME__);
Serial.print(", ");
Serial.println(__DATE__);
Serial.println(__FILE__);
Serial.println(" ");
// initialize the button pins as input with pullup so active low
// make sure the button is from pin to ground
Serial.println("Should show button, pin, 1100:");
for (int i = 0; i < howManyButtons; i++)
{
pinMode(buttonPins[i], INPUT_PULLUP);
//initialize button states
buttonStates[i] = digitalRead(buttonPins[i]);
lastButtonStates[i] = buttonStates[i];
buttonIsNewlyPressed[i] = 0;
buttonIsNewlyReleased[i] = 0;
Serial.print(i);
Serial.print(", ");
Serial.print(buttonPins[i]);
Serial.print(", ");
Serial.print(buttonStates[i]);
Serial.print(lastButtonStates[i]);
Serial.print(buttonIsNewlyPressed[i]);
Serial.print(buttonIsNewlyReleased[i]); //should show button, pin, then 1100
Serial.println("");
}
//initialise pulse led
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, bwodState);
Serial.println("setup() done");
Serial.println("Press a button....");
Serial.println(" ");
}
void loop()
{
bwod();
checkForButtonStateChange();
doSomeThingWithANewlyPressedButton();
doSomeThingWithANewlyReleasedButton();
} //loop
void checkForButtonStateChange()
{
for (int i = 0; i < howManyButtons; i++)
{
buttonStates[i] = digitalRead(buttonPins[i]);
// compare the buttonState to its previous state
if (buttonStates[i] != lastButtonStates[i]) // means it changed... but which way?
{
if (buttonStates[i] == LOW) // changed to pressed
{
Serial.print(i);
Serial.println(" newly pressed");
buttonIsNewlyPressed[i] = true;
}
else // changed to released
{
// if the current state is HIGH then the button was released
Serial.print(" ");
Serial.print(i);
Serial.println(" newly released");
buttonIsNewlyReleased[i] = true;
}
// ersatz de-bounce
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonStates[i] = buttonStates[i];
}
} // checkForButtonStateChange()
void doSomeThingWithANewlyPressedButton()
{
for (int i = 0; i < howManyButtons; i++)
{
if (buttonIsNewlyPressed[i])
{
Serial.print("Using the new press of button ");
Serial.println(i);
buttonIsNewlyPressed[i] = false;
}
}
}//doSomeThingWithANewlyPressedButton
void doSomeThingWithANewlyReleasedButton()
{
for (int i = 0; i < howManyButtons; i++)
{
if (buttonIsNewlyReleased[i])
{
Serial.print(" Using the new release of button ");
Serial.println(i);
buttonIsNewlyReleased[i] = false;
}
}
}//doSomeThingWithANewlyReleasedButton
void bwod()
{
if (millis() - previousMillisBwod >= bwodLedInterval)
{
previousMillisBwod = millis();
bwodState = !bwodState;
digitalWrite(LED_BUILTIN, bwodState);
}
} //bwod
Sample output:
setup() ...
.... state change detect on a button array ....
Compiler: 4.9.2, Arduino IDE: 10805
Created: 06:35:44, Nov 15 2019
C:\Users\sayHovis\Documents\Arduino\statechangedetect_array\statechangedetect_array.ino
Should show button, pin, 1100:
0, 8, 1100
1, 9, 1100
2, 10, 1100
setup() done
Press a button....
0 newly pressed
Using the new press of button 0
2 newly pressed
Using the new press of button 2
0 newly released
Using the new release of button 0
1 newly pressed
Using the new press of button 1
2 newly released
Using the new release of button 2
1 newly released
Using the new release of button 1