B4R Tutorial Blink Example

Discussion in 'B4R Tutorials' started by Erel, Apr 11, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    A simple example that uses a Timer to change the state of pin 13 (this is the pin connected to the built-in led):
    Code:
    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
     
  2. tufanv

    tufanv Well-Known Member Licensed User

    this is the b4r version of arduino's blink example right ?
     
  3. Erel

    Erel Administrator Staff Member Licensed 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.
     
  4. Toley

    Toley Active Member Licensed User

    Do B4R have a delay(ms) function?
     
  5. Beja

    Beja Expert Licensed User

    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.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Serdar K., jcohedman and Cableguy like this.
  7. demasi

    demasi Active Member Licensed 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
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    demasi likes this.
  9. demasi

    demasi Active Member Licensed User

    It works ok now. Thank you!
     
Loading...