I am trying to make a weather monitor using Arduino. I want my weather monitor to send me a message of the weather data every 5 hours using GSM sheild. Also, there is an LCD on my weather monitor which shows live weather conditions. The problem I am facing is that the delay of 5 hours in my code also applies to the LCD which updates the LCD every 5 hours and not every second as it should. Please tell me how to run these two functions simultaneously.
-
see the BlinkWithoutDelay exampleJuraj– Juraj ♦2019年02月11日 12:54:14 +00:00Commented Feb 11, 2019 at 12:54
-
Here is a simple abstraction (Timemark) that makes it very easy to write. github.com/mikaelpatel/Arduino-Timemark/blob/master/Timemark.hMikael Patel– Mikael Patel2019年02月11日 15:31:19 +00:00Commented Feb 11, 2019 at 15:31
-
please post your codejsotola– jsotola2019年02月11日 17:18:06 +00:00Commented Feb 11, 2019 at 17:18
2 Answers 2
Consider using a finite state machine to track multiple tasks. Specifically, the state in which you send your message once every 5 hours. And, the state you update your LCD, perhaps once every second.
As your tasks are associated with time, consider using the built in Arduino call to millis() to find how many milliseconds the Arduino program has been running. This will not block your program. Instead it will immediately return with a number you then need to manage.
To create a delay, use the above number from millis() and add the number of milliseconds for the desired delay. Call millis() repeatedly until it returns the calculated number or a number greater than the calculated number. Use this to drive your state machine. Perhaps you only use millis() to count seconds and use software to count up to 5 hours (5 x 60 x 60 seconds).
Note, millis() will eventually roll over as it is of type "unsigned long". That is, it will roll over once about every 50 days. If you want to keep accurate timing when this happens, you will have to account for the roll over in your program.
-
1note: the BlinkWithoutDelay example "accounts for the roll over "2019年02月11日 13:31:10 +00:00Commented Feb 11, 2019 at 13:31
Something along the lines of this pseudocode should help you.
unsigned long weatherUpdate; // Use unsigned longs for millis times
unsigned long screenUpdate; // You need to worry about wraparound less often
unsigned long timeNow;
setup {
unsigned int interval1 = 0; // 0 Should be whatever delay you need
unsigned int interval2 = 0; // 0 Should be whatever delay you need
updateScreen();
screenUpdate = millis();
updateWeather();
weatherUpdate = millis;
}
loop {
timeNow = millis();
/*
These if statements are true when the interval between the current time
and the last time the if ran is greater than the required interval. For
organization purposes, maybe create separate functions for when you
update your screen and weather functions instead of just replacing
the function stubs I've included.
*/
if((timeNow - screenUpdate) > interval1){
updateScreen(); // Do what you need
screenUpdate = millis(); // Update time since last run
}
if((timeNow - weatherUpdate) > interval2){
updateWeather(); // Do what you need
weatherUpdate = millis(); // Update time since last run
}
}