B4J Tutorial [IoT] Traffic Light with Raspberry Pi

Discussion in 'B4J Tutorials' started by Erel, Sep 2, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This is a simple example of a traffic light implementation using Raspberry Pi 2.

    The red, yellow and green leds are connected to pins 27, 28 and 29 (see the numbering scheme: http://pi4j.com/pins/model-b-plus.html).


    Using a timer we switch between four different states:

    The complete code:
    Sub Process_Globals
    Type State (Duration As Int, Red As Boolean, Yellow As Boolean, Green As Boolean)
    Private timer1 As Timer
    Private controller As GpioController
    Private pinRed, pinYellow, pinGreen As GpioPinDigitalOutput
    Private states As List
    Private currentStateIndex As Int
    End Sub

    Sub AppStart (Args() As String)
    4000TrueFalseFalse)) 'only red
       states.Add(CreateState(2000TrueTrueFalse)) 'red + yellow
       states.Add(CreateState(4000FalseFalseTrue)) 'only green
       states.Add(CreateState(2000FalseTrueFalse)) 'only yellow
       timer1.Enabled = 
       currentStateIndex = -
    1 'it will be incremented in Timer1_Tick
       Timer1_Tick 'start with the first state
    End Sub

    Private Sub Timer1_Tick
       currentStateIndex = (currentStateIndex + 
    1mod states.Size
    Dim st As State = states.Get(currentStateIndex)
       pinRed.State = st.Red
       pinYellow.State = st.Yellow
       pinGreen.State = st.Green
       timer1.Interval = st.Duration
    End Sub

    Private Sub CreateState(Duration As Int, Red As Boolean, Yellow As Boolean, Green As Boolean) As State
    Dim st As State
       st.Duration = Duration
       st.Red = Red
       st.Yellow = Yellow
       st.Green = Green
    Return st
    End Sub
    It depends on jPi4J v1.5+: http://www.b4x.com/android/forum/threads/jpi4j-raspberry-pi-gpio-controller.37493
    Mark Turney, johndb, rwblinn and 4 others like this.
  2. sorex

    sorex Expert Licensed User

    interesting that you have 4 states.

    In Belgium (and Holland?) it's just green, orange, red

    And I believe in Luxemburg it's green, green+orange, red

    so another combo than what you have there with green, orange, red, red+orange.
  3. Erel

    Erel Administrator Staff Member Licensed User

    I guess that the purpose of the second state (red + yellow) is to allow drivers to distinguish between the transition to red and the transition to green.
  4. mariusz

    mariusz New Member Licensed User

    Yes, you are right. In Poland we have also 4 states. The transitions are: red --> red+yellow --> green--> yellow --> red. Such approach allows to distinguish beetween red to green vs. green to red transitions. Additionally for pedestrians we have only red and green lights and the transitions are: red --> green --> blinking green (meaning "yellow") --> red.
  5. Beja

    Beja Expert Licensed User

    Thanks Erel,
    How the whole system is interfaced? is it connected to a computer?
  6. Erel

    Erel Administrator Staff Member Licensed User

    It is a raspberry pi computer.
    Beja likes this.
  7. giggetto71

    giggetto71 Member Licensed User

    forgive the stupid question. I am new to raspberry Pi world programming. I would like to test your trafic light example but I guess I missed something very basic. I understood you need
    Pi4J library on android device side (I was able to install the library on my pc where i have the B4A ide). What I don't get is what I need on raspberry PI side. Can you help me to understand? For example in the example code I did not find any place where you set the address of the PI device. thanks for your help.
  8. Erel

    Erel Administrator Staff Member Licensed User

  9. derez

    derez Expert Licensed User

    Erel likes this.
  10. derez

    derez Expert Licensed User

    I am trying to put the above two lines into a script which is run at startup (to enable running the application automatically) but it does not work.
    They are required to create/provide access to the gpio_sw folder.
    Does anybody know of a solution ?
  11. Roycefer

    Roycefer Well-Known Member Licensed User

    If my recollection serves me, you can't call "sudo" in a script unless the script itself already has superuser privileges. That is, the script had to have been called with "sudo" if you want anything inside the script to be able to call "sudo". This makes for a chicken-and-the-egg problem if you want sudo stuff to run automatically on start up, without user intervention.
  12. Erel

    Erel Administrator Staff Member Licensed User

    Have you tried it without the "sudo"?
  13. derez

    derez Expert Licensed User

    The attached program demostrates the lights on orangepi. It includes also a button which holds the change of lights (so the user can manually give more green if needed...).
    There is a small class to handle the pins.
    EDIT: see two posts down for an updated class.

    Yes to no avail...

    Attached Files:

    Last edited: Jan 30, 2016
    Erel likes this.
  14. derez

    derez Expert Licensed User

    Found the solution !
    The command modprobe does not run in a script, but it controls the loaded modules. in /etc/modules there is a list of the modules that load at start.
    I added "gpio-sunxi" to the list of modules and gpio works without any more commands after strat.

    Of course, there is a reason not to load it for use of the board which does not need gpio, so do the change only for gpio use.
    Erel likes this.
  15. derez

    derez Expert Licensed User

    An updated GPIO class for OrangePi, including Pulse Width Modulation:
    Edit: Found out that the READ setting is good only to read a previous set of state, not for real sensing a pin with voltage.
    'Class module
    Sub Class_Globals
    Public WRITE As String = "1"
    Public READ As String = "0"
    Private pname As String
    Private dir As String = "/sys/class/gpio_sw/"
    Private t1,t2 As Int
    Private dt As Timer
    End Sub

    'Initializes the object.
    'Pin string should match a sub folder of /sys/class/gpio_sw/
    'Put function as WRITE for writing to and READ for reading the pin.
    Public Sub Initialize(pin As String, function As String)
        pname = pin
    File.WriteString(dir & pname ,"cfg",function)
    End Sub

    public Sub setState(pin_on As Boolean)
    If pin_on Then
    File.WriteString(dir & pname ,"data","1")
    File.WriteString(dir & pname ,"data","0")
    End If
    End Sub

    public Sub getState  As Boolean
    Dim st As String = File.readString(dir & pname ,"data")
    If st.CharAt(0) = "1" Then
    Return True
    Return False
    End If
    End Sub

    public Sub PWMOn(timeon As Int, timeoff As Int)
        t1 = timeon
        t2 = timeoff

        dt.Enabled = 
    End Sub

    public Sub PWMOff
        dt.Enabled = 
    End Sub

    Private Sub dt_Tick
    If dt.Interval = t1 Then
         dt.Interval = t2
        dt.Interval = t1
    End If
    End Sub
    Note: using short times like few milisec with debug mode doesn't produce correct results because of the debug additional time-load, but it works in release mode.
    Note: PWM can be used to match the intensity of color leds.
    Last edited: Apr 18, 2016
    Laurent95 and Erel like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice