Hello friends, I've found this debounce code online and have tried for hours to make it work x4
however I come up with several errors this is my first time doing this and when knowing how I plan on using my arduino for my house's lighting. here is my code wich makes 1 Light work but unsure how to proceed to the other Led's.
const int buttonPin2 = 2;
const int ledPin3 = 3;
const int buttonPin4 = 4;
const int ledPin5 = 5;
const int buttonPin8 = 8;
const int ledPin9 = 9;
const int buttonPin10 = 10;
const int ledPin11 = 11;
int ledState3 = HIGH; //the current state of the output pin
int buttonState2; //the current reading from the input pin
int lastButtonState2 = LOW; //the previous reading from the input pin
int ledState5 = LOW; //the current state of the output pin
int buttonState4; //the current reading from the input pin
int lastButtonState4 = HIGH; //the previous reading from the input pin
int ledState9 = LOW; //the current state of the output pin
int buttonState8; //the current reading from the input pin
int lastButtonState8 = HIGH; //the previous reading from the input pin
int ledState11 = LOW; //the current state of the output pin
int buttonState10; //the current reading from the input pin
int lastButtonState10 = HIGH; //the previous reading from the input pin
unsigned long lastDebounceTime2 = 0;
unsigned long debounceDelay2 = 50;
unsigned long lastDebounceTime4 = 0;
unsigned long debounceDelay4 = 50;
unsigned long lastDebounceTime8 = 0;
unsigned long debounceDelay8 = 50;
unsigned long lastDebounceTime10 = 0;
unsigned long debounceDelay10 = 50;
void setup() {
pinMode (buttonPin2, INPUT);
pinMode (ledPin3, OUTPUT);
pinMode (buttonPin4, INPUT);
pinMode (ledPin5, OUTPUT);
pinMode (buttonPin8, INPUT);
pinMode (ledPin9, OUTPUT);
pinMode (buttonPin10, INPUT);
pinMode (ledPin11, OUTPUT);
digitalWrite(ledPin3, ledState3);
digitalWrite(ledPin5, ledState5);
digitalWrite(ledPin9, ledState9);
digitalWrite(ledPin11, ledState11);
}
void loop() {
int reading = digitalRead(buttonPin2);
if (reading != lastButtonState2) {
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay2) {
if (reading != buttonState2) {
buttonState2 = reading;
if (buttonState2 == HIGH) {
ledState3 = !ledState3;
}
}
}
digitalWrite(ledPin3, ledState3);
lastButtonState2 = reading;
}
Thank you in advance.