Single LED, Single Switch--- On Delay (without using delay) then off

Hello

I am trying to figure out how I can simply have a single pushbutton to turn on an LED for 5 seconds and then go back to previous state.

also if I click the pushbutton at any time during the 5 seconds I want the LED to Reset back to off until the button is pressed again.

does any one have any idea on how to proceed with this? I am a complete novice and I have attempted the following bellow with no luck at all.

I have used and scrounged peoples lines from previous projects hoping to get an understanding.

any help is appreciated.

// constants won't change. Used here to
// set pin numbers:
const int ledPin = 13; // the number of the LED pin

// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 5000; // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}

void loop()
{
// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}

I am trying to figure out how I can simply have a single pushbutton to turn on an LED for 5 seconds and then go back to previous state.

Where are you reading the state of the switch? Are you basing your decisions on whether the switch IS pressed or on whether it has BECOME pressed?

The state change detection example shows the latter.

Record when you turn the LED on. Periodically (on every pass through loop()), see if the LED is on (the on time will be greater than 0), and enough time has passed to warrant turning it off. If so, turn it off.

also if I click the pushbutton at any time during the 5 seconds I want the LED to Reset back to off until the button is pressed again.

If the LED is on when the switch becomes pressed, turn it off and set the on time to 0.

does any one have any idea on how to proceed with this?

Yes.

I have attempted the following bellow with no luck at all.

Programming is not like buying a lottery ticket. No luck is involved. Logic is.

I appreciate your input... although I clearly do not know how to use timers and was hoping some one could show me or give me an example.

as for logic vs luck... I am inexperienced and trying to learn, so sorry for my language I clearly have to learn how to approach people on this forum.

Kind Regards

Aaron

This will help you with how to read a switch change:http://www.arduino.cc/en/Tutorial/ButtonStateChange

was hoping some one could show me or give me an example.

Try this example,

const byte buttonPin = A1;
const byte ledPin = 13;
unsigned long buttonPressTime;
unsigned long period = 5000;  //milliseconds
boolean lightTheLed = false;
byte currentButtonState = HIGH;
byte previousButtonState = HIGH;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);  //turn on the internal pullup resistor.  Connect the other side of the button to GND
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);  //the LED start by being off
}

void loop()
{
  currentButtonState = digitalRead(buttonPin);
  if (currentButtonState != previousButtonState && currentButtonState == LOW)  //button has become pressed
  {
    buttonPressTime = millis();    //save the time
    lightTheLed = true;
  }

  if (millis() - buttonPressTime >= period)    //its time to turn off the LED
  {
    lightTheLed = false;
  }

  if (lightTheLed)
  {
    digitalWrite(ledPin, LOW);  //LED on
  }
  else
  {
    digitalWrite(ledPin, HIGH);  //LED off
  }
  previousButtonState = currentButtonState;  //save the button state for the next time through loop()
}

Pressing the button will turn on the LED for 5 seconds as will holding it down. You need to add code to turn the LED off if the button is pressed during the 5 second on period and change pin numbers and logic levels to suit your setup.

Hi,
Bob's example is awesome.

Look at the first example

you can use attach interrupt just follow his code and :

void pinChange ()
{
 if (digitalRead (reset_button) == LOW) // reset_button is just the BUTTON in the link
     lightTheLed = false;
}

you can then press the button on pin 2(interrupt 0) to reset it as you have stated in your question

Why complicate things by using an interrupt ? A digitalRead() in the right place in loop() will do just as well.

Why complicate things by using an interrupt ? A digitalRead() in the right place in loop() will do just as well.

Thanks, Bob.