Hello, I have a simple project I started just an experiment to learn a few things. I was hoping for help with what should be a simple thing.
I have a arduino uno. Hooked to it are 4 buttons, and 4 LEDs. I just wanted to have each button toggle on and off the corresponding LED. I used the example that Jeremy Blum put together on his site, just added three more buttons.
Link to his tutorial; // it's great.
It works great, and I added his denouncing function. I'll post my code at the end of this message.
My first question is. Do I need a separate function for each debounced button? Or, can I write one function to debounce each. Right now in the code it's 4 separate functions. One for each button.
Next question. I would like it to print out the serial port the state of the button like "Button 1 ON" or OFF. I can get it to kind of work, but it constantly writes it out over and over. I figured I'd need some sort of for or while loop to do this but just can's seem to figure it out. I looked at the included example with the Arduino IDE under digital>Stage Change Detection. I could kind of get it to work. It would print out that the led was on, but also right after it would say it was off.
Here's the code without the Serial.print stuff, could someone please show me how they would do it.
Thank you very much in advance for any help.
int switchPin_1 = 2;
int switchPin_2 = 4;
int switchPin_3 = 7;
int switchPin_4 = 8;
int ledPin_1 = 3;
int ledPin_2 = 5;
int ledPin_3 = 6;
int ledPin_4 = 9;
boolean lastButton_1 = LOW;
boolean lastButton_2 = LOW;
boolean lastButton_3 = LOW;
boolean lastButton_4 = LOW;
boolean currentButton_1 = LOW;
boolean currentButton_2 = LOW;
boolean currentButton_3 = LOW;
boolean currentButton_4 = LOW;
boolean ledOn_1 = false;
boolean ledOn_2 = false;
boolean ledOn_3 = false;
boolean ledOn_4 = false;
void setup(){
pinMode(switchPin_1, INPUT);
pinMode(switchPin_2, INPUT);
pinMode(switchPin_3, INPUT);
pinMode(switchPin_4, INPUT);
pinMode(ledPin_1, OUTPUT);
pinMode(ledPin_2, OUTPUT);
pinMode(ledPin_3, OUTPUT);
pinMode(ledPin_4, OUTPUT);
Serial.begin(9600);
Serial.print("Serial Ready!");
}
void loop(){
currentButton_1 = debounce_1(lastButton_1);
if (lastButton_1 == LOW && currentButton_1 == HIGH) {
ledOn_1 = !ledOn_1;
}
lastButton_1 = currentButton_1;
digitalWrite(ledPin_1, ledOn_1);
currentButton_2 = debounce_2(lastButton_2);
if (lastButton_2 == LOW && currentButton_2 == HIGH) {
ledOn_2 = !ledOn_2;
}
lastButton_2 = currentButton_2;
digitalWrite(ledPin_2, ledOn_2);
currentButton_3 = debounce_3(lastButton_3);
if (lastButton_3 == LOW && currentButton_3 == HIGH) {
ledOn_3 = !ledOn_3;
}
lastButton_3 = currentButton_3;
digitalWrite(ledPin_3, ledOn_3);
currentButton_4 = debounce_4(lastButton_4);
if (lastButton_4 == LOW && currentButton_4 == HIGH) {
ledOn_4 = !ledOn_4;
}
lastButton_4 = currentButton_4;
digitalWrite(ledPin_4, ledOn_4);
}
// debounce functions for each button.
boolean debounce_1(boolean last){
boolean current = digitalRead(switchPin_1);
if (last != current) {
delay(5);
current = digitalRead(switchPin_1);
}
return current;
}
boolean debounce_2(boolean last){
boolean current = digitalRead(switchPin_2);
if (last != current) {
delay(5);
current = digitalRead(switchPin_2);
}
return current;
}
boolean debounce_3(boolean last){
boolean current = digitalRead(switchPin_3);
if (last != current) {
delay(5);
current = digitalRead(switchPin_3);
}
return current;
}
boolean debounce_4(boolean last){
boolean current = digitalRead(switchPin_4);
if (last != current) {
delay(5);
current = digitalRead(switchPin_4);
}
return current;
}