Pages: [1]   Go Down
Author Topic: switch on a led for 5 sec and turn off??  (Read 5122 times)
0 Members and 1 Guest are viewing this topic.
Sulaymaniyah-Kurdistan-Iraq
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
stay foolish - stay hungry
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi

please if you replay don't write any suggestion?? ok??---write the code
i want when you turn on the led after 5 sec turn off and doesn't turn on again????
thank for replay
« Last Edit: July 19, 2012, 09:21:17 am by zhirantaha » Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you don't want our suggestions, what do you want?
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Sulaymaniyah-Kurdistan-Iraq
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
stay foolish - stay hungry
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i need code
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple version:

Code:
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:

Code:
{
  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! smiley
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Sulaymaniyah-Kurdistan-Iraq
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
stay foolish - stay hungry
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

by the way thank's smiley
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26471
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You realise the simple version in reply #3 version doesn't actually meet your requirements?

@OP: If you only want code written for you and don't want to put in any effort, will you please post in "Gigs and Collaborations"?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's hard to know what the requirements are from the brief few words the OP has written.
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

start from the blinc example.. study it and read the reference
this is so simple that i think you shouldnt ask for a code example but do a little bit homework.
the more rewarding it will be when you have solved it.

hint try using an if statement... and a variable..
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try not to be so demanding or angry. This is the simplest way to light an LED for five seconds then turn off permanently. It is just modified blink. I have tested it, so no worries. Enjoy.

int led = 13;
void setup() {               
  pinMode(led, OUTPUT);
digitalWrite(led, HIGH); 
  delay(5000); 
}
void loop()
digitalWrite(led, LOW);   
}
Logged

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 12
Posts: 1616
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

it's soooooo00 cake!
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

More complex version:

Code:
{
  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! smiley

It needed some alterations smiley-wink but its working great.Thank you SIR
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is just what I needed!!! smiley

However, how would you go about modifying this so that you can turn it off manually before the time expires with another button press?

This would help me A LOT!!
Logged

Pages: [1]   Go Up
Jump to: