B4R Question Loop and Timers?

miker2069

Active Member
Licensed User
Longtime User
So I saw this thread and https://www.b4x.com/android/forum/threads/timers-loopers-and-callsubplus.65989/#content it raised an eyebrow (and heart rate *smile*). So as I *understand it* (which is limited), I didn't think Arduino had "threads" in the normal sense of operating system based threads (since there is no OS).

So I'm intrigued at how timers are implemented (which is very very freaking awesome as I didn't think that was possible with Arduino type MCs). How are you doing this? You mentioned a message queues?

So to implement any sort of "loop" you should use at least one timer correct? If you want that loop to run as fast as possible, do you fire the timer, with something like zero? Sorry for the simplistic or somewhat incorherent questions, just trying to get my head around it as I'm my mind is now swirling with all the possibilities here.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are no threads in B4R. There is a message queue. Whenever the main thread is not busy with running any code, it processes the next event in the message queue (if there is one). Some of the events are scheduled which means that they will only run after a specific time. This is how timers and CallSubPlus are implemented.

If you want that loop to run as fast as possible, do you fire the timer, with something like zero?
No. You will use AddLooper. The looper sub will be called as fast as possible.
 
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
Thank You, very intriguing. I wired up a couple of colored leds and blink them at different frequencies with a couple of timers, and have another timer that's varying the frequency (by updating the interval of the led timers) every 1 second. Very straightforward with B4R, would have been a bit cumbersome in normal Arduino C.

I assume then I could use the timers say on a NodeMCU (esp8266 based) with my web server in the main thread and timers to collect or drive data on digital pins?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I assume then I could use the timers say on a NodeMCU (esp8266 based) with my web server in the main thread and timers to collect or drive data on digital pins?
Yes as long as you don't do anything that blocks the main thread.

I would avoid implementing a web server on ESP8266. There are simpler options such as MQTT (requires a broker) or WebSockets (requires a B4J server, can run on RPi) or raw sockets.
 
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
I agree, not a web server in the traditional sense, just something that accepts limited connections from a single device (vs. say from a browser that would make multiple concurrent requests that causes wierd behavior with esp8266). As long as I'm good about just making a single connection at a time, all is well. I assume this is what you were referring to?
 
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
Thank You Erel! I'm currently using a .net program for sending commands via tcp/ip (vs a browser) but for my overall home control, I think I will look at using a RPI and B4J for the overall control.
 
Upvote 0
Top