Pages: [1]   Go Down
Author Topic: Switch on->LED on for 5 seconds until switch is reset?  (Read 1410 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?


Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1199
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1199
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1199
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Help to self-help  smiley

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.
Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: January 29, 2011, 10:44:28 pm by swifftyb » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 1
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1199
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

Pages: [1]   Go Up
Jump to: