B4R Tutorial Blink Example

A simple example that uses a Timer to change the state of pin 13 (this is the pin connected to the built-in led):
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) '1000ms = 1 second
   Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick
   Dim currentState As Boolean = pin13.DigitalRead
   Log("CurrentState: ", currentState)
   Dim NewState As Boolean = Not(currentState)
   Log("NewState: ", NewState)
   pin13.DigitalWrite(NewState)
End Sub
 

Toley

Active Member
Licensed User
Longtime User
The functionality is the same. Unlike the blink example that is based on a loop with calls to Delay, the above code doesn't pause the execution thread. So the program can do many other things while it also toggles the led.
Do B4R have a delay(ms) function?
 

Beja

Expert
Licensed User
Longtime User
Do B4R have a delay(ms) function?

The best bet is to use another timer and initialize it with the delay value you wish.. you can also modify that value at runtime
for uses in other subs or anything else. just make sure to disable it after each use and only enable it when you need to use it.
 

demasi

Active Member
Licensed User
Longtime User
Hello,
I'm trying to run this BLINK example in my Arduino Mega 2560, but it doesn't load.
I can load and rur normally with Arduino IDE with same model/port configurations.
I'm using the most recent B4R.
I got this error:

2016-10-13 21 34 22.png
 
Top