Timer without blocking a running programm

Hey guys… now I could finish my little home-control-project-sketch using Blynk. Everything works, but not as well as I would like. Two simple loops avoid a clean running of the hole sketch. These loops are called only every second, but the Blynk-Timer make judder the whole sketch. Is the a better way to check an input every second automatically??

BlynkTimer timer; 
void setup()
{
timer.setInterval (1000, checkDigitalInput); 
}
void checkDigitalInput(){
 RoomlightState = digitalRead(WemosPin_D1);
}

Attach an interrupt to your Wemos pin D1.
Depending on the type of behaviour you want, a different type of interrupt can be used. There are RISING, FALLING and CHANGE interrupts available.

When the interrupt criteria is met (i.e the pin changes from LOW to HIGH (RISING), HIGH to LOW (FALLING) or simply changes (CHANGE)), the specified function is called.
This saves you having to poll the pin every second.

Pete.

It sounds interested. I only want check two states (Roomlight On/Off and a presence detector). You didn’t happen to have something for example??

In that case, use a CHANGE interrupt then when the interrupt fires check the state of the pin to see if it’s HIGH or LOW as you do at the moment.

I don’t have an example to hand, but a quick search should give you some simple examples.
If you find Arduino examples then you’ll see that the Arduino is limited to which pins can have interrupts attached. This restriction doesn’t apply to the Wemos D1 Mini (assuming that’s what you’re using).

Pete.

Thank you in the meantime :wink:

First problem as good as solved. Second one: Measure the temperature every hour to get a nice diagram on SuperChart widget. Yes… I could use 24 timer widgets but does it exist a nicer methode to get every hour a pulse to start the measure. Also there I have to avoid the Blynk/Simple timer. There I evaluate no input signal to start the loop :see_no_evil::see_no_evil:

Are you saying that you need these measurements to be taken at a specific time, not simply every hour (e.g they need to be at at 09:00am 10:00am, 11:00am etc)?

Pete.

If it’s possible yes :wink:
I want to get the value of the temperature in periodic distances. If it’s now 10:00 or 10:03 is equal

Every hour is easy, just using a Blynk timer set to 3600000 Millis to call the function that takes a temperature reading and upload it to Blynk.
Doing it this way will take a reading every hour, but when during that hour will depend on when you first started-up your NodeMCU.

If you want to do it at a precice time - say on the hour - then you’d probably use the RTC widget followed by a countdown timer.

Pete.


How do you mean? What I have to insert to start and stop time to call it periodically?
I prefer your solution 1 :blush:

No, you’re getting confused.

Option 1 is very simple.
It uses a Blynk timer in your sketch to run a fu cation, in the sketch, to take the temperature once every hour.

Option 2 is a bit more complex, and only needed if you wnat the readings to be taken at a certain time (every hour on the hour for example)
This would require the RTC widget (not a time input widget) to be added to your app. There are no parameter for the RTC widget, except setting the time zone.
In your sketch you would then get the RTC time and check to see if it is an exact hour (minutes ==0).you’do this by checking the RTC time regularly (at least once every minute) with a timer.
You could then keep doing this check once every minute, or use a Blynk timeout timer within your code to then take a reading in one hours time.

I’d reccomend you go for option 1 if you’re not worried about when during the hour the reading is taken.

Pete.

The problem of the simple way with the Blynk-timer works but disturb the flow of the program. For example, without any running timer which runs periodically the output led fade up very smooth, with one it slows down it and the rest of the sketch
You perhaps don’t have any example where you used your “way 2” by you?? Would be nice :hugs:
I am not lazy, I only don’t want to invent the wheel usually new.

Having a timer that is triggered once per hour shouldn’t make any difference to your program flow.
Option 2 will be worse, because the timer will need to run once per minute at least.

Maybe you should post your full code, as it could be something to do with the other stuff you’re doing.

If it turns out that having any timers running will disrupt your LEDs then you should connect your temperature sensor to another device and have that take your temperature readings and upload them.

Pete.

Probably you are right. At the moment I have two Blynk timers running. One runs two times per second to check the inputs and one every hour. Yesterday I comented both timers and everything ran smooth. Today I will try to remove the comment from timer which runs every hour to see what happen. And if it doesn’t run I on take 24 timer widgets.
Thanks you a lot :wink:

How?? I use timers all the ‘time’… and yes, while they can cause issues if too fast or way to many… I am talking much more than two and much faster than 1 second :stuck_out_tongue:

As suggested, you could show the code, explain the needs, and perhaps we can further assist with ideas.

When you defined these two timers, are they defined like this, with the same timer instance name (“timer” in the example below), or have you created two different instances?

timer.setInterval (500, twicepersecondfunction); 
timer.setInterval (360000, hourfunction); 

sharing your complete code would save us all a lot of time.

Pete.

Naturally
BlynkTimer timerCallInput
BlynkTimer timerMeasure

timerCallInput.setIntervall (500, checkInput)
timerMeasure.setIntervall (360000, IndoorMeasure)

Okay, well that’s one of your problems then.
One Blynk timer object can handle 16 different timer instances.
You’ve declared 2 objects, capable of handling 32 different instances, but have only 1 timer instance assigned to each object.

This uses additional memory and processing power to handle these two objects. You should have just one, as in my example above.

Pete.

Every day I lern something new :joy::joy:

1 Like

Me too. The only problem is that my brain works the same way as Homer Simpson’s - every time I learn something new I have to forget something else to make space for it.
Now my head is full of Blynk stuff and I can’t remember when my wedding anniversary is!
(Well, that’s my excuse and I’m sticking to it).

Pete.

3 Likes