2021年11月21日 星期日

B4R 4Bit 16x2 LCD 顯示器

 B4R 4Bit 16x2 LCD 顯示器










#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
Public Serial1 As Serial

Public pinButton As Pin 'pin for the button
Public pinLEDGreen, pinLEDYellow, pinLEDRed As Pin 'pins for the Leds
Public lcdDisplay As LiquidCrystal
Public TimerGreenRed, TimerYellow As Timer
Public LightOn = False As Boolean
Public LightGreen = False As Boolean
Private BounceTime As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
TimerGreenRed.Initialize("TimerGreenRed_Tick", 2000)
TimerYellow.Initialize("TimerYellow_Tick", 500)

pinButton.Initialize(pinButton.A5, pinButton.MODE_INPUT_PULLUP) 'Using the internal pull up resistor to prevent the pin from floating.
pinButton.AddListener("pinButton_StateChanged")
pinLEDGreen.Initialize(7, pinLEDGreen.MODE_OUTPUT)
pinLEDYellow.Initialize(8, pinLEDYellow.MODE_OUTPUT)
pinLEDRed.Initialize(9, pinLEDRed.MODE_OUTPUT)

'initialize the display(RS As Byte, RW As Byte, Enable As Byte
'RS pin > Arduino digital pin 12
'RW pin > 255 means mot used 
'Enable pin > Arduino digital pin 11 
'DataPins: Arduino digital pins 5, 4, 3, 2 
lcdDisplay.Initialize(12, 255, 11, Array As Byte(5, 4, 3, 2))
lcdDisplay.Begin(16, 2) 'set the display type 2 * 16 characters
lcdDisplay.Write("Light OFF") 'write "Light OFF" in the first line 
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
Log("Light: ", LightOn) 'Log the LightOn value
BounceTime = Millis
lcdDisplay.Clear 'clear the display
If LightOn = True Then
lcdDisplay.Write("Light ON") 'write "Light ON" in the first line 
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("Red") 'write "red" in the second line
Else 'if State = True
lcdDisplay.Write("Light OFF") 'write "Light OFF" in the first line
End If
End If
End If
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 Sub
Private Sub TimerGreenRed_Tick
If LightGreen = True Then 'if LightGreen = True
Log("TimerGreenRed_Tick LightYellow") 'write the Log
lcdDisplay.SetCursor(0, 1) 'position the cusor at the begin of second line
lcdDisplay.Write("yellow") 'write yellow on the display
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 'if LightGreen = False
Log("TimerGreenRed_Tick LightGreen")'write the Log
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("green ") 'write "green " in the second line
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(" ")
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("red   ") 'write "red   " in the second line
pinLEDYellow.DigitalWrite(False) 'switch OFF LED Yellow
pinLEDRed.DigitalWrite(True) 'switch ON LED Red
TimerYellow.Enabled = False 'disable Timer TimerYellow
End Sub

[ESP32-WROOM-32]





#Region Project Notes
'B4R HowTo Project: Light Dimmer (ESP32-WROOM-32)
'
'Connection (looking at the blue sensor from right to left)
 ' pinLEDGreen-------------------- GPIO19 
 ' pinLEDYellow------------------- GPIO18
 ' pinLEDRed---------------------- GPIO5
 
 ' LCD 4bit mode RS--------------- GPIO13
 ' LCD 4bit mode EN--------------- GPIO12
 ' LCD 4bit mode D4--------------  GPIO14
 ' LCD 4bit mode D5--------------  GPIO27
 ' LCD 4bit mode D6--------------  GPIO26
 ' LCD 4bit mode D7--------------  GPIO25
'  
#End Region
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Public Serial1 As Serial

Public pinButton As Pin 'pin for the button
Public pinLEDGreen, pinLEDYellow, pinLEDRed As Pin 'pins for the Leds
Public lcdDisplay As LiquidCrystal
Public TimerGreenRed, TimerYellow As Timer
Public LightOn = False As Boolean
Public LightGreen = False As Boolean
Private BounceTime As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
TimerGreenRed.Initialize("TimerGreenRed_Tick", 2000)
TimerYellow.Initialize("TimerYellow_Tick", 500)

pinButton.Initialize(23, pinButton.MODE_INPUT_PULLUP) 'Using the internal pull up resistor to prevent the pin from floating.
pinButton.AddListener("pinButton_StateChanged")
pinLEDGreen.Initialize(19, pinLEDGreen.MODE_OUTPUT)
pinLEDYellow.Initialize(18, pinLEDYellow.MODE_OUTPUT)
pinLEDRed.Initialize(5, pinLEDRed.MODE_OUTPUT)

'initialize the display(RS As Byte, RW As Byte, Enable As Byte
'RS pin > Arduino digital pin 12
'RW pin > 255 means mot used 
'Enable pin > Arduino digital pin 11 
'DataPins: Arduino digital pins 5, 4, 3, 2 
'lcdDisplay.Initialize(12, 255, 11, Array As Byte(5, 4, 3, 2))

lcdDisplay.Initialize(13, 255, 12, Array As Byte(14, 27, 26, 25))

lcdDisplay.Begin(16, 2) 'set the display type 2 * 16 characters
lcdDisplay.Write("Light OFF") 'write "Light OFF" in the first line 
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
Log("Light: ", LightOn) 'Log the LightOn value
BounceTime = Millis
lcdDisplay.Clear 'clear the display
If LightOn = True Then
lcdDisplay.Write("Light ON") 'write "Light ON" in the first line 
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("Red") 'write "red" in the second line
Else 'if State = True
lcdDisplay.Write("Light OFF") 'write "Light OFF" in the first line
End If
End If
End If
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 Sub
Private Sub TimerGreenRed_Tick
If LightGreen = True Then 'if LightGreen = True
Log("TimerGreenRed_Tick LightYellow") 'write the Log
lcdDisplay.SetCursor(0, 1) 'position the cusor at the begin of second line
lcdDisplay.Write("yellow") 'write yellow on the display
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 'if LightGreen = False
Log("TimerGreenRed_Tick LightGreen")'write the Log
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("green ") 'write "green " in the second line
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(" ")
lcdDisplay.SetCursor(0, 1) 'set the cursor at the begin of second line
lcdDisplay.Write("red   ") 'write "red   " in the second line
pinLEDYellow.DigitalWrite(False) 'switch OFF LED Yellow
pinLEDRed.DigitalWrite(True) 'switch ON LED Red
TimerYellow.Enabled = False 'disable Timer TimerYellow
End Sub


沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...