2021年11月19日 星期五

B4R 伺服馬達控制

 B4R 伺服馬達控制

所有examples序列埠(Serial1  port)

原來bit rate 115200均需改成9600

Serial1.Initialize(9600)

Log("AppStart")







#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 Angle = 0 As UInt

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


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)

pinMotor.AnalogWrite(128)

End If

End If

End Sub


Private Sub Timer1_Tick

Private Value As UInt

Value = pinPot.AnalogRead

' Angle = MapRange(Value, 0, 1023, 0, 180)

' Angle = MapRange(Value, 0, 1023, 0, 255)

Angle = MapRange(Value, 0, 1023, 40, 225)

Log("Angle = ", Angle)

pinMotor.AnalogWrite(Angle)

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...