I have a code currently running on a pyboard and is being used with a motor shield adafruit Motorshield v2.3. I know most ultrasonic sensors timeout after not detecting a surrounding for a long time. I want my code to continually search for a surrounding and not timeout. The robot is meant to drive in a large area until it reaches a surrounding and redirects. Below my code is attached. (Ignore the comments in the code. like the switches. these were used when i was testing still)
Thanks!!
i2c = machine.I2C(scl=machine.Pin('Y9'), sda=machine.Pin('Y10'))
motors = motor.DCMotors(i2c)
MOTOR1 = 2
MOTOR2 = 3
#Initiate Trigger and Echo Pin from Ultrasonic sensor
TRIGGER_PIN =pyb.Pin.board.X9
ECHO_PIN = pyb.Pin.board.X10
#Initiate Communication from Sonar sensor
sensor = Ultrasonic (TRIGGER_PIN, ECHO_PIN)
#Create minimum distance For Ultrasonic sensor
min_distance = sensor.distance_in_cm()
print("min_distance= ",min_distance)
#button = pyb.switch()
#def autonomy()
#no_problem = True
try:
while (True):
#if (button()):
min_distance = sensor.distance_in_cm()
#sensor_front = sensor.distance_in_cm(15)
if min_distance >= 70:
print(min_distance)
motors.speed(MOTOR1, -3500)
motors.speed(MOTOR2, -3500)
# motors.speed(MOTOR1, 3500)
# motors.speed(MOTOR2,-3500)
#if something is in the way
else:
print(min_distance)
motors.speed(MOTOR1, 0)
motors.speed(MOTOR2, 0)
time.sleep_us(10)
#Turn around
print("Do the pivot shuffle.")
motors.speed(MOTOR1, 3500)
motors.speed(MOTOR2, -3500)
time.sleep_us(10)
print(min_distance)
time.sleep_us(10)
except KeyboardInterrupt:
pass