This is an extremely programming intensive project. When doing this lab, it would be a good idea to begin by making an algorithm of all the things that need to be done and then splitting up the different tasks to group member according to their abilities.

Encoder Readings

Because of differences in room lighting, your encoder readings will vary depending on your location in a room. So, you might want to build a hood around the encoder. This way, the surrounding lighting won't effect the photocell readings, you won't have to constantly adjust the threshold values, and the darkness caused by the hood will increase the difference between the light/no light values.

Getting the Car to Go Straight.

I found that, although I set both my motors to the same power level, this wouldn't guarantee my wheels would both be turning at the same speed. Through inequality of the motor powers, uneven gearing of the wheels, or whatever, motor power level doesn't always equal wheel speed.

One solution is to manually equalize the wheel speed through trial and error. The problem with this method is it takes considerable time and patients. And, if you are fiddling with your robot and decide to change the way it is put together, the delicate balance between the motor powers may be thrown off. Also, it may be impossible to totally equalize the wheel speeds.

Another solution may be to build more then one encoder, one for each wheel. Then you can write a program that would continuously try to equalize the speed of the wheels while they are moving. However, I wasn't a good enough programmer to make this method work for me.

Setting the High and Low Threshold Values.

One seemingly obvious way to find the threshold values may be to manually turn the spoke so the photocell is fully exposed to the LED light, and then ask IC what the analog value is. Then turn the spoke so it is covering up as much of the photocell as possible and ask IC what that analog value is. However, I found this method DID NOT work. As the motor is running, the spoke is turning very quickly. And as the spoke quickly turns, the photocell won't have time to reach its "totally-dark" value before it is again exposed to the LED light. And while the photocell is exposed to light, it won't have time to reach its maximum "I'm fully exposed to light" value before the light source is removed.

One method to deal with this is to write a program that records 100 consecutive values the analog port is receiving while the wheels are turning. On reviewing these values, you should notice a rhythmical pattern of highs and lows.

Motor Speed

I found that having a low motor speed was better then setting the wheels at a high motor speed. Setting a lower motor speeds will allow for a greater difference in high and low threshold values (see "Setting the High and Low Threshold Values")

Difference in Photocell and LED Lights

If using more then one encoder, set each encoder and set each encoder threshold separately. Due to differences in photocells, LED lights, and so on, the readings you obtain from one encoder won't necessarily be the same readings you'll get from the other encoder.


After giving the "all off" command, the wheels may not come to an immediate halt. More likely then not, after the motor power is cut off, the wheels will continue to move a little as it slows to a stop. One way to counteract the wheels' drifting is to make the wheels run in the opposite direction they had been for a fraction of a second, and then give the "all off" command.

Accurately Changing Direction of Movement

As mentioned in the "Drifting Section," your lego wheels won't come to an immediate halt when the motor power is turned off. Instead, they will drift to a stop. This slight drifting will cause problems when your car is moving in one direction and you want it to go another direction. For example, your robot is moving forward and you decide you want it to turn left right now. In order to make a perfect 90 degree turn, your right wheel must move in a positive direction and your left wheel must move in a negative direction for x seconds. Because the right wheel is already moving forward, the car has no problem executing this instruction and does it quickly. However, getting the left wheel to move backwards requires more effort. In order to go backwards, the wheel must first expend some energy fighting the forward drift and momentum of the wheel. Thus, the backward left wheel command isn't executed as quickly as the forward right wheel command. And the time difference in the execution of the commands will effect how the car turns.

To solve this problem, have the car, just before it is about to change its direction of movement, come to a complete stop for x seconds.