I have a program (aka ‘sketch’) which steers my arduino robot (ie, cheapbot) in a square path. I revised it to use a phototransistor as a mode switch between standby and act (ie, move) modes.
Originally, I used the delay() function for the timing of the servo movements, but that meant I was sampling the analog port that the phototransistor is connected to only once every few seconds. I’m reading Beginning Arduino Programming, and it suggested that instead of the delay function, use the millis() function in a while loop because then I could continuously sample the analog port inside the loop, many many times per second.
So that I did, and it kind of works. I think the reason that it doesn’t work as well as I’d like is because of the optical equivalent of mechanical switch bounce, but we’ll see what can be done about that.
One problem that I successfully trouble-shooted is that at first I was getting very strange, random responses from the robot whenever I shone the light on the phototransistor. I finally figured out it was because I was using an int type declaration for the time variable, and I needed to make it unsigned long. This is covered at the arduino site and also in the book.
Anyhow, this is what the behavior of the phototransistor as robot mode switch looks like so far: