2021年11月21日 星期日

B4R Object arrays 用法

 B4R  Object arrays 用法




'Initialize the buttons and add the StateChange event
'Analog pins A4, A5
'Analog pins A0 = 14, A1 = 15, A2 = 16, A3 = 17, A4 = 18, A5 = 19
'in our case (i + 18), 18 and 19, therefore A4 and A5 
For i = 0 To NbButtons - 1
'Using the internal pull up resistor to prevent the pin from floating.
pinButtons(i).Initialize( i + 18, pinButtons(i).MODE_INPUT_PULLUP)
pinButtons(i).AddListener("pinButtons_StateChanged")
Next

'Initialize the LEDs
'Digital pins 7, 8, 9, 10
For i = 0 To NbLEDs - 1
pinLEDs(i).Initialize(i + 7, pinLEDs(i).MODE_OUTPUT)
Next


===========================================

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

Public NbLEDs = 4 As Int 'Number of Leds
Public pinLEDs(4) As Pin 'Pins for the Leds
Public NbButtons = 2 As Int 'Number of buttons
Public pinButtons(2) As Pin 'Pins for the buttons

Public TimerLEDs As Timer
Public LightON = False As Boolean 'lights ON or OFF, ON = True
Public LEDIndex = 0 As Int 'index of current LED
Public AllLedsON = False As Boolean 'lighting type 
Private BounceTime(2) As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Private i As Int

Serial1.Initialize(115200)
Log("APP Starting.....")
TimerLEDs.Initialize("TimerLEDs_Tick", 500)

'Initialize the buttons and add the StateChange event
'Analog pins A4, A5
'Analog pins A0 = 14, A1 = 15, A2 = 16, A3 = 17, A4 = 18, A5 = 19
'in our case (i + 18), 18 and 19, therefore A4 and A5 
For i = 0 To NbButtons - 1
'Using the internal pull up resistor to prevent the pin from floating.
pinButtons(i).Initialize( i + 18, pinButtons(i).MODE_INPUT_PULLUP)
pinButtons(i).AddListener("pinButtons_StateChanged")
Next

'Initialize the LEDs
'Digital pins 7, 8, 9, 10
For i = 0 To NbLEDs - 1
pinLEDs(i).Initialize(i + 7, pinLEDs(i).MODE_OUTPUT)
Next
End Sub
Private Sub pinButtons_StateChanged (State As Boolean)
Private p As Pin

Log("State: ", State) 'Log the State value
p = Sender 'get the Pin which raised the event
Log("Pin Number: ", p.PinNumber) 'Log the Pin Number
Select p.PinNumber
Case 18 'Lights ON / OFF
If State = False Then 'if State = False, button down
If Millis - BounceTime(0) < BounceDelay Then
Return
Else
AllLedsON = Not(AllLedsON) 'change the state of AllLedsON
BounceTime(0) = Millis
If LightON = True Then 'if LightOn = False
For i = 0 To NbLEDs - 1
'switch OFF or ON all LEDs depending on AllLedsON
pinLEDs(i).DigitalWrite(AllLedsON) 
Next
'switch ON or OFF the current LED depending on AllLedsON
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON))
End If
End If
End If
Case 19 'Light type
If State = False Then 'if State = False, button down
If Millis - BounceTime(1) < BounceDelay Then
Return
Else
LightON = Not(LightON) 'change the value of LightON
Log("Light: ", LightON) 'Log the LightOn value
BounceTime(1) = Millis
TimerLEDs.Enabled = LightON 'enable or disable TimerLEDs Timer

If LightON = False Then 'if LightOn = False, lights OFF
For i = 0 To NbLEDs - 1
pinLEDs(i).DigitalWrite(False) 'switch OFF all LEDs
Next
Else
For i = 0 To NbLEDs - 1
pinLEDs(i).DigitalWrite(AllLedsON) 'switch OFF or ON all LEDs
Next
'switch ON or OFF the current LED
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON))
End If
End If
End If
End Select
End Sub
Private Sub TimerLEDs_Tick
pinLEDs(LEDIndex).DigitalWrite(AllLedsON) 'switch OFF or ON the current LED
'increment LedIndex by 1 and set it to 0 if LEDIndex = NbLEDs
LEDIndex = (LEDIndex + 1) Mod NbLEDs
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON)) 'switch ON or OFF the current LED
End Sub


ESP32-WROOM-32





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

Public NbLEDs = 4 As Int 'Number of Leds
Public pinLEDs(4) As Pin 'Pins for the Leds
Public NbButtons = 2 As Int 'Number of buttons
Public pinButtons(2) As Pin 'Pins for the buttons

Public TimerLEDs As Timer
Public LightON = False As Boolean 'lights ON or OFF, ON = True
Public LEDIndex = 0 As Int 'index of current LED
Public AllLedsON = False As Boolean 'lighting type 
Private BounceTime(2) As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Private i As Int

Serial1.Initialize(115200)
TimerLEDs.Initialize("TimerLEDs_Tick", 500)

'Initialize the buttons and add the StateChange event
'Pushbottom GPIO22, GPIO23
'LED out GPIO15 ,16 ,17 18  

For i = 0 To NbButtons - 1
'Using the internal pull up resistor to prevent the pin from floating.
pinButtons(i).Initialize( i + 22 , pinButtons(i).MODE_INPUT_PULLUP)
pinButtons(i).AddListener("pinButtons_StateChanged")
Next

'Initialize the LEDs
'Digital pins 15 ,16 ,17 18
For i = 0 To NbLEDs - 1
pinLEDs(i).Initialize(i + 15, pinLEDs(i).MODE_OUTPUT)
Next
End Sub
Private Sub pinButtons_StateChanged (State As Boolean)
Private p As Pin

Log("State: ", State) 'Log the State value
p = Sender 'get the Pin which raised the event
Log("Pin Number: ", p.PinNumber) 'Log the Pin Number
Select p.PinNumber
Case 22 'Lights ON / OFF
If State = False Then 'if State = False, button down
If Millis - BounceTime(0) < BounceDelay Then
Return
Else
AllLedsON = Not(AllLedsON) 'change the state of AllLedsON
BounceTime(0) = Millis
If LightON = True Then 'if LightOn = False
For i = 0 To NbLEDs - 1
'switch OFF or ON all LEDs depending on AllLedsON
pinLEDs(i).DigitalWrite(AllLedsON) 
Next
'switch ON or OFF the current LED depending on AllLedsON
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON))
End If
End If
End If
Case 23 'Light type
If State = False Then 'if State = False, button down
If Millis - BounceTime(1) < BounceDelay Then
Return
Else
LightON = Not(LightON) 'change the value of LightON
Log("Light: ", LightON) 'Log the LightOn value
BounceTime(1) = Millis
TimerLEDs.Enabled = LightON 'enable or disable TimerLEDs Timer

If LightON = False Then 'if LightOn = False, lights OFF
For i = 0 To NbLEDs - 1
pinLEDs(i).DigitalWrite(False) 'switch OFF all LEDs
Next
Else
For i = 0 To NbLEDs - 1
pinLEDs(i).DigitalWrite(AllLedsON) 'switch OFF or ON all LEDs
Next
'switch ON or OFF the current LED
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON))
End If
End If
End If
End Select
End Sub
Private Sub TimerLEDs_Tick
pinLEDs(LEDIndex).DigitalWrite(AllLedsON) 'switch OFF or ON the current LED
'increment LedIndex by 1 and set it to 0 if LEDIndex = NbLEDs
LEDIndex = (LEDIndex + 1) Mod NbLEDs
pinLEDs(LEDIndex).DigitalWrite(Not(AllLedsON)) 'switch ON or OFF the current LED
End Sub


沒有留言:

張貼留言

Messaging API作為替代方案

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