2021年11月19日 星期五

B4R H Bridge L293D 馬達控制 (Arduino UNO)

B4R H Bridge L293D 馬達控制 (Arduino UNO)


 












#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region

Sub Process_Globals
Public Serial1 As Serial

Private MotorON = False As Boolean 'motor ON or OFF, ON = True
Private MotorDirection = False As Boolean 'motor direction 
Private MotorSpeed = 0 As UInt
Private TimerMotor As Timer
Private pinSwitchMotorONOFF, pinSwitchMotorDirection, pinMotorSpeed, pinMotorControl1, pinMotorControl2, pinPot As Pin
Private BounceTimeMotorON, BounceTimeMotorDirection As ULong
Private BounceDelay = 10 As ULong
End Sub

Private Sub AppStart
Serial1.Initialize(9600)
Log("AppStart")
TimerMotor.Initialize("TimerMotor_Tick", 200)
pinSwitchMotorONOFF.Initialize(pinSwitchMotorONOFF.A5, pinSwitchMotorONOFF.MODE_INPUT_PULLUP)
pinSwitchMotorONOFF.AddListener("pinSwitchMotorONOFF_StateChanged")
pinSwitchMotorDirection.Initialize(pinSwitchMotorONOFF.A3, pinSwitchMotorDirection.MODE_INPUT_PULLUP)
pinSwitchMotorDirection.AddListener("pinSwitchMotorDirection_StateChanged")
pinMotorControl1.Initialize(2, pinMotorControl1.MODE_OUTPUT)
pinMotorControl2.Initialize(3, pinMotorControl1.MODE_OUTPUT)
pinMotorSpeed.Initialize(10, pinMotorSpeed.MODE_OUTPUT)
pinPot.Initialize(pinPot.A1, pinPot.MODE_INPUT)

pinMotorControl1.DigitalWrite(MotorDirection)
pinMotorControl2.DigitalWrite(Not(MotorDirection))
End Sub

Private Sub pinSwitchMotorONOFF_StateChanged (State As Boolean)
If State = False Then
If Millis - BounceTimeMotorON < BounceDelay Then
Return
Else
MotorON = Not(MotorON)
If MotorON = True Then
TimerMotor.Enabled = MotorON
Else
pinMotorSpeed.AnalogWrite(0)
End If
BounceTimeMotorON = Millis
End If
Log("MotorON: ", MotorON)
End If
End Sub

Private Sub pinSwitchMotorDirection_StateChanged (State As Boolean)
' Log("StateON ", State)
If State = False Then
If Millis - BounceTimeMotorDirection < BounceDelay Then
Return
Else
MotorDirection = Not(MotorDirection)
pinMotorControl1.DigitalWrite(MotorDirection)
pinMotorControl2.DigitalWrite(Not(MotorDirection))
BounceTimeMotorDirection = Millis
End If
Log("MotorDirection: ", MotorDirection)
End If
End Sub

Private Sub TimerMotor_Tick
If MotorON = True Then
MotorSpeed = pinPot.AnalogRead / 4
' Log("MotorSpeed: ", MotorSpeed)
pinMotorSpeed.AnalogWrite(MotorSpeed)
End If
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...