Interactive simulation created with Visual Basic and VB.NET
This traffic light simulator works like actual traffic lights and can be easily programmed using Visual Basic. The simulator demonstrates fundamental programming concepts like timers, conditional logic, and state management. This interactive version lets you experience how the traffic light works and see the corresponding Visual Basic code.
Traffic lights are a perfect example to demonstrate timer-based programming in Visual Basic. The program uses a timer to cycle through three states: green, yellow, and red. Each light stays on for a predetermined time before transitioning to the next state.
The traffic light simulator works by cycling through three states using a timer:
Indicates that traffic can proceed. This state lasts for 5 seconds.
Warns that the light is about to change to red. This state lasts for 2 seconds.
Indicates that traffic must stop. This state lasts for 5 seconds.
The program uses a timer to control the transitions between these states. The timer triggers an event at regular intervals, and in the event handler, the program checks the current state and switches to the next state.
Below is the Visual Basic code for the traffic light simulator. We've provided both VB6 and VB.NET implementations:
' VB6 Traffic Light Simulator Private Sub Timer1_Timer() ' Check current state and transition to next If Shape1.Visible Then ' Green is visible ' Transition to yellow Shape2.Visible = True Shape1.Visible = False Shape3.Visible = False ElseIf Shape2.Visible Then ' Yellow is visible ' Transition to red Shape3.Visible = True Shape2.Visible = False Shape1.Visible = False Else ' Red is visible ' Transition to green Shape1.Visible = True Shape2.Visible = False Shape3.Visible = False End If End Sub Private Sub Form_Load() ' Initialize the traffic light Shape1.Visible = True ' Green Shape2.Visible = False Shape3.Visible = False ' Set timer interval to 5000ms (5 seconds) Timer1.Interval = 5000 Timer1.Enabled = True End Sub
In this VB6 implementation, we use three Shape controls (Shape1 for green, Shape2 for yellow, Shape3 for red). The timer event handler checks which light is currently visible and transitions to the next light in sequence.
' VB.NET Traffic Light Simulator Public Class TrafficLightForm Private currentState As Integer = 0 ' 0=Green, 1=Yellow, 2=Red Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick ' Turn off all lights GreenLight.Visible = False YellowLight.Visible = False RedLight.Visible = False ' Determine next state Select Case currentState Case 0 ' Green -> Yellow YellowLight.Visible = True currentState = 1 Timer1.Interval = 2000 ' 2 seconds for yellow Case 1 ' Yellow -> Red RedLight.Visible = True currentState = 2 Timer1.Interval = 5000 ' 5 seconds for red Case 2 ' Red -> Green GreenLight.Visible = True currentState = 0 Timer1.Interval = 5000 ' 5 seconds for green End Select End Sub Private Sub TrafficLightForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Initialize the traffic light GreenLight.Visible = True YellowLight.Visible = False RedLight.Visible = False ' Start timer Timer1.Interval = 5000 Timer1.Start() End Sub End Class
In this VB.NET implementation, we use a state variable to track the current light state. The timer event handler uses a Select Case statement to determine the next state, making the code more maintainable and easier to extend.
Watch how the traffic light simulator works in a real Visual Basic application.
Want to master Visual Basic programming with practical examples like this traffic light simulator? Our book Visual Basic Programming with Code Examples covers both VB6 and VB.NET with dozens of real-world projects.
You'll learn how to create: