Tasks for long period and Friday
Activity 1: “Theremin”
You will write a program that beeps at different pitches based on the light sensor values. For example, I wrote one that just adds up the three sensor values and uses that as the beep frequency. I had mine loop for 20 seconds so that as I approach my hand to the robot, the pitch gets higher and higher. This behavior is roughly based off of an interesting instrument called the Theremin. Look it up!
Activity 2: Cockroach function
Your cockroach function will take one value as a parameter: the length of time the robot should move for. Your cockroach should flee light and go towards darker places. So, for example, if you start it near the edge of the shadow of a table, it should go under the table. There are a few ways to implement this, but here is a simple one:
- read all three light sensor values
- if the left light sensor gives a value less than both the middle and the right light sensor, move right (remember, a low value means a bright light)
- if the right light sensor gives a value less than both the middle and the left light sensor, move left
- otherwise, go forward
Activity 3: Obstacle avoidance
The following code is the start to an obstacle avoidance program. See if you can figure out what it does from reading it, then save it and run it.
while timeRemaining(30):
if getObstacle("right"):
backward(1, .1)
turnLeft(0.7, .1)
elif getObstacle("left"):
backward(1, .1)
turnRight(0.7, .1)
else:
forward(.5)
wait(.1)
stop()
Right now, it’s not so hot. What happens if you make your robot run this program while it’s directly facing a wall? It just crashes, right? That’s because we’re totally ignoring obstacles right in front of us! Add a case for when there’s something right in front. Your robot should go backwards then pick randomly with equal probability to either turn right or left.
Next, make your robot beep when it sees an obstacle. You should use three different pitches and tone lengths for the three different obstacle positions so you can hear what your robot is doing.
Finally, make your robot stop and take a picture whenever it runs into an obstacle.
Call me over to check you off when you have all of this done!
If you do finish all of these, please look over this book chapter about implementing insect-like behavior in robots: http://cs.brynmawr.edu/~dkumar/Myro/Text/June09/PDF/Chapter6.pdf and try out the examples.




