B4R Tutorial Traffic Light Example


Staff member
Licensed User
Example of implementing a "traffic light" with 3 leds. There are four states: red, red + yellow, green and yellow.

It uses CallSubPlus to switch to the next state after the current state duration.

Sub Process_Globals
   Public Serial1 As Serial
   Type State (Red As Boolean, Yellow As Boolean, Green As Boolean, Duration As UInt)
   Private Green, Yellow, Red As Pin
   Private States(4) As State
   Private CurrentStateIndex As Int = 0
End Sub

Private Sub AppStart
   Red.Initialize(Red.A0, Red.MODE_OUTPUT)   
   Yellow.Initialize(Yellow.A1, Yellow.MODE_OUTPUT)   
   Green.Initialize(Green.A2, Green.MODE_OUTPUT)   
   'Reset the pins
   For Each p As Pin In Array As Pin(Red, Yellow, Green)
   SetState(States(0), True, False, False, 1000) 'red
   SetState(States(1), True, True, False, 500) 'yellow  + red
   SetState(States(2), False, False, True, 1000) 'green
   SetState(States(3), False, True, False, 500) 'yellow
End Sub

Private Sub SetState(s As State, RedValue As Boolean, YellowValue As Boolean, GreenValue As Boolean, duration As UInt)
   s.Red = RedValue
   s.Yellow = YellowValue
   s.Green = GreenValue
   s.Duration = duration
End Sub

Private Sub RunState(unused As Byte)
   Log("CurrentStateIndex: ", CurrentStateIndex)
   Dim s As State = States(CurrentStateIndex)
   CurrentStateIndex = (CurrentStateIndex + 1) mod States.Length
   'switch to the next state after the current state duration.
   CallSubPlus("RunState", s.Duration, 0)
End Sub


Licensed User
Thanks Erel,
Tried to think about the other side lights but found it more complex. definitely not by simply inverting the LEDs because if red and green switched on at the same time then this is a guaranteed accident.
Anyone thought about this?


Well-Known Member
Licensed User
Other road junctions should always be held on red. You would need another status boolean for each set of lights to designate if the lights were changing or if they were on red. You would probably also want some method of prioritising which lights are allowed to change first i.e. major roads have priority over minor roads.


Licensed User
Got it.. thanks RandomCoder..
So the red time = total time of green + green/yellow + yellow. Then when the yellow time is up this side is red and the other side starts the same sequence as in the arduino code above without change.. that's easy.


Licensed User
Your error is related to thé DH sensor you are including, thus not related to this example!
Also keep in mind that esp boards do not use the same pin layout as an arduino!
Last edited:


Licensed User
In the B4R Beginner's Guide you also have a TraficLight example with a button to switch the lights on or off.
yes i have looked at them but the problem is that there is no sketch on how to connect everything to the board. so its impossible for me to use them.
i have tried to connect all leds like in erels sketch and run the traffic light example from the b4r booklets examples but nothing happens.

erels example runs fine.
Last edited:


Licensed User
yes i have looked at them but the problem is that there is no sketch on how to connect everything to the board.
Sorry, but I don't understand, the TraficLight project is explained in chapter 12.1 with a sketch and an explanation of the code!?



Licensed User
ohh ok now i see it. when i go to b4r on the top i see this: https://www.b4x.com/b4r/documentation.html

B4X Programming Language and IDE: (English - PDF + code)(French - PDF + code)(Spanish - PDF only)
so i downloaded the english - pdf + code zip but thats the wrong zip file. its a global b4x guide file but not for b4r (it only include 4 examples without any explaination)

but below that inside Parentheses is the b4r beginner guide. (a little bit irritating, dont you think klaus?) i think in the b4r page should the b4r beginner guide be in the first line and not in Parentheses below a download link to a global b4x guide zip. (just thinking)

thank you anyway :)


Licensed User
It would have been easier to look in my signature.
The first line: Beginner's Guides ...

The Beginner's Guides will disappear with time and be replaced by the multi platform B4x Booklets.
Currently they are still available because there are still informations in which are not yet covered by the Booklets.
But this is a long term task.
I have in my pipeline booklets for SQLite database, Graphics and a B4R User's Guide with the examples not covered in the Booklets and new examples, but unfortunately no time limit.

By the way, the four examples you are speaking of are explained in the B4x Getting Started Booklet in chapter 5.7.
Last edited: