Hello,
I'm currently writing a piece of code and I need to switch something on and off with the press of a button.
this in itself is a rather simple piece of code and I've got it working perfectly.
int state = HIGH;
int reading;
int previous = LOW;
int debounce = 1000;
unsigned long previousMillis = 0;
void setup () {
pinMode(4, INPUT);
digitalWrite(4, HIGH);
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis(); //store the time
{
reading = digitalRead(4); //read the state of the button
if (reading == LOW && previous == HIGH && (unsigned long)(currentMillis - previousMillis) >= debounce) //when the button gets pressed and the debounce time has passed go to next part
{
if (state == HIGH) //change the state value
state = LOW;
else
state = HIGH;
previousMillis = currentMillis; // saves the time to compare against in the next loop
}
previous = reading; // stores either high or low to compare against in the next loop
}
Serial.println(state); // prints a 1 or 0 so I can see if the value switched
}
When the button gets pressed the value changes and does not change again untill you release (the release part is important for the next part) the button and press it again.
Now what I want to do is achieve the same but with having multiple physical buttons as imputs.
I know I can just wire the buttons in parrallel but it is prefered to have the buttons wired individually incase 1 of the buttons needs to do something else in the future without having to change wiring.
So I've tried changing the code to accomodate for this.
I have succeeded at changing the code but now I've run into some unwanted behaviour.
int state = HIGH;
int reading;
int previous = LOW;
int debounce = 1000;
unsigned long previousMillis = 0;
void setup () {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.println("start");
}
void loop() {
unsigned long currentMillis = millis(); // store time
for(int i=2;i<10;i++) //to cycle trough the different buttons
{
reading = digitalRead(i); // checks a different button on each loop (2 trough 9)
if (reading == HIGH && previous == LOW && (unsigned long)(currentMillis - previousMillis) >= debounce) //when the button gets pressed and the debounce time has passed go to next part
{
if (state == HIGH) // change the state value
state = LOW;
else
state = HIGH;
previousMillis = currentMillis; // saves the time to compare against in the next loop
}
previous = reading; // stroes either high or low to compare against in the next loop
}
Serial.println(state); // prints a 1 or 0 so I can see if the value switched
}
The unwanted behaviour I've run into is that after the debounce timer has passed (currently 1 second) and the button is still being pressed the value will switch again since the previous value has been changed due to the fact that the button that was checked before has the HIGH value.
Is there a way to modify this code to work as the single button code so that keeping the button pressed will not trigger another switch without adding a whole lot of extra code?
-Warzon3