[Solved] Don't know why this isn't working (DeBounceing)

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 :slight_smile:

Did you mean to use i for your iteration variable and buttonPin as your paramenter to digitalRead?

	for (int i=0; i <= 9; i++){
		// read the state of the switch into a local variable:
		int reading = digitalRead(buttonPin);
	READ_STATES();

Perhaps you've noticed that the Arduino has functions like digitalRead() and Serial.readUntil(), not DIGITAL_READ() and SERIAL.READ_UNTIL().

I'd strongly recommend that you get with the program and stop screaming.

PaulS:
Perhaps you've noticed that the Arduino has functions like digitalRead() and Serial.readUntil(), not DIGITAL_READ() and SERIAL.READ_UNTIL().

I'd strongly recommend that you get with the program and stop screaming.

The reason I do this is its easier to see my functions, I don't care if everyone else is not using caps. It doesn't affect the code at all...

The reason I do this is its easier to see my functions

But now your functions look like your variables and tables too.
But, you're quite correct, it doesn't affect how the code doesn't work.

What Oracle said, + buttonPin is not even declared so the program doesn't compile as is.


Rob

PixelMaster:
The reason I do this is its easier to see my functions, I don't care if everyone else is not using caps.

Easier for you, maybe, but you're flouting widely adopted conventions about how to name functions/variables/constants/macros and that is a distraction for anyone trying to make sense of it - for example the people here who you are asking for help from.

Graynomad:
What Oracle said, + buttonPin is not even declared so the program doesn’t compile as is.

Changed it,

//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() {
	ReadStates();
	
	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"); 	
	
	SaveStates();
}

//Reads Pins and stores each value into an int array
//With Debouncing
void ReadStates() {
	for (int i=0; i <= 9; i++){
		// read the state of the switch into a local variable:
		int reading = digitalRead(BTN[i]);

		// 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 SaveStates(){
	//Saves STATES[10] to STATES_OLD[10];
	for (int i=0; i <= 9; i++){
		STATES_OLD[i] = STATES[i];
   }  
}

But it’s still not returning 0 when the button is pressed

Figured it out, Wasn’t saving the read state, This works now

//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_READ[10]	= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
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() {
	ReadStates();
	
	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"); 	
	
	for (int i=0; i <= 9; i++) {
		Serial.print(DEBOUNCE_MIL[i]); 
		Serial.print(", "); 
	}
	Serial.println("DEBOUNCE_MIL");  	
	
	SaveStates();
}

//Reads Pins and stores each value into an int array
//With Debouncing
void ReadStates() {
	for (int i=0; i <= 9; i++){
		// read the state of the switch into a local variable:
		byte reading = digitalRead(BTN[i]);

		// 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_READ[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;
		}
		
		STATES_READ[i]	= reading;
    }
}


// Saves the STATES array to the STATES_OLD
void SaveStates(){
	//Saves STATES[10] to STATES_OLD[10];
	for (int i=0; i <= 9; i++){
		STATES_OLD[i] = STATES[i];
   }  
}