I am trying to make a light bulb stay on for a set amount of time and then return to the off default position of the device. having issues of wither blinking the device and staying on or just using th button as a toggle.
const int BUTTON_PIN = 2; // Arduino pin connected to button's pin
const int LED_PIN = 8; // Arduino pin connected to LED's pin
// variables will change:
int lightState = LOW; // the current state of LED
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup() {
//Serial.begin(9600); // initialize serial
pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
currentButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(BUTTON_PIN); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
// toggle state of LED
//lightState = !lightState;
digitalWrite(LED_PIN, lightState);
delay (100);
lightState = !lightState;
digitalWrite(LED_PIN, lightState);
delay (100);
//lightState = !lightState;
digitalWrite(LED_PIN, lightState);
delay (100);
//lightState = !lightState;
// control LED arccoding to the toggled state
}
}
any help would be appreciated, in the current configuration it just acts as a toggle.