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.
- Use the micro:bit Python Editor to open a script that already has the cyber:bot module, like phototransistor_voltage or cyberbot-template-with-blink.
- Set the project's name to halt_under_shadow, and enter the script below over the existing script.
- 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.
- Click Save, and then click Send to micrso:bit.
#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)
- Place your cyber:bot on the floor in ambient light, pointing straight towards the cardboard box "garage" or other dark spot.
- Set the cyber:bot board's PWR switch to 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.