byte button[] = {30, 26, 28, 32};
byte numberOfButtons = 4;
byte buttonState[numberOfButtons];
byte activeButton[numberOfButtons];
boolean hit;
void setup()
{
for (byte i = 0; i < numberOfButtons; i++)
{
pinMode(button[i], INPUT_PULLUP); //Check for LOW for a button press
}
Serial.begin(115200);
}
void loop()
{
for (byte i = 0; i < numberOfButtons; i++) //read all buttons at beginning of loop()
{
buttonState[i] = !digitalRead(button[i]);
}
for (byte i = 0; i < numberOfButtons; i++)
{
if (buttonState[i] && (!otherButtonPressed(i) || activeButton[i])) //if no other buttons are active or this one is already active
{
activeButton[i] = true;
Serial.print(i+1);
}
else
{
activeButton[i] = false;
}
}
}
boolean otherButtonPressed(byte currentButton)
{
hit = 0;
for (byte i = 0; i < numberOfButtons; i++)
{
if (i == currentButton)
{
continue; // skip test for button passed to function
}
if (activeButton[i] == HIGH) // if any other button is currently the active one
{
hit = true;
}
}
return hit; //Were any other buttons already pressed?
}