SAMENVATTING: We besturen onze servomotor en laten hem schakelen tussen links- en rechtsom draaien, bestuurd door onze knoppen.
from
machine
import
Pin, PWM
from
utime
import
sleep
# Servo pin numbers
servoOnePin
=
7
# Key pin numbers
buttonLeftPin
=
15
buttonRightPin
=
11
# Initialization of the servo
servoOne
=
PWM(Pin(servoOnePin))
# Initialize the buttons with PULL_UP to use the default HIGH state
buttonLeft
=
Pin(buttonLeftPin, Pin.IN, Pin.PULL_UP)
buttonRight
=
Pin(buttonRightPin, Pin.IN, Pin.PULL_UP)
# Servo speeds in nanoseconds
leftSpeed
=
1300000
# Moves the servo to the left
rightSpeed
=
1700000
# Moves the servo to the right
# Servo frequency
servoOne.freq(50)
# Typical servo frequency of 50Hz
# Status of the servo
servoState
=
'left'
# Starts with counterclockwise rotation
# Last state of the buttons to recognize edges
lastButtonLeft
=
buttonLeft.value()
lastButtonRight
=
buttonRight.value()
while
True:
# Read out the current status of the buttons
currentButtonLeft
=
buttonLeft.value()
currentButtonRight
=
buttonRight.value()
# Check whether an edge from HIGH to LOW was detected (button was pressed)
if
lastButtonLeft
==
1
and
servoState
=
'right'
# Changes the direction to the right when the left
button is pressed
elif
lastButtonRight
==
1
and
servoState
=
'left'
# Changes the direction to the left when the right
button is pressed
currentButtonLeft
==
0:
currentButtonRight
==
0:
Initialisatie van de servomotor en
knoppen
De knoppen controleren
23