2021年11月19日 星期五

B4R 直流馬達控制 (Arduino UNO)

 B4R 直流馬達控制 (Arduino UNO)

 (ESP32 無法使用AnalogWrite指令)






#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
Public Serial1 As Serial
Private pinButton, pinPot, pinMotor As Pin
Private Reading = False As Boolean
Private Timer1 As Timer
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")
pinMotor.Initialize(10, pinMotor.MODE_OUTPUT)

Timer1.Initialize("Timer1_Tick", 200)
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
Reading = Not(Reading)
BounceTime = Millis
Timer1.Enabled = Reading
Log("Reading: ", Reading)
If Reading = False Then
pinMotor.AnalogWrite(0)
End If
End If
End If
End Sub
Private Sub Timer1_Tick
Private Value As UInt
Value = pinPot.AnalogRead
Log("Value = ", Value)
pinMotor.AnalogWrite(Value / 4)
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...