Go Down

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

jstone

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?



Msquare

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


jstone

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

Msquare

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!

jstone

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?

Msquare

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.

jstone

#6
Jan 29, 2011, 07:39 pm Last Edit: Jan 30, 2011, 04:44 am by swifftyb Reason: 1
Hi,
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);
    delay(5000);   
    digitalWrite(ledPin, LOW);
 }
 else {
   // turn LED off:
   digitalWrite(ledPin, LOW);
 }
}

Suggestions?
Thanks

PaulS

At the end of the delay, it might help if you turned the LED off. It doesn't go off on it's own.

jstone

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

Msquare

"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