I am trying to debounce 10 different buttons
this is my code
//Buttons
byte BTN[10] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
//Debounce
long DEBOUNCE_MIL[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
//All States in int array
byte STATES[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
byte STATES_OLD[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
void setup() {
Serial.begin(9600);
// pinModes set to INPUT_PULLUP
for (int i=0; i <= 9; i++) {
pinMode(BTN[i] , INPUT_PULLUP);
}
}
void loop() {
READ_STATES();
for (int i=0; i <= 9; i++) {
Serial.print(STATES[i]);
Serial.print(", ");
}
Serial.println("STATES");
for (int i=0; i <= 9; i++) {
Serial.print(STATES_OLD[i]);
Serial.print(", ");
}
Serial.println("STATES_OLD");
SAVE_STATES();
}
//Reads Pins and stores each value into an int array
//With Debouncing
void READ_STATES() {
for (int i=0; i <= 9; i++){
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != STATES_OLD[i]) {
// reset the debouncing timer
DEBOUNCE_MIL[i] = millis();
}
if ((millis() - DEBOUNCE_MIL[i]) > 50) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
STATES[i] = reading;
}
}
}
// Saves the STATES array to the STATES_OLD
void SAVE_STATES(){
//Saves STATES[10] to STATES_OLD[10];
for (int i=0; i <= 9; i++){
STATES_OLD[i] = STATES[i];
}
}
Its out putting
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES_OLD
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES_OLD
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES_OLD
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES_OLD
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES_OLD
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES_OLD
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, STATES
Even if I'm pressing the buttons
I have fallowed the tutorial exactly but its not working, I'm new to coding and not sure what I did wrong. Any help is much appreciated ![]()