from machine import Pin,PWM
motor1_pwm = PWM(Pin(21))
motor2_pwm = PWM(Pin(22))
motor1_pwm.freq(15000)
motor2_pwm.freq(15000)
motor1_pwm.duty(0)
motor2_pwm.duty(0)
switch_MoveForward = Pin(25, Pin.IN, Pin.PULL_UP)
switch_TurnLeft = Pin(26, Pin.IN, Pin.PULL_UP)
switch_TurnRight = Pin(27, Pin.IN, Pin.PULL_UP)
def forward(speed):
duty = int(speed * 10.23)
motor1_pwm.duty(duty)
motor2_pwm.duty(duty)
def left(speed):
duty = int(speed * 10.23)
motor1_pwm.duty(0)
motor2_pwm.duty(duty)
def right(speed):
duty = int(speed * 10.23)
motor1_pwm.duty(duty)
motor2_pwm.duty(0)
def stop():
motor1_pwm.duty(0)
motor2_pwm.duty(0)
while True:
if not switch_MoveForward.value():
forward(50)
elif not switch_TurnLeft.value():
left(50)
elif not switch_TurnRight.value():
right(50)
else:
stop()