Microsoft Small Basic

Program Listing: DHK287
' ************************************************************************************
' * Traffic Light v0.1.sb 2014-09-07 *
' * Author: Luis Fonseca MS Small Basic v1.0 *
' ************************************************************************************
' * Description:
' * Simulate the trafic light process.
' * This project is designed for the "Traffic Light Challenge" on MS Small basic blog of
' * september 2014.
' *
' * Note: Need internet access for the background picture and the picture disponibility
' * is not garanted.
' ************************************************************************************


' ----- Settings -------------------------------------------------------------------------------------------
BackgroundPicture = "http://social.msdn.microsoft.com/Forums/getfile/528272"
LightRadius = 60
LightCenter_X = 59
RedLightCenter_Y = 42
CenterLightSeparation = 63
RedWaitTime = 10000
YellowWaitTime = 4000
GreenWaitTime = 7000
' ------------------------------------------------------------------------------------------- Settings -----


' *************************************************************************************
' *********************************** Main Program **********************************

Initiate()

ReStart:

SleepMode()
WorkMode()

Goto ReStart


' ************************************************************************************
' ********************************** Sub Programs ***********************************

' ==== Initiate() ===================================================
Sub initiate
' Insert background picture
Shapes.AddImage(BackgroundPicture)

' Create Shapes of lights
GraphicsWindow.BrushColor = "Black"
MaskRedLight = Shapes.AddEllipse(LightRadius, LightRadius)
MaskYellowLight = Shapes.AddEllipse(LightRadius, LightRadius)
MaskGreenLight = Shapes.AddEllipse(LightRadius, LightRadius)

' Place shapes of lights on the background picture
XPos = LightCenter_X - LightRadius/2
YRedPos = RedLightCenter_Y - LightRadius/2
Shapes.Move(MaskRedLight, XPos, YRedPos)
Shapes.Move(MaskYellowLight, XPos, YRedPos + CenterLightSeparation)
Shapes.Move(MaskGreenLight, XPos, YRedPos + CenterLightSeparation*2)

' Create control buttons
Controls.AddButton("Quit", 260, 20)
Controls.AddButton("Wake up", 160, 20)
Controls.AddButton("Sleep", 180, 20)
Controls.HideControl("Button3")

Controls.ButtonClicked = ButtonProcess
EndSub
' =================================================== Initiate() ====


' ==== ButtonProcess() ==============================================
Sub ButtonProcess
If Controls.GetButtonCaption(Controls.LastClickedButton) = "Quit" Then
Program.End()
EndIf

If (Controls.LastClickedButton = "Button2") Then
SleepMode = "false"
Controls.HideControl("Button2")
EndIf

If (Controls.LastClickedButton = "Button3") Then
WorkMode = "false"
Controls.HideControl("Button3")
EndIf
EndSub
' ============================================== ButtonProcess() ====


' ==== SleepMode() ================================================
' Wait in sleep Mode by flashing yellow when the trafic Light is out of service.

Sub SleepMode
SleepMode = "true"
Controls.ShowControl("Button2")

While SleepMode = "true"
Shapes.HideShape(MaskYellowLight)
Program.Delay(1000)
Shapes.ShowShape(MaskYellowLight)
Program.Delay(1000)
EndWhile

Shapes.HideShape(MaskYellowLight)

' Yellow wait
Program.Delay(YellowWaitTime)

EndSub
' ================================================ SleepMode() ====


' ==== WorkMode() ================================================
Sub WorkMode
WorkMode = "true"
Controls.ShowControl("Button3")

' Change from Yellow to Red
Shapes.ShowShape(MaskYellowLight)
Shapes.HideShape(MaskRedLight)

' Red wait
Program.Delay(RedWaitTime)

While WorkMode = "true"

' Change from Red to Green
Shapes.ShowShape(MaskRedLight)
Shapes.HideShape(MaskGreenLight)

' Green wait
Program.Delay(GreenWaitTime)

' Change from Green to Yellow
Shapes.ShowShape(MaskGreenLight)
Shapes.HideShape(MaskYellowLight)

' Yellow wait
Program.Delay(YellowWaitTime)

' Change from Yellow to Red
Shapes.ShowShape(MaskYellowLight)
Shapes.HideShape(MaskRedLight)

' Red wait
Program.Delay(RedWaitTime)
EndWhile

' Cut red light
Shapes.ShowShape(MaskRedLight)

EndSub
' ================================================ WorkMode() ====