Arduino C example #1:
While the above design is simple and works properly for very small examples, it doesn't work with larger programs as the single thread cannot do anything else while it sleeps.
The B4R solution is to use a Timer instead. There could be many timers running and they will all work without interfering each other.
Blink program with a timer:
CallSubPlus is similar to a timer that fires once. CallSubPlus allows you to run a sub after a specified duration. Like a timer it doesn't block the thread.
Blink example based on CallSubPlus:
The sub signature must be the same as the subs above.
Note that CallSubPlus was recently added to other B4X tools with CallSubUtils class: https://www.b4x.com/android/forum/threads/60877/#content
Internally the single thread runs in a loop and manages a messages queue.
You can use AddLooper keyword to set a sub that will be called on each internal loop. This is similar to a timer that runs as fast as possible.
Note that you can call this method multiple times to add multiple subs that will run each loop.
If you find yourself calling AddLooper and then calling Delay in the "looper" sub then you should replace it with a timer.
AddLooper example:
Arduino Uno: 19.6 loops per millisecond
Arduino MKR1000: 150 loops per millisecond
Arduino 101: 150 loops per millisecond
Arduino Due: 415 loops per millisecond.
ESP8266: 500 loops per milliseconds.
ESP32: 1750 loops per millisecond
B4X:
void setup() {
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
While the above design is simple and works properly for very small examples, it doesn't work with larger programs as the single thread cannot do anything else while it sleeps.
The B4R solution is to use a Timer instead. There could be many timers running and they will all work without interfering each other.
Blink program with a timer:
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private Timer1 As Timer
Private pin13 As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
pin13.Initialize(13, pin13.MODE_OUTPUT)
Timer1.Initialize("Timer1_Tick", 1000)
Timer1.Enabled = True 'don't forget to enable it
End Sub
Private Sub Timer1_Tick
pin13.DigitalWrite(Not(pin13.DigitalRead))
End Sub
CallSubPlus is similar to a timer that fires once. CallSubPlus allows you to run a sub after a specified duration. Like a timer it doesn't block the thread.
Blink example based on CallSubPlus:
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private pin13 As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
pin13.Initialize(13, pin13.MODE_OUTPUT)
LedOn(0)
End Sub
Private Sub LedOn (tag As Byte)
pin13.DigitalWrite(True)
CallSubPlus("LedOff", 1000, 0)
End Sub
Private Sub LedOff(tag As Byte)
pin13.DigitalWrite(False)
CallSubPlus("LedOn", 1000, 0)
End Sub
The sub signature must be the same as the subs above.
Note that CallSubPlus was recently added to other B4X tools with CallSubUtils class: https://www.b4x.com/android/forum/threads/60877/#content
Internally the single thread runs in a loop and manages a messages queue.
You can use AddLooper keyword to set a sub that will be called on each internal loop. This is similar to a timer that runs as fast as possible.
Note that you can call this method multiple times to add multiple subs that will run each loop.
If you find yourself calling AddLooper and then calling Delay in the "looper" sub then you should replace it with a timer.
AddLooper example:
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private counter As ULong
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
AddLooper("Looper1")
End Sub
Sub Looper1
counter = counter + 1
If counter mod 100000 = 0 Then
Log("Took me ", Millis, " milliseconds to count to ", counter)
Log(counter / Millis, " loops per milliseconds")
End If
End Sub
Arduino Uno: 19.6 loops per millisecond
Arduino MKR1000: 150 loops per millisecond
Arduino 101: 150 loops per millisecond
Arduino Due: 415 loops per millisecond.
ESP8266: 500 loops per milliseconds.
ESP32: 1750 loops per millisecond
Last edited: