Avoid Delay

@Gunner told you how to do

Sorry … But I do not understand that, where is the 30-second pause?

timer.setTimeout(30000, DoSomethingLater); // will run this function in 30 seconds

that’s what I did … but how do I make that loop repeat?

 if (TempC > (TempP + 4))
    A = TempC;
    timer.setTimeout(30000, encendido);
  if (TempC < (TempP + 3))
    B = TempC;
    timer.setTimeout(20000, apagado);

void encendido()
  float Temp2 = sensors.getTempCByIndex(1);
  if (Temp2 <= A)
    digitalWrite (RELAY, HIGH);

Your code snippets are still hard to decipher.

The concept of timers depends on the timer you use… you really need to sit down and read all about their different options, then experiment with simple concepts before plunging into something that affects real-world controls… checkout that link above… to my example sketches… it also has links to the SimpleTimer library that Blynk Timer is based on.

Then you can do lots of different “timing” things without the delay()

Another pseudo code example… you want to have an action, every 5 minutes to take a reading, then compare it to itself 60 seconds later (for whatever reason… just spitballin…)

Ideally you would have an interval timer in setup() repeatedly calling a function, say… every 5 minutes. Then that function calls another for the reading, then runs a different type of timer… the timeout timer, which only runs the one time, x seconds later, to call another function… that then calls your sensors again, compares them, and does whatever logic you want based on the results.

void setup() {
timer.setInterval(3000000, MyFirstreading);  // Interval timer will run this function every 5 minutes

void MyFirstreading() {
MySensors();    // run the sensor function
firstTemp = temp;  // transfer the value
timer.setTimeout(60000, MySecondReading);  // then run a timeout timer that runs once, 60 seconds later, each each time this containing function is called

void MySecondReading() { 
MySensors();  // run the sensor function
secondTemp = temp;
// compare firstTemp with secondTemp and do something based on result

void MySensors() {
temp = sensor_read_command;  // read a sensor, store it in a variable called temp

This example could be further refined with less functions… but I am trying to spread them out (the differing timers) for better viewing :wink:

1 Like

Opps :blush: I had the wrong type of timer showing in my above post, the one in setup() is supposed to be an interval, not a timeout. It is fixed now… and a few other syntax issues… happens when I make non-running pseudo code that is not verified in a compiler.



3000000 or 30000?

Well, it depends on how long you want the timer to run… I may have added an extra zero, making my example 50 minutes… opps :stuck_out_tongue:

The defining number (a Long Integer) is in milliseconds, so 1,000 = 1 second & 60,000 = 1 minute. Multiply that by 5 = 300,000 = 5 minutes.

Math and me don’t get along, and we can’t use the thousands designation , in the code… so that is my excuse :innocent:


I’m somewhat mathematically challenged, (AKA I’m crap at maths) so I prefer to do it this way:

timer.setInterval(5x(1000*60), MyFirstreading);   //   5 minutes
timer.setInterval(50x(1000*60), MyFirstreading);  //  50 minutes
timer.setInterval(120x(1000*60), MyFirstreading); // 120 minutes = 2 hours


timer.setInterval(2x(1000*60*60), MyFirstreading); // 2 hours

I like this approach because it’s easy to see the actual value that you’re going to get at the start of the equation, and I can usually mange to remember that there are 1,000 millis in a second, 60 seconds in a minute and 60 minutes in an hour (at least after the first couple of cups of coffee!).



Excellent, and dare I say, obvious (just not to myself :blush: ) this might save me from having to remove my socks when counting…


Thank you very much to all!!!
It is working perfectly :ok_hand:.
You were very kind to me.