Hi,
I am new to Arduino and I was wondering if you could help me with my first project. My goal for this project is to have a LED light blink between two different rates when a debounce switch is pressed. If you could please look over my sketch and help me out on how to understand this problem at a coding level, I would greatly appreciate it. I am using an Arduino Uno board along with two resistors, a LED light and a debounce switch, which is shown in the attached image. Thank you!
const int buttonPin = 2;
const int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
if (buttonState == LOW) {
ledState = !ledState;
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
}
}
}
digitalWrite(ledPin, ledState);
lastButtonState = reading;
}
test.ino (1.03 KB)