Arduino robot with phototransistor mode switch (attempt #1)

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:

About engineerzero

Once and future engineer.
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s