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.


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.

  • 1
    note: the BlinkWithoutDelay example "accounts for the roll over " – Juraj Feb 11 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

  screenUpdate = millis();

  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



Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.