2021年11月20日 星期六

B4X Pulse Period Modulation 脈波週期調變(Adruino UNO , ESP32-WROOM-32)

 B4X Pulse Period Modulation脈波週期調變 (Adruino UNO , ESP32-WROOM-32)

               






#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
Public Serial1 As Serial
Private pinButton, pinPot, pinLED As Pin
Private StateON_OFF = False As Boolean 'state ON or OFF
Private MaxPulsePeriod = 2000 As ULong 'Max PulsePeriod value 
Private PulseWidth = 200 As ULong 'remains constant
Private PulsePeriod As ULong 'calculated in TimerPulse_Tick
Private BounceTime As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Serial1.Initialize(9600)
Log("AppStart")
'Using the internal pull up resistor to prevent the pin from floating.
pinButton.Initialize(pinButton.A5, pinButton.MODE_INPUT_PULLUP) 
pinButton.AddListener("pinButton_StateChanged")
pinLED.Initialize(10, pinLED.MODE_OUTPUT)
pinPot.Initialize(pinPot.A1, pinPot.MODE_INPUT)
End Sub
Private Sub pinButton_StateChanged (State As Boolean)
Log("state: ", State)
'state will be False when the button is clicked because of the PULLUP mode.
If State = False Then
If Millis - BounceTime < BounceDelay Then
Return
Else
StateON_OFF = Not(StateON_OFF)
Log("SateON_OFF: ", StateON_OFF)
BounceTime = Millis
If StateON_OFF = True Then
NextPulse(0)
Else
pinLED.DigitalWrite(False)
End If
End If
End If
End Sub
Private Sub NextPulse(Tag As Byte)
If StateON_OFF = True Then
PulsePeriod = MapRange(pinPot.AnalogRead, 0, 1023, PulseWidth, MaxPulsePeriod)
If PulsePeriod >= MaxPulsePeriod - 1 Then
pinLED.DigitalWrite(False)
Else if PulsePeriod <= PulsePeriod + 1 Then
pinLED.DigitalWrite(True)
Else
pinLED.DigitalWrite(True)
CallSubPlus("EndPulse", PulseWidth, 0)
End If
CallSubPlus("EndPulse", PulseWidth, 0)
CallSubPlus("NextPulse", PulsePeriod, 0)
Log("NextPulse") 'Log for testing
End If
End Sub
Private Sub EndPulse(Tag As Byte)
pinLED.DigitalWrite(False) 'switch the LED OFF 
Log("EndPulse") 'Log for testing
End Sub










'For ESp32-WROOM-32  Pulse_Period_Modulation





#Region Project Notes
'B4R HowTo Project: Light Dimmer (ESP32-WROOM-32)
'
'Connection (looking at the blue sensor from right to left)
' pushbutton switch --------------GPIO23
' blue LED 220 Ω resistor---------GPIO12
' potentiometer-------------------GPIO13 
'  
#End Region
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 600
#End Region
Sub Process_Globals
Public Serial1 As Serial
Private pinButton, pinPot, pinLED As Pin
Private StateON_OFF = False As Boolean 'state ON or OFF
Private MaxPulsePeriod = 2000 As ULong 'Max PulsePeriod value 
Private PulseWidth = 200 As ULong 'remains constant
Private PulsePeriod As ULong 'calculated in TimerPulse_Tick
Private BounceTime As ULong
Private BounceDelay = 10 As ULong
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
'Using the internal pull up resistor to prevent the pin from floating.
pinButton.Initialize(23, pinButton.MODE_INPUT_PULLUP) 
pinButton.AddListener("pinButton_StateChanged")
pinLED.Initialize(12, pinLED.MODE_OUTPUT)
pinPot.Initialize(13, pinPot.MODE_INPUT)
End Sub
Private Sub pinButton_StateChanged (State As Boolean)
Log("state: ", State)
'state will be False when the button is clicked because of the PULLUP mode.
If State = False Then
If Millis - BounceTime < BounceDelay Then
Return
Else
StateON_OFF = Not(StateON_OFF)
Log("SateON_OFF: ", StateON_OFF)
BounceTime = Millis
If StateON_OFF = True Then
NextPulse(0)
Else
pinLED.DigitalWrite(False)
End If
End If
End If
End Sub
Private Sub NextPulse(Tag As Byte)
If StateON_OFF = True Then
PulsePeriod = MapRange(pinPot.AnalogRead, 0, 1023, PulseWidth, MaxPulsePeriod)
If PulsePeriod >= MaxPulsePeriod - 1 Then
pinLED.DigitalWrite(False)
Else if PulsePeriod <= PulsePeriod + 1 Then
pinLED.DigitalWrite(True)
Else
pinLED.DigitalWrite(True)
CallSubPlus("EndPulse", PulseWidth, 0)
End If
CallSubPlus("EndPulse", PulseWidth, 0)
CallSubPlus("NextPulse", PulsePeriod, 0)
Log("NextPulse") 'Log for testing
End If
End Sub
Private Sub EndPulse(Tag As Byte)
pinLED.DigitalWrite(False) 'switch the LED OFF 
Log("EndPulse") 'Log for testing
End Sub









沒有留言:

張貼留言

Node-Red --> MQTT --> Fuxa 開源碼網頁式圖控平台

Node-Red --> MQTT --> Fuxa      FUXA(一個開源的 Web HMI / SCADA 自動化監控軟體)的專案設定檔 。 這份設定檔完整定義了 HMI 監控畫面的 後端通訊(MQTT 連線、點位標籤) 與 前端網頁圖形介面(SVG 畫布...