2021年11月16日 星期二

B4R 紅綠燈

 B4R 紅綠燈



紅綠燈影片

switch ON or OFF the traffic lights with the pushbutton switch

#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src
Sub Process_Globals
Public Serial1 As Serial
Private pinButton As Pin 'pin for the button
Private pinLEDGreen, pinLEDYellow, pinLEDRed As Pin 'pins for the Leds
Private TimerGreenRed, TimerYellow As Timer
Private LightOn = False As Boolean
Private LightGreen = False As Boolean
Private BounceTime As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
TimerGreenRed.Initialize("TimerGreenRed_Tick", 2000)
TimerYellow.Initialize("TimerYellow_Tick", 500)
'Using the internal pull up resistor to prevent the pin from floating.
pinButton.Initialize(23, pinButton.MODE_INPUT_PULLUP)
pinButton.AddListener("pinButton_StateChanged")
pinLEDGreen.Initialize(12, pinLEDGreen.MODE_OUTPUT)
pinLEDYellow.Initialize(14, pinLEDYellow.MODE_OUTPUT)
pinLEDRed.Initialize(27, pinLEDRed.MODE_OUTPUT)

End Sub
Private Sub pinButton_StateChanged (State As Boolean)
Log("State: ", State)       'Log the State value
If State = False Then 'if State = False
If Millis - BounceTime < BounceDelay Then
Return
Else
pinLEDRed.DigitalWrite(True) 'switch ON the red LED
LightOn = Not(LightOn) 'change the value of LightOn
BounceTime = Millis
'   Log("Light: ", LightOn)     'Log the LightOn value
TimerGreenRed.Enabled = LightOn 'enable TimerGreenRed Timer
If LightOn = False Then 'if LightOn = False
pinLEDGreen.DigitalWrite(False) 'switch OFF LED Green
pinLEDYellow.DigitalWrite(False) 'switch OFF LED Yellow
pinLEDRed.DigitalWrite(False) 'switch OFF LED Red
End If
End If
End If
End Sub
Private Sub TimerGreenRed_Tick
If LightGreen = True Then 'if LightGreen = True
Log("TimerGreenRed_Tick LightYellow") 'write the Log
TimerYellow.Enabled = True 'enable TimerYellow
pinLEDGreen.DigitalWrite(False) 'switch OFF LED Green
pinLEDYellow.DigitalWrite(True) 'switch ON LED Yellow
LightGreen = False 'set LightGreen to False
Else
Log("TimerGreenRed_Tick LightGreen") 'write the Log
pinLEDRed.DigitalWrite(False) 'switch OFF LED Red
pinLEDGreen.DigitalWrite(True) 'switch ON LED Green
LightGreen = True 'set LightGreen to True
End If
End Sub
Private Sub TimerYellow_Tick
Log("TimerYellow_Tick LightRed") 'write the Log
Log(" ")
pinLEDYellow.DigitalWrite(False) 'switch OFF LED Yellow
pinLEDRed.DigitalWrite(True) 'switch ON LED Red
TimerYellow.Enabled = False 'disable Timer TimerYellow
End Sub



沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...