// https://forum.arduino.cc/t/function-for-incrementing-an-integer-value/1045878
# define NPINS 4 // actual number realized in wiring
const int buttonPin[8] = {3, 4, 5, 6,}; // just four four now
int scene[8] = {0, 0, 0, 0, 0, 0, 0, 0};
bool dirt = false; // just to keep printing to a minumum
int lastButtonState[8] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
int buttonState[8] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
void sceneSelect(int potNum)
{
buttonState[potNum] = digitalRead(buttonPin[potNum]);
if (buttonState[potNum] != lastButtonState[potNum]) {
if (buttonState[potNum] == LOW) {
scene[potNum]++;
if (scene[potNum] >= 5) scene[potNum] = 0;
dirt = true; // we need to print out all the scene values as there has been a change
}
lastButtonState[potNum] = buttonState[potNum];
}
}
void setup()
{
Serial.begin(9600);
Serial.println("function for incrementing an integer value\n");
for (unsigned char tt = 0; tt < NPINS; tt++)
pinMode(buttonPin[tt], INPUT_PULLUP);
}
void loop()
{
for (unsigned char tt = 0; tt < NPINS; tt++)
sceneSelect(tt);
if (dirt) {
for (unsigned char tt = 0; tt < NPINS; tt++) {
Serial.print(scene[NPINS - 1 - tt]); // printing was backwards
Serial.print(" ");
}
Serial.println();
dirt = false;
}
delay(20); // effective global debounce!
}