[SOLVED] Timer.setinterval - best use?

Hi I have an arduino with sensors and with an attiny85 measuring peaks from a powermeter.

the strange thing is the attiny85 gets unstable in counting peaks over weeks, but all the sensor data are captured correctly.

So I suspect that the timer.setInterval’s used in conjuction with the onPulse:

attachInterrupt(digitalPinToInterrupt(interruptPin), onPulse, FALLING);

the setIntervals:

  timer.setInterval(3003L, sendSensor1);
  timer.setInterval(3020L, sendSensor2);
  timer.setInterval(3009L, sendSensor3);
  timer.setInterval(3012L, sendSensor4);
  timer.setInterval(3016L, sendUptime);
  timer.setInterval(1000,showCurrentTime);
  timer.setInterval(60000L, sendMinMax); 

Should I define the miliseconds differently the avoid “collisions” in reading sensors?

is there 3 seconds between ever reading or 0.006 between?

@tjohansen if you are worried about “time collisions” perhaps look at setTimeout() whereby function 2 will be called X ms after function 1 finishes, then function 3, function 4 etc.

so instead of using setinterval() I could use setTimeout() ? same feature but waiting for the last to end before next start?

Correct.

when looking at simpletimer library setTimeout is used for a “run once” and not “looped” on interval.

You set one setInterval() and at the end of the called function have a setTimeout() to the next function and at the end of that function another setTimeout() to the next function etc.

So setTimeout() runs each time setInterval() kicks in and it doesn’t just run once.

what is goin on here?

Senario 1
does it do sendSensor1 and then wait 3020 milisecs and the do sendSonsor2?

Senario 2
or does it wait 3003 milisecs and then do sendSensor1
and then after 6 milisecs it does sendSensor3
and then after 3 milisecs it does sendSensor4
and then after 4 milisecs it does sendUptime
and then after 4 milisecs it does sendSensor2
etc…

so in the end of

void sendSensor1() 
{
  sensors1.requestTemperatures();
  Blynk.virtualWrite(1, sensors1.getTempCByIndex(0) + 6.5); //adjusted with 6.5 celsius because measuring outside of tube    
}

I should add

setTimeout(1000, sendSensor1)

Not quite:

in setup()

timer.setInterval(3000L, sendSensor1);

then:

void sendSensor1() 
{
  sensors1.requestTemperatures();
  Blynk.virtualWrite(1, sensors1.getTempCByIndex(0) + 6.5); //adjusted with 6.5 celsius because measuring outside of tube   
 int callFunction2 = timer.setTimeout(1000, sendSensor2)   // TWO not ONE
}

void sendSensor2() 
{
  // your sensor2 code here  
 int callFunction3 = timer.setTimeout(1000, sendSensor3)   // THREE
}
1 Like

oh… so you only use setinterval to call the first void function and then use the next void function to call the next void function.

nice when learning new stuff.

1 Like