Circuit Python tutorials in Robotics II
View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python
Let’s get some more practice with line sensors with a variety of challenges
Line sensors are sensitive to light conditions. Changes in ambient light (shadows or brighter sun) can effect the line sensor’s reading. Glossy vs matte surfaces also affect things. This is why our sumo ring is painted matte white in the middle with a matte black ring on the outside. However, lifting a sensor off the ground can also cause false readings. Let’s try to stop these false readings.
and
statement to combine 2 separate conditions into one statement. In our case, we’re checking that a variable is in between 2 values# ... initialize sensor & library imports
def mapped_voltage(pin):
# Maps our current 0-65535 range to 0-100
mapped_value = map_range(pin.value, 0, 65535, new_min, new_max)
return floor(mapped_value)
# ----------- INIT VARIABLES --------------
black_min = 70 # change to minimum observed reading for black
white_max = 50 # change to maximum observed reading for white
air_min = 51 # change to minimum observed reading for in the air
air_max = 69 # change to minimum observed reading for in the air
while True:
line_sensor = mapped_voltage(line_pin) # replace with your init sensor
if line_sensor > black_min:
print("on black line!")
# consider turning on debug LED 1 here
elif line_sensor < air_min and line_sensor > air_max:
print("lifted up in the air!")
# consider turning on debug LED 2 here
else:
print("we're on white!")
# consider turning off all LEDs here
time.sleep(0.02)
Now that you have the basics of handling false positive readings in the air, let’s make a rover that stays on the platform.
# ...
while True:
line_sensor = mapped_voltage(line_pin) # replace with your init sensor
if line_sensor > black_min:
print("on black line!")
# stop
# back up
# turn 180*
elif line_sensor < air_min and line_sensor > air_max:
print("lifted up in the air!")
# consider turning on debug LED 2 here
# move forward
else:
print("we're on white!")
# consider turning off all LEDs here
# move forward
time.sleep(0.02)