The script halt_under_shadow will make the cyber:bot go forward until the phototransistor detects a shadow that’s dark enough to make the voltage applied to D/A2 drop below 0.1 V, or a different threshold you choose.
- Enter and save the script halt_under_shadow script, but don't flash it to your micro:bit yet.
- Using the measurements you just took with the script phototransistor_voltage, update the threshold value 0.1 in if volts < 0.1 with a value consistent with shade in your ambient lighting.
#halt_under_shadow
from cyberbot import *
bot(18).servo_speed(75)
bot(19).servo_speed(-75)
while True:
ad2 = pin2.read_analog()
volts = ad2 * (3.3/1024)
if volts < 0.1: # Update voltage threshold value here
bot(18).servo_speed(None)
bot(19).servo_speed(None)
- Save the modified version of halt_under_shadow and flash it to your micro:bit module.
- Place your cyber:bot on the floor in ambient light, pointing straight towards the cardboard box "garage" or other dark spot.
- Move the power switch to position 2 so the cyber:bot will drive forward. How close did it get to stopping directly in the box-garage?
- If needed, try making adjustments to the threshold you set in the if volts <... statement to get the cyber:bot to stop directly the box-garage.