Circuit Python tutorials in Robotics II
View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python
Lets get more practice using a distance sensor on our rover:
Stop the rover when an obstacle is detected using the distance sensor.
import time
import board
# Initialize the distance sensor
# Function to control your rover motors
def stop_rover():
# Code to stop the rover's motors
pass
def move_forward():
# Code to move the rover forward
pass
try:
while True:
#read distance sensor
if distance < 20: # Threshold in cm
stop_rover()
else:
move_forward()
time.sleep(0.1)
Use an LED to visually indicate when an object is detected within a threshold.
Make the rover move forward until it detects an obstacle, then reverse for a short period.
def reverse_rover():
# Code to reverse the rover
pass
def turn_rover():
# code to turn rover 90*
pass
try:
while True:
distance = sonar.distance
print("Distance:", distance)
if distance < 20:
stop_rover()
time.sleep(0.5)
reverse_rover()
time.sleep(2) # Reverse for 2 seconds
stop_rover()
turn_rover()
else:
move_forward()
time.sleep(0.1)