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產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...