I have a sketch which is use to controll temperatures and humidity.
I am also using a LCD, what i want to do is to push a button and turn the LCD backlight and it should turn off 30 seconds later.
Record the time from millis() when the button was pushed and you put the light on. You will need to use either a static or a global variable to remember this value.
Then each time through your main loop, check if that time has expired. If it has, turn the light off.
Use an unsigned long variable to keep the value of mills() function at the moment the LED went on.
Then add another if in your code and if 30 secs have passed, turn the LED off.
Drop the -else- part of your code too.
You may want to further improve the code, so as to detect the previous state and execute only the required part of the code, but you will find it out as you go on.
Drunken1337:
How do i need to modify this to achive the 30 seconds? I cannot use delay because i have other parts in the sketch.
Untested code:
const byte pushbuttonPin=3;
const byte backlightPin=A2;
boolean backLightActive=false;
unsigned long backLightActiveSince;
#define BACKLIGHT_DURATION 30000 // in milliseconds
void backLightTask()
{
// check if backlight is active for BACKLIGHT_DURATION
if (backLightActive && millis()-backLightActiveSince>=BACKLIGHT_DURATION)
{ // if so
backLightActive=false; // shut down backlight
}
else if (!backLightActive && digitalRead(pushbuttonPin)==LOW)
{ // if backlight is off and the button is down
backLightActive=true;
backLightActiveSince=millis();
}
digitalWrite(backlightPin, backLightActive);
}
Include in your sketch, set pinModes in setup(), and call "backLightTask();" within the loop() function.
Loop function code must be non-blocking (no "delay()" and no "busy waiting" allowed.
You don't use a delay to do that you can use the loop, you have a variable " i = 0 "
which in every loop gets +1 " i++ " so in this case you write a simple code which gonna be:
( if you press a button to light up the LCD back light "which gonna activate the " i" variable too" the back light will stay HIGH untill the " i >= 30", 30 loops, after 30 loops the back light will stay LOW untill
you press again the button).
Now I know I just told you how it's gonna work without the code, you can imagine
how exactly you gonna write the code.
Get a button (code), backlight (code) and combine them as i said with a " if " loop code.
Done
ps(almost forgot, 30 loops in case your loop is delay(1000); 1sec, if it's lower then calculate how many loops you need to turn the backligh off, if delay(500); half sec then you need 60loops, i>=60; )
it's not hard
Domino60:
Now I know I just told you how it's gonna work without the code, you can imagine how exactly you gonna write the code.
No, sorry, I can't.
And I have a suspicion that what you describe is a first cousin of delay() and will not be suitable. Please post your suggested code so I can see that I am wrong.
Thank you all very much for your help, special thanks to the guys who posted code, since i am pretty bad at this, it will help me a lot!
I'll test it later today if i can make it work.
And I have a suspicion that what you describe is a first cousin of delay() and will not be suitable. Please post your suggested code so I can see that I am wrong.
well yea if we gonna use a "IF statement" then the loop will run inside the if untill it's ended so the outside
code in void loop will not run.