hello all i need help adding a debounce to this part of my code
digital_state0[count] = digitalRead(mux_buttonPin[0]); //set mux0 input val for each digitalstate value
digital_state1[count] = digitalRead(mux_buttonPin[1]); //set mux1 input val for each digitalstate value
digital_state2[count] = digitalRead(mux_buttonPin[2]); //set mux2 input val for each digitalstate value
digital_state3[count] = digitalRead(mux_buttonPin[3]); //set mux3 input val for each digitalstate value
here is the full code of the program
int mux_buttonPin[4] = {5,6,7,8};
int buttonState0 = LOW;
int lastButtonState0 = LOW;
int buttonState1 = LOW;
int lastButtonState1 = LOW;
int buttonState2 = LOW;
int lastButtonState2 = LOW;
int buttonState3 = LOW;
int lastButtonState3 = LOW;
int digital_state0 [7] = {
LOW};//set aside input values for mux
int digital_state1 [7] = {
LOW};//set aside input values for mux
int digital_state2 [7] = {
LOW};//set aside input values for mux
int digital_state3 [7] = {
LOW};//set aside input values for mux
int r0 = 0; //value select pin at the 4051 (s0)
int r1 = 0; //value select pin at the 4051 (s1)
int r2 = 0; //value select pin at the 4051 (s2)
int row = 0; // storeing the bin code
int count = 0; // just a count
int bin [] = {
000, 1, 10, 11, 100, 101, 110, 111};//bin = binär, some times it is so easy
void setup(){
for (int i=0; i<=7; i++) {
pinMode(digital_state0[i], INPUT); //mux0 inputs
pinMode(digital_state1[i], INPUT); //mux1 inputs
pinMode(digital_state2[i], INPUT); //mux2 inputs
pinMode(digital_state3[i], INPUT); //mux3 inputs
}
pinMode(2, OUTPUT); // s0
pinMode(3, OUTPUT); // s1
pinMode(4, OUTPUT); // s2
pinMode(mux_buttonPin[0], INPUT); // mux0 read pin //digital
pinMode(mux_buttonPin[1], INPUT); // mux1 read pin //digital
pinMode(mux_buttonPin[2], INPUT); // mux2 read pin //digital
pinMode(mux_buttonPin[3], INPUT); // mux3 read pin //digital
Serial.begin(9600);
}
void loop () {
for (count=0; count<=7; count++) {
row = bin[count];
r0 = row & 0x01;
r1 = (row>>1) & 0x01;
r2 = (row>>2) & 0x01;
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
//Serial.println(bin[count]); //print current bin
digital_state0[count] = digitalRead(mux_buttonPin[0]); //set mux0 input val for each digitalstate value
digital_state1[count] = digitalRead(mux_buttonPin[1]); //set mux1 input val for each digitalstate value
digital_state2[count] = digitalRead(mux_buttonPin[2]); //set mux2 input val for each digitalstate value
digital_state3[count] = digitalRead(mux_buttonPin[3]); //set mux3 input val for each digitalstate value
//Serial.println(digital_state0[count]); // print digital_state0 for debug
//Serial.println(digital_state1[count]); // print digital_state1 for debug
//Serial.println(digital_state2[count]); // print digital_state2 for debug
//Serial.println(digital_state3[count]); // print digital_state3 for debug
buttonState0 = digital_state0[count];
buttonState1 = digital_state1[count];
buttonState2 = digital_state2[count];
buttonState3 = digital_state3[count];
//MUX0
if (buttonState0 != lastButtonState0) {
// if the state has changed, increment the counter
if (buttonState0 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
Serial.print("MUX0 button ");
Serial.print(count);
Serial.println(" pressed");
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.print("MUX0 button ");
Serial.print(count);
Serial.println(" released");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState0 = buttonState0;
//MUX1
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
Serial.print("MUX1 button ");
Serial.print(count);
Serial.println(" pressed");
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.print("MUX1 button ");
Serial.print(count);
Serial.println(" released");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1 = buttonState1;
//MUX2
if (buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState2 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
Serial.print("MUX2 button ");
Serial.print(count);
Serial.println(" pressed");
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.print("MUX2 button ");
Serial.print(count);
Serial.println(" released");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState2 = buttonState2;
//MUX3
if (buttonState3 != lastButtonState3) {
// if the state has changed, increment the counter
if (buttonState3 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
Serial.print("MUX3 button ");
Serial.print(count);
Serial.println(" pressed");
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.print("MUX3 button ");
Serial.print(count);
Serial.println(" released");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState3 = buttonState3;
}
delay (10); //delay 10 for read between values
}
thank you in advance its driving me crazy i dont know were or how to implement the debounce