Self-driving car

Lauren Stec and Daniel Joyce

Description of car and program
The car that we hacked into is a remote control car which we made drive itself. We used the arduino, an H-bridge, an infrared range sensor, and a heck of a lot of wires.

To get the program to work, we used the arduino code shown below to make our car turn as the infrared range sensor senses objects. When the car is within about 11 inches from an object (solid, like a person or a wall), the car will back up while turning left, and then drive forward turning right. When the car is between about 11 and 17 inches from an object, the car drives forward while turning right. If the car is further than 17 inches from an object, it will continue driving straight. The Infrared Range Sensor from Sharp uses triangulation of infrired light to read values and sense objects, while the arduino code directs the cars wheels to move.

The circut diagram shown below will show how we utilized the H bridge to consolidate the wiring and control the motor and back wheels as well as the axels and front wheels.



Arduino Code for our self-driving car

int ledPin = 4;        // pin for the motor and back wheels
int ledPin2 = 2;        // pin for the back wheels  
int analog = 3;        // analog pin for speed control
int axel1 = 7;         // axel pin to turn front wheels left and right
int axel2 = 8;          // another axel pin
int axelpmw = 9;        // analog pin power to turn the wheels  
int irReader = 1;        // infrared range sensor (analog pin)
int irVal = 0;          // infrared sensor value

void setup()                    
{
  pinMode(ledPin, OUTPUT); 
  pinMode(ledPin2, OUTPUT);  
  pinMode(analog, OUTPUT);
  pinMode(axel1, OUTPUT);
  pinMode(axel2, OUTPUT);
  pinMode(axelpmw, OUTPUT);
  Serial.begin(9600);
}

void loop()                     
{
  irVal = analogRead(irReader);      
  Serial.println(irVal);            // serial monitor for sensor
  analogWrite(analog, 120);          // speed of the car (0-255)
  analogWrite(axelpmw, 255);        // power to turn wheels (0-255)

  if (irVal > 205)                    
  // if sensor is closer than about 11 inches to an object
  {
    digitalWrite(axel2, LOW);
    digitalWrite(axel1, HIGH);          // wheels turn left
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin, LOW);          // back wheels move backwards
    delay(2000);                        // delay 2 sec
    digitalWrite(axel2, HIGH);
    digitalWrite(axel1, LOW);            // front wheels turn right
    digitalWrite(ledPin2, LOW);          // back wheels move forward
    digitalWrite(ledPin, HIGH);
    delay(2000);                          // delay 2 sec
    digitalWrite(axel2, LOW);
    digitalWrite(axel1, HIGH);            
    delay(125);                            // wheels return to center
    digitalWrite(axel1, LOW);
    digitalWrite(axel2, LOW);              // wheels stay in position
  }
  if (irVal >= 150 && irVal <= 205)        
  // if sensor is between 11 and about 17 in. from an object
  {
    digitalWrite(axel2, HIGH);
    digitalWrite(axel1, LOW);        // front wheels turn right
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin, HIGH);      // back wheels turn forward
    delay(3000);                    // delay 3 sec
    digitalWrite(axel2, LOW);
    digitalWrite(axel1, HIGH);      // front wheels return to center
    delay(125);                      
    digitalWrite(axel1, LOW);
    digitalWrite(axel2, LOW);       // wheels stay in position
  }
  else                              
  // if sensor if further than 17 in. from an object
  {
    digitalWrite(axel1, LOW);
    digitalWrite(axel2, LOW);          // wheels stay forward  
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin, HIGH);        // back wheels move forward
  }
}

 
 
Here is the circuit diagram for the h-bridge component we used to not only fit two motors on one circuit, but also to be able to use the power from the batteries to turn and spin the wheels.  before this piece, we had to use nine wires from the arduino to turn the wheels one way.  The arduino itself did not have the power provided by the normal remote control car batteries.  when the external power source was introduced back into the circuit, we were able to greatly reduce the size of that circuit.  in case any of you are curious it's the: L-293 h-bridge.

 
 
 
Pictures
 
 
The wiring for the car and the infrared range sensor.
 
 
The full view of the car with a foam cup around the sensor.
 
 
The foam cup around the sensor. The cup solved the issue of light reflecting off the floor interfering with the infrared range sensor. It also solved the problem with the car hitting an object and being too close to figure out what to do.
 
 
Another picture of the wiring of the car. The wire connected to the h-bridge but not connected to the board in the upper left corner acts as an on/off switch. When it is connected to the positive volts, the car starts to function.
 
Video
 

YouTube Video

 
 
Problems:
     there was a multitude of problems we encountered, both in the code and the physical wiring.  First of all, the code was difficult in the sense that we couldn't get our car to straight after turning to avoid an obstacle.  we eventually figured out that we needed to have a delay of 175 milliseconds of the motor turning in the opposite direction to get the wheels to snap back to front.  this as well as the physical distance from the object to initiate the turn was the major problems with the code.  as for the wiring, well... 9th times' the charm.  it took both of us a few tries to completely get not only all the correct wires for the motor, batteries, and axles identified, but also soldered and put in the correct place in the h-bridge.
 
 
Additional Attachments:
    Really, there's only one realistic thing we would do if we had more time or money, and that would be t add a second sensor so the machine could know which way to turn in order to avoid it, because in case we haven't mentioned it yet...our robot can only turn right.  if i had a few months, a few hundred dollars, and a few extra self-drivng cars, i would give it weapons.  a flamethrower or two.  you know.  just in case.
Comments