Go Down

Topic: switch on a led for 5 sec and turn off?? (Read 5 times) previous topic - next topic

zhirantaha

Jul 19, 2012, 04:13 pm Last Edit: Jul 19, 2012, 04:21 pm by zhirantaha Reason: 1
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

majenko

If you don't want our suggestions, what do you want?
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

zhirantaha


majenko

Simple version:

Code: [Select]

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: [Select]

{
  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! :)
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

zhirantaha


AWOL

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

majenko

It's hard to know what the requirements are from the brief few words the OP has written.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

PGT

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

ledguy315

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);   
}

NI$HANT

"Real Men can Accomplish  Anything"
- <a href="http://www.winacro.com">  <b><font color="red">Website</font></b> </a> 
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੁਂ!

manosv


More complex version:

Code: [Select]

{
  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! :)


It needed some alterations ;) but its working great.Thank you SIR

jpkotor

This is just what I needed!!! :)

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

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview