BBC micro:bit
Sensing Light is a test program. You need to be using a PC with serial port drivers and something like Mu to view the REPL output. The function is used to get the readings for the left and right sensors, printing to the REPL window. Covering up the sensors and shining lights on them when running this program is useful.
from microbit import * # select: 0 = left 1 = right # dark 0, light 1023 def SenseLight(select): pin16.write_digital(select) return pin2.read_analog() while True: print(SenseLight(0), SenseLight(1)) sleep(50)
This is the simplest version of a light following program. It demonstrates the principle but needs a lot of optimisation. The function compares the two readings on the sensors and makes the robot head in the direction of the brighter light. You need to point a light source at the sensors to control the movement of the robot.
from microbit import * # select: 0 = left 1 = right # dark 0, light 1023 def SenseLight(select): pin16.write_digital(select) return pin2.read_analog() HeadTowardsLight(): lft = SenseLight(0) rgt = SenseLight(1) if lft>rgt: # head left Drive(100,400) elif rgt>lft: # head right Drive(400,100) else: #straight on Drive(400,400) sleep(20) while True: HeadTowardsLight()
Challenges
- Have the robot wake up when it detects a big change in light on either sensor. Start with a sleep statement HeadTowardsLight function is far from perfect. With a little tweaking, you can get better responses from the torch remote control if you have a small dead zone, a tolerance that allows you to move in a straight line more easily. In the function,?
- If you can get the robot to travel to and from a point to all points on the compass, a set distance away, you can make a room scanning robot. Have a robot try each of the compass points and go to the one with the brightest light. Repeat the process again. An alternative is to to go to each one in sequence and take the first one that is brighter than the starting point before repeating the process.
- If you have a light source that is easy to flash, then program the robot to respond to specific sequences of light. For example, a flash of light, followed by a pause between 500 - 1000 milliseconds, another flash, another pause, another flash. This makes the robot lights come one. Do the same again to switch them off. Work out how to tell the difference between long and short flashes of light. Now you have the tools to receive Morse code. You need to allow some tolerance, for a human user and experiment with the time frames so that things are repeatable to you. Find out how to make a dictionary using the .- characters as keys and letters as the values. If you can encode a sequence of flashes as a string of dots and dashes, you can convert them to letters. If you crack the Morse code, then use it to trigger some intersting behaviours from the robot, some light, sound etc. You could, for example, use Morse code to change the colour displayed on the neopixels or have the robot drive in a circle, flash its lights a bit, wiggle about. It's about the slowest and trickiest remote control ever but a great idea nonetheless.