# Robot That Roams in Serpentine Fashion

Hello Everyone,

For this project, I need a robot to roam on a rectangular surface enclosed by walls and search for colored shapes. Each time it passes over a colored shape, an LED needs to light up. I have this part down.

What I do not have down is how to get the robot to roam. Right now I have an ultrasonic sensor on the front that senses the distance in front of the robot. When the distance is less than 10 inches, it turns left, takes a value, and then stores it, and then turns right and does the same. Then, it compares the two distances, and turns left twice to begin the serpentine movement (moves vertically, but the serpentine movement gets the robot to the other side of the board eventually) across the board.

The issue is that once it gets more than 50% across the board (as in left to right), the distances switch. In other words, I cannot use the same if…else statement(s) again since now, it would just begin going back to where it started.

Any help on this would be much appreciated. Anything from a way to solve this to a new pattern would be greatly appreciated.

The simplest trick is to have the robot turn 45 degrees left each time it finds an obstacle. In the end (as the robot will never turn exactly 45 degrees it will cover the whole of and shaped box and find its way out of any traps.

Mark

cman5145:
For this project, I need a robot to roam on a rectangular surface enclosed by walls and search for colored shapes. Each time it passes over a colored shape, an LED needs to light up. I have this part down.

Could you post the code you already have completed?

Why not something like this - robot starting from bottom right, as in your example:

1. Move forward, checking front sensor.
2. Is distance from front < 10 inches?
3. If yes, go to step 5.
4. If no, go to step 1.
5. Turn left 90 degrees.
6. Is distance from front < 10 inches?
7. If yes, go to step 18.
8. Move forward a bit, then turn left 90 degrees.
9. Move forward, checking front sensor.
10. Is distance from front < 10 inches?
11. If yes, go to step 13.
12. If no, go to step 9.
13. Turn right 90 degrees.
14. Is distance from front < 10 inches?
15. If yes, go to step 18.
16. Move forward a bit, then turn right 90 degrees.
17. Go to step 1.
18. Stop.

This assumes that you can make accurate turns, which if you are already zig-zagging along currently, you can. Now - I don’t know if you are using a compass, or encoders to know what direction you are facing, but for steps 8 & 16, by “moving forward a bit” - I mean for however long of a distance you need (one body length? Less? Regardless, just move forward by some amount). Ideally, if you have encoders on the wheels, you can “measure” how far to move forward.

I also assumed you had to “stop” at the end; if not, then the code becomes a bit more complicated - likely needing further measurements after each turn, and more logic while tracking orientation. The above algorithm is also implemented in a naive manner; a better solution would involve a state machine of some sort most likely.

Finally, if you don’t have a compass, then including one may help you, instead of relying solely on encoders; if your wheels slip, for instance, then your robot may think its facing in a particular direction when it isn’t (and the error may accumulate over time, putting the robot way off course by the end of the run). Note that a compass won’t fix everything; it isn’t a magic bullet or such - but an extra means of determining orientation to a certain degree of probability can’t hurt matters.