need programming idea

hello,im new to programming..i need some help on my small lab project...i need to use a button to turn on the led and off after 2 seconds..bt even im keep on holding the button the led must off after 2 seconds.... need help guys..tq :)

pravinan: hello,im new to programming..i need some help on my small lab project...i need to use a button to turn on the led and off after 2 seconds..bt even im keep on holding the button the led must off after 2 seconds.... need help guys..tq :)

When does the LED come back on? Once the button is released?

1st situation : when i press the button and release the led must turn on...after 2 second it must turn off.... 2nd situation : when i press the button and hold it the led must turn on and after 2 second must turn off even im still holding the button...

both situation must happen in one programming...

IDE -> file/examples/digital/stateChangeDetection

several things at the same time

When the button *becomes closed, * not *is closed, * reset a free-running timer. As long as the timer accumulated value is less than the preset time turn the LED on, otherwise turn it off.

Hello.

If you want something super simple, you could try what's given below. This is assuming that nothing happens on release of the button. It also disregards cases where the button is released and again pressed during the time that the LED is on. This code compiles but has not been tested.

if (digitalRead(buttonPin) == HIGH){
  digitalWrite(ledPin, HIGH);
  delay(2000);
  while (digitalRead(buttonPin) == HIGH);
}