Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Write your code here :-)
- import board
- import pulseio
- from digitalio import DigitalInOut, Direction
- import adafruit_hcsr04
- import time
- sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D12, echo_pin=board.D11)
- EN1_PIN = board.D6
- EN2_PIN = board.D5
- IN1_PIN = board.D7
- IN2_PIN = board.D4
- en1 = None
- en2 = None
- in1 = None
- in2 = None
- uart = None
- def motor_1(pwm, reverse=False):
- global en1
- en1.duty_cycle = pwm * 255
- if reverse:
- in1.value = True
- else:
- in1.value = False
- def motor_2(pwm, reverse=False):
- global en1
- en2.duty_cycle = pwm * 255
- if reverse:
- in2.value = True
- else:
- in2.value = False
- def setup():
- global en1
- global en2
- global in1
- global in2
- en1 = pulseio.PWMOut(EN1_PIN, frequency=5000, duty_cycle=0)
- en2 = pulseio.PWMOut(EN2_PIN, frequency=500, duty_cycle=0)
- in1 = DigitalInOut(IN1_PIN)
- in1.direction = Direction.OUTPUT
- in2 = DigitalInOut(IN2_PIN)
- in2.direction = Direction.OUTPUT
- setup()
- while True:
- motor_1(255)
- motor_2(255)
- distance = 0
- try:
- distance = sonar.distance
- except RuntimeError:
- print("Retrying!")
- print((distance,))
- if distance > 0 and distance < 8:
- motor_1(0)
- motor_2(0)
- time.sleep(0.2)
- motor_1(255, reverse=True)
- motor_2(255, reverse=True)
- time.sleep(0.8)
- motor_1(255, reverse=False)
- motor_2(255, reverse=True)
- time.sleep(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement