I got a cheap R/C car for Christmas (my girlfriend knew I needed some kind of motorized toy to build the robot) and I just striped its guts off.
After that I just put a single side Arduino that I made, a L293D H-Bridge, a IR proximity sensor (bought at SparkFun.com) and some LDR.
H-Bridge
The R/C car had two small DC brushed motors, one for the traction and one for the direction. It was pretty easy to wire up this to the L2W93D.
Proximity sensor
The proximity sensor has 3 pins: VCC, V0 and GND. I just wired the VCC and GND to the respective Arduino pins, and V0 to an analog input.
Light sensor
The light sensor was a little more complicated. Because I needed to detect the direction of the brightest light source I decided to use 3 single sensors, 2 pointing in each direction and one forward. This way I can evaluate the sides against the central value and decided whether to turn left or right.
The output of each sensor was connected to an analog input.
The Code
/* Photovore
*/
// Pin configuration
int motorLeadF = 9;
int motorLeadB = 10;
int directionLeadL = 5;
int directionLeadR = 6;
// Variable declaration
int proximity = 0;
int leftLight = 0;
int centerLight = 0;
int rightLight = 0;
void setup() {
// start signal
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
// pin configuration
pinMode(motorLeadF, OUTPUT);
pinMode(motorLeadB, OUTPUT);
pinMode(directionLeadL, OUTPUT);
pinMode(directionLeadR, OUTPUT);
// motor initialization
digitalWrite(motorLeadF, LOW);
digitalWrite(motorLeadB, LOW);
digitalWrite(directionLeadL, LOW);
digitalWrite(directionLeadR, LOW);
}
void loop()
{
proximity = analogRead(3);
leftLight = analogRead(2);
centerLight = analogRead(1);
rightLight = analogRead(0);
if (proximity > 205) {
// go back
digitalWrite(motorLeadF, LOW);
digitalWrite(directionLeadL, HIGH);
digitalWrite(motorLeadB, HIGH);
delay(750);
digitalWrite(motorLeadB, LOW);
digitalWrite(directionLeadL, LOW);
}
if (abs(deltaRight - deltaLeft) > 20) {
if (deltaRight > 10 || deltaLeft > 10) {
analogWrite(motorLeadF, 180);
if (leftLight > centerLight) {
digitalWrite(directionLeadL, HIGH);
digitalWrite(directionLeadR, LOW);
} else if (rightLight > centerLight ) {
digitalWrite(directionLeadL, LOW);
digitalWrite(directionLeadR, HIGH);
} else {
digitalWrite(directionLeadL, LOW);
digitalWrite(directionLeadR, LOW);
}
}
} else {
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13, LOW);
delay(300);
}
}



0 comments:
Post a Comment