Pages: [1]   Go Down
Author Topic: How to use millis function to turn led on for 5s then turn it off indefinitely  (Read 1159 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This code turns on the led for 5 seconds then switches the led off but i have a problem...
Code:
const int ledPin =  13;      // the number of the LED pin

unsigned long time;     

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT); 
  digitalWrite(ledPin,LOW); 
}

void loop()
{  time= millis();
   if (time >= 1000 && time < 1020){digitalWrite(ledPin,HIGH);};  // turn it on when its been running for a second
   if (time >= 6000 && time < 6020){digitalWrite(ledPin,LOW);};  // turn it off when its been running for 6 seconds
 
// write rest of code here . as written it will repeat about every 49 days when millis() 'goes round the clock'
}



When i add a button press to this it no longer works?
What am i missing people?

Code:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledOdd =  13;      // the number of the LED pin
const int ledPin = 50;
// variables will change:
int buttonstate = 0;   
// variable for reading the pushbutton status
int buttonPress = 0;

int buttonState = 0;

int ledState = LOW;   
int ledEoff = LOW;
 // ledState used to set the LED
long previousMillis = 0;
long interval = 1000;
long intervalTen = 10000;
unsigned long time;

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

void loop(){
  buttonState = digitalRead(buttonPin);
  time= millis();
 
  if(buttonState == HIGH) {
   if (time >= 1000 && time < 1020){digitalWrite(ledPin,HIGH);};  // turn it on when its been running for a second
   if (time >= 6000 && time < 6020){digitalWrite(ledPin,LOW);};  // turn it off when its been running for 6 seconds
}

}
Logged

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

Quote
What am i missing people?
How have you wired the switch? You are not using the internal pull-up resistor, so you need an external resistor. Do you have one? How is it wired?

How quickly did you press the switch after restarting the Arduino? That code will only turn the LED on it the switch is pressed within the 5 second window. After the 5 second window ends, the switch has nothing to do.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The switch is wired up using a pull up resistor. Tested on arduino button example.
Even if i press the button immediately i still get nothing. Ive it with a 15 second window too..

Is this beyond the arduinos capabilities?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 441
Posts: 23834
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So you have the button pulled up - reads as High unless the button is pushed? 
The buttonpush brings the signal low and prevents the time comparing from happening the way I read it.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to create a system that can do the following.

"Push button clicked once,
An LED comes on for 5 seconds and then switches off."

If the button is pressed again...The above happens again..

If pressed again... The above happens again.

Thats all im after
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thats what im looking for but how do i wright this in code form? Thats where i get stuck

if(buttonState == HIGH) && (ledState == LOW)
digitalWrite(ledPin, HIGH);
off_time = millis() + 5000
(???)if

if millis() > off_time
digitalWrite,(ledPin, LOW);
(???)if

Am i on the right track?
« Last Edit: March 02, 2011, 07:50:29 pm by swifftyb » Logged

The Netherlands, Europe
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A little "thinko" slipped in your code when adding the button press detection code: you give it a /very/ tight window for it to turn on / off: only if it's pressed exactly within 1.000 - 1.020s and off at exactly 6.000s to 6.020s. So, it is highly probable you are simply missing that window of opportunity.

You are on track now, but it is important to remember the state of the LED as well -- because you want to be able to check if it needs to turn off.

Let's unroll that pseudo code .. As it can only be on or off, you could use a boolean for the LED status, so at the top of the code, where the off_time is defined, also throw in this one:
unsigned long off_time;
boolean ledState = false;


Then, in your loop() code, there should be something like this;

if ((ledState) && (millis()>=off_time)) /* is it on and is it later or equal to off_time */
{
    digitalWrite(ledPin,LOW);
    ledState = false;
}
else if (!ledState) /* is it off? */
{
    buttonState = digitalRead(buttonPin);
    if(buttonState == HIGH)
    {
        digitalWrite(ledPin, HIGH);
        ledState = true;
        off_time = millis() + 5000;
    }
}


This may behave more like you want it to.

To understand more of coding in general, you may find it interesting to observe the difference in behaviour if you change the
"else if (!ledState)"
to just
"else"

Hope this helps!
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Dude, i will give it a try and keep you posted as to how i get on.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A little "thinko" slipped in your code when adding the button press detection code: you give it a /very/ tight window for it to turn on / off: only if it's pressed exactly within 1.000 - 1.020s and off at exactly 6.000s to 6.020s. So, it is highly probable you are simply missing that window of opportunity.

You are on track now, but it is important to remember the state of the LED as well -- because you want to be able to check if it needs to turn off.

Let's unroll that pseudo code .. As it can only be on or off, you could use a boolean for the LED status, so at the top of the code, where the off_time is defined, also throw in this one:
unsigned long off_time;
boolean ledState = false;


Then, in your loop() code, there should be something like this;

if ((ledState) && (millis()>=off_time)) /* is it on and is it later or equal to off_time */
{
    digitalWrite(ledPin,LOW);
    ledState = false;
}
else if (!ledState) /* is it off? */
{
    buttonState = digitalRead(buttonPin);
    if(buttonState == HIGH)
    {
        digitalWrite(ledPin, HIGH);
        ledState = true;
        off_time = millis() + 5000;
    }
}


This may behave more like you want it to.

To understand more of coding in general, you may find it interesting to observe the difference in behaviour if you change the
"else if (!ledState)"
to just
"else"

Hope this helps!

Thanks so much dude. This is exactly what i was looking for and the code is simple enough to understand! Your the man!
Logged

Pages: [1]   Go Up
Jump to: