Go Down

Topic: Timed Backlight (Read 1 time) previous topic - next topic

AlbertaClipper

I could use a hand with this little problem. I have a LCD03 displaying some temperatures of my CNC machine motors. I am using a Arduino Duemilanove to read/calculate the temperatures and send them (serially) to the LCD03. I would like to add a momentary switch that would turn the backlight of the display on for 30 seconds and then have the backlight go off. I don't want the sketch to stop calculating and displaying the temperatures while the backlight timer is on. I can get the backlight to come on while I hold the switch on but I can't get it to stay on for a certain amount of time with just one touch of a momentary button. How can I get the program to continue while the backlight timer is counting? Maybe I'm thinking about this too hard? ;)
Thanx

jmrobert

#1
Jul 07, 2010, 07:19 pm Last Edit: Jul 07, 2010, 07:20 pm by jmrobert Reason: 1
use this:  http://www.arduino.cc/playground/Code/TimedAction
have the button reset or enable the task.

when the task trips, have it turnoff the backlight, and disable itself.

AlbertaClipper

Why won't the following code work?
I wrote it for a 5 second LED instead of the backlight but its the same idea.

[code]
#define LED 13                  //LED connected to digital pin 13
#define Button 36               //button on pin 36

int state = 0;                 // state of Button

unsigned long currentMillis;    //millis() value at time of Button Pushed
unsigned long onTime;          //millis() at time Button was pushed + 30 seconds
                              //= millis() value to reach before turning LED off
unsigned long timeNow;         //current millis()
long onDuration = 5000;       //LED on for 5 sec


void setup()
{
pinMode(LED, OUTPUT);
pinMode(Button, INPUT);
}



void loop()
{
 state = digitalRead(Button);  //read Button status and store it in state
if (state == HIGH)
     currentMillis = millis();      // read millis() value when Button is pushed
     onTime = currentMillis + onDuration ;  // millis() value when Button is
                                            // pushed plus 5 seconds
     timeNow = millis();                                                    
 if (onTime - timeNow <= 0){
   digitalWrite(LED, HIGH);
 }else{
   digitalWrite(LED,LOW);}
   delay(50);  
 
}
code]

PaulS

You realize, I hope, that indenting is no substitute for curly braces.
Code: [Select]
if (state == HIGH)
    currentMillis = millis();      // read millis() value when Button is pushed
    onTime = currentMillis + onDuration ;  // millis() value when Button is
                                           // pushed plus 5 seconds
    timeNow = millis();

If the button state is HIGH, currentMillis will get a new value. onTime and timeNow will always get new values.

You asked:
Quote
Why won't the following code work?

It does work. Perhaps you meant to ask "Why doesn't this code do what I want it to do?". In which case you really need to explain what you meant for it to do, and how what it does differs from what you wanted it to do.

AlbertaClipper

Thanks PaulS, I should have asked "Why dosen't this code do what I want it to do?". Which is when I push the momentary pushbutton I want the LED to come on for 5 seconds then turn off. When I get this working I will adapt it to turn my LCD backlight on for a determined length of time with just a momentary pushbutton.


davekw7x

#5
Jul 08, 2010, 05:17 pm Last Edit: Jul 08, 2010, 05:35 pm by davekw7x Reason: 1
Quote
...what I want it to do...


Maybe try pseudo-code.

First a couple of notes:

It makes more sense to me to use "offTime" as the variable name to indicate when the LED will be turned off.

Then, instead of doing a subraction, just compare present time with offTime.  The logic seems more obvious to me.  (Also: see the footnote.)


Anyhow, here's how the logic lays itself out for me:
Code: [Select]

loop:

 IF button_is_pressed THEN
     offTime = presentTime + onDuration;
 END IF
 
 IF (presentTime < offTime) THEN
     Turn_on_the_LED
 ELSE
     Turn_off_the_LED
 END IF



For the real code, "presentTime" is just millis(), right?  I mean you can use a separate variable, but it's not really necessary.


Regards,

Dave

Footnote: If you subtract unsigned long ints, the result is an unsigned long int.  Can't be less than zero.

AlbertaClipper

Thanks Dave...worked like a charm. :)

Go Up