Is this weird for simpleTimer?

my aim is to display three sets of data on OLED, with 2 seconds between each screen.

this is the display related code bits:

#include <SimpleTimer.h>
double foodTemperature;
int foodWarningTemp = 55; //starting number in Celcius
int cook_h, cook_m, cook_s;

SimpleTimer timer

void displayUpdate()
{
  display.clearDisplay();
  display.println("Food probe");
  display.print(foodTemperature);
  display.print((char)247);
  display.println("C");
  display.display();
  timer.setTimeout(displayFreq * 1000L, displayUpdate2);
}

void displayUpdate2()
{
  display.clearDisplay();
  display.println("Food warn:");
  display.print(foodWarningTemp);
  display.print((char)247);
  display.println("C");
  display.display();
  timer.setTimeout(displayFreq * 1000L, displayUpdate3);
}

void displayUpdate3()
{
  display.clearDisplay();
  display.println("Cook time:");
  display.print(cook_h);
  display.print(":");
  display.println(cook_m);
  display.display();
}

void setup()
{  
timer.setInterval(displayFreq * 3L * 1000L, displayUpdate);
}

so it works a finite amount of times then only the displayUpdate() keeps updating.

is there an easier way yo do what i want? or am i using simpleTimer wrong(again)?

Itā€™s maybe easier to call setInterval and the likes in the functions themselves. There is no need to do it in setup(). It makes you even more inflexible. You can set the interval, timeout or enable/disable timers from everywhere :slight_smile:

i call the setTimeoutā€™s in the functions, i only set one interval in the setup, otherwise - how do i start the display function?

I see now, I think I looked at it before the coffee, LOL.

If it works, it works. I think itā€™s fine like this. With my latest project (which I abandoned quickly) I use a single timer to start other timers, that is too say, I have a function to enable all timers and this function is called ONCE with a setTimeout. This is made to start running function after I completed all the setup parts.

Imagine you want to synchronize time first and then start running your ā€œnormalā€ code. You canā€™t do that in the setup() because the time sync has to take place from the Blynk.run() part.

Your code could probably be a bit shorter and everything can be in one function if you set flags to display what you want. E.g. displayCooktime = 1 (or 0) and make a switch or if statement.

it DOESNT work (sorry for shouting)

the setIntervals seem stop working?

Iā€™m sorry, your question stated if there was simpler way, so I assumed it worked. The timeout runs only once! So if you set displayFreq to 5, it waits 5000ms before it executes, so it will always execute one time and I assume that is not what you want.

I would do a sequence for that purpose, I think this is the simplest way to achieve what you want.

1 Like

Maybe this?

Just set interval on nextScreen()

typedef void (*displayScreenList[])();
displayScreenList Screens = { displayUpdate, displayUpdate2, displayUpdate3 };
uint8_t CurrentScreenNumber = 0;

#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))

void nextScreen() {
  // add one to the current screen number, and wrap around at the end
  CurrentScreenNumber = (CurrentScreenNumber + 1) % ARRAY_SIZE( Screens);
}

1 Like

Can someone please tell me what the ā€˜Lā€™ is doing in the interval? Iā€™ve looked through this forum, and the documentation at Arduino Playground - HomePage and canā€™t find any reference as to what the ā€˜Lā€™ is for, and what other options might be available.

Thanks!
BRAD

L stands for Long which is a data type bigger than an integer. If you want the timer to run every 60 days it will be a number of milliseconds greater than you can hold in an integer variable.

1 Like

Brilliant-- Thank you! This was driving me crazy. Can I assume that default = int, L= long, and D=double?

BRAD

Convention is that the Timer interval is shown as xL milliseconds irrespective of the duration but you could drop it if the hardware you are using can handle the integer size. ESPā€™s (32bit) and Arduinoā€™s (most are 16bit) have different integer sizes.

You wouldnā€™t be using a double as there isnā€™t a floating point aspect to a millisecond.

Try changing setup() to this:
timer.setTimeout(displayFreq * 3L * 1000L, displayUpdate);

and at the end of displayUpdate3 add:
timer.setTimeout(displayFreq * 3L * 1000L, displayUpdate);

1 Like

thanks everyone for your considered and helpful responses, but unfortunatly it really seems that there might be something in other parts of my code that are disrupting the simpleTimer for the display section. (weird!)

so i have moved the displayUpdate call to the end of another function (the temperature update section) that is called every 5 seconds, and the display sequence works without disruptionā€¦ :slight_smile:

[if anyone is interested i can post the two full sets of code for analysis, but at least it is now workingā€¦ ]