Simple version:
void setup()
{
pinMode(13,OUTPUT); // LED output
pinMode(2,INPUT); // Button input
}
void loop()
{
while(digitalRead(2)==LOW);
digitalWrite(13,HIGH);
delay(5000);
digitalWrite(13,LOW);
}
More complex version:
{
pinMode(13,OUTPUT); // LED output
pinMode(2,INPUT); // Button input
}
void loop()
{
static unsigned char ledState = LOW;
static unsigned char buttonState = LOW;
static unsigned char lastButtonState = LOW;
static unsigned long ledCameOn = 0;
// If the LED has been on for at least 5 seconds then turn it off.
if(ledState == HIGH)
{
if(millis()-ledCameOn > 5000)
{
digitalWrite(13,LOW);
ledState = LOW;
}
}
// If the button's state has changed, then turn the LED on IF it is not on already.
buttonState = digitalRead(2);
if(buttonState != lastButtonState)
{
lastButtonState = buttonState;
if((buttonState == HIGH) && (ledState == LOW))
{
digitalWrite(13,HIGH);
ledState = HIGH;
ledCameOne = millis();
}
}
}
Note: I haven't tested either bits of code, so do they work? Who knows!