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


沒有留言:

張貼留言

Messaging API作為替代方案

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