Go Down

Topic: Switch on->LED on for 5 seconds until switch is reset? (Read 2652 times) previous topic - next topic


Switch in the on position->led lights up for 5 seconds and off again untill switch reset?
Is there any way i can get my arduino to run like this?


Yes very easily:

In the loop examine the switch input pin.
  if HIGH and FLAG is LOW
    turn on the LED; wait 5 secs; turn LED off; set FLAG HIGH
  if LOW and FLAG is HIGH
    Set FLAG LOW


Thanks for the reply. Im totally new to all this. Ermm do you recon you can get a little more detailed with that? Absolute beginners talk? Much appreciated. Samuel


Yes.... in effect writing the program. It will be the same lines just with correct syntax. For this look at the http://arduino.cc/en/Reference/HomePage page and click on if() to get syntax details. Remember the two '=' inside the if - a common beginner fault.

A nearly complete example close to what you need is the http://arduino.cc/en/Tutorial/Button

Examine this, include/modify what I suggest, and see if you can work it out.

Good luck/work!


Thanks for the point in the right direction but im still totally stumpped.. Im waiting for my arduino to turn up in the post. So im trying to crack this code thing before it comes. Any more help possible?


Help to self-help  :)

Download the Arduino software, and write your code. USe the Arrow-button (or Ctrl-R) to compile. You can do this without the arduino board. When you have written what you hope is the right code and it compiles without errors, then post it here, and I'll gladly comment on it.


Jan 29, 2011, 07:39 pm Last Edit: Jan 30, 2011, 04:44 am by swifftyb Reason: 1
This is what i got so far but no luck...

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);    

void loop(){
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {    
   // turn LED on:    
    digitalWrite(ledPin, HIGH);
    digitalWrite(ledPin, LOW);
 else {
   // turn LED off:
   digitalWrite(ledPin, LOW);



At the end of the delay, it might help if you turned the LED off. It doesn't go off on it's own.
The art of getting good answers lies in asking good questions.


I changed that but for some reason it still wont work... hmm


"Does not work" is not enough a description.  What is happening, and what are you expecting to happen?

You left out the code with the FLAG variable from my earlier suggestions. It ensured you also have to release the button/toggle the switch back. Your code will leave the LED on for as long as the switch is on.

Go Up