Light sensors guids this robot towards the light.
int minIndex(int a[], int length){ int minIndex = 0; for (int i = 1; i < numSensors; i++){ if ( a[minIndex] > a[i] ) minIndex = i; } return minIndex;}
goes through every one of the 5 light sensors and sees which has the smallest value. The one with the smallest value corresponds to the light sensor receiving the most light. Now depending on which sensor is the minimum there are three possibilities of action. The possibilities are to turn left if the brightest light was to the left, go straight if the bright light was head on, and go to the right if the light was on the right side. This is expressed in the if...else statment below:
if ( curMin == 0 ){ // go forward we are facing the light left.write(85); right.write(105); } else if ( curMin == 1 || curMin == 2){ left.write(105); right.write(105); } else { left.write(80); right.write(80 ); }
/* LucidTronix Plant Navigator * For tutorial, schematics and explantion see: * http://www.lucidtronix.com/tutorials/17 * this servo robot steers in the brightest direction * in effect heading towards the light. * Using five photoresistor light sensors arranged in a circle * And differential steering on the 2 servo motors. */#includeconst int numSensors = 5;int lightSensors[numSensors];const int ledPin = 13; Servo left;Servo right;int minIndex(int a[], int length){ int minIndex = 0; for (int i = 1; i < numSensors; i++){ if ( a[minIndex] > a[i] ) minIndex = i; } return minIndex;}void setup() { Serial.begin(9600); left.attach(5); right.attach(6); }void loop() { for (int i = 0; i < numSensors; i++){ lightSensors[i] = analogRead(i); } int curMin = minIndex(lightSensors, numSensors); if ( curMin == 0 ){ // go forward we are facing the light left.write(85); right.write(105); } else if ( curMin == 1 || curMin == 2){ left.write(105); right.write(105); } else { left.write(80); right.write(80 ); }}
| Title | Description | # | Cost | Link | Picture |
| Servo Motor | Continuous rotation servo motor. | 2 | $13.95 | Link |
|
| Servo Wheel | Wheel that attaches easily to servo motors. | 2 | $4.0 | Link |
|
| ATMEGA168A-AU | IC MCU AVR 16K FLASH 32TQFPValue:1.8 V ~ 5.5 V 20MHz | 1 | $2.43 | Link |
|
| Photocell | Photo resistor Light sensor 16-33KOHMValue: 16 ~ 33 kOhms @ 10 lux | 6 | $0.8 | Link |
|
| Crystal | CRYSTAL 16.00000 MHZ 20PF SMDValue:16MHz | 1 | $0.53 | Link |
|
| Resistor | RES 10K OHM 1/4W 5% CF MINIValue:10k | 10 | $0.08 | Link |
|
Comments: