Project tutorial
Color Following Car

Color Following Car © GPL3+

Ever wanted a car the drives semi-autonomously? There you have it! You only need two diffrent colored lines and it easy to programm it.

  • 2,053 views
  • 0 comments
  • 15 respects

Components and supplies

DC motor (generic)
×4
Color Sensor
×3
Adafruit Motor Shield V1
×1
Tens70
9V battery (generic)
×1
12002 04
Breadboard (generic)
×1

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

I've always wondered what it took to make a self-driving car. So for my first program, I decided to try it. The car follows the blue line, and when one of the sensor sees red, it makes a curve in the opposite direction.

The plate on the back of the car is where I have theb battery with a battery overwatch.

The Chassis was purchased from the Internet, but it does not have to be this one.

Explination of the Program:

1. Definition:

1.1 Motor:

#include<AFMotor.h>
AF_DCMotormotor1(1, MOTOR12_1KHZ);
AF_DCMotormotor2(3, MOTOR34_1KHZ);
AF_DCMotormotor3(2, MOTOR12_1KHZ);
AF_DCMotormotor4(4, MOTOR34_1KHZ);

First line is the Inclution of the AFMotor library. Lines 2-4 is the definition of the diffrent Motors.

1.2 Color Sensor:

const int s0 = A8; //Conection for Sensor
const int s1 = A9;
const int s2L = A11; //Left Sensor
const int s3L = A10;
const int outL = A12;
int redL =0; 
int greenL= 0;
int blueL =0;
const int s2R = A14;  //Right Sensor
const int s3R = A13;
const int outR = A15;
int redR =0; 
int greenR= 0;
int blueR =0;

"A9" and "A8" are the outputs for the General Light of the Colorsensor.

"s2" and "s3" are the inputs of the sensor each sensor has such an port. The letter on the end is for the Location. L is for Left and R is for Right.

out is the frequenty output of the Sensor also left and right. See more info here.

The variable for the colors Red, Blue and Green are also two times at hand the diffrents is also the last letter L and R.

1.3 Detection:

int sensor_left = 0;
int sensor_right = 0;

Those two variables are for the case when the Car sees the the Color it shoud follow. More to that later.

2. Set up:

Now follows the "void Setpup()"

2.1 Serial Kommunitaktion

Fist thing is we activate the Serial Komunikation.

Serial.begin(9600); //Serielle Kommunikation 

2.2 Color Sensor:

The Color Sensor has some outputs. The common outputs Are "s0" and "s1".

pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);

The fist two Lines set the Pins "s1" and "s0" to output mode.

The two last lines set the Output to High that the LED are allways on.

pinMode(s2L, OUTPUT);
pinMode(s3L, OUTPUT);
pinMode(outL, INPUT);
pinMode(s2R, OUTPUT);
pinMode(s3R, OUTPUT);
pinMode(outR, INPUT);

Now we set up the other pins the "s2" and "s3" must be set to output mode and the "out" pins must be set to input.

3. Main Program

Now comes the main part.

3.1 Activate Function Color

The first thing is the activation of the function color. This function will be defined at the end of the Program.

color();

3.2 Detection of the Street.

Here we wil program the detection part of the prgoram. The part that look if the Road is beneath the Car. We will do that with an easy "If" function.

if(blueL <= 10 && redL >=11 && greenL >= 11 && blueL < redL && blueL <greenL)
{
sensor_left = 1;
}
else
{
sensor_left = 0;
}
}

So that the variable "sensor_left" to an High Signal switches. The following condiction must be met.

  • The variable "blueL" must have a lower or even value of 10.
  • The variable "redL" and greenL" must have a higher or even value of 11.
  • Also "blueL" must be smaller than "redL" and "greenL".

Or else the variables "sensor_left" will go to 0.

The code for the Right sensor is Identical, the only diffrents is "L" and the "left". there then must be a "R" and "right" for the other Sensor.

3.3 Driving

Now comes the most important part of the program. The driving part.

The car is programmed to drive "staighthead" if both sensors in the front see blue and when only one of them sees blue it will turn the opposite direction. But if only the Sensor in the back sees blue it will go backwards.

Code

Copie and Paste Auto Code V17Arduino
Thats the Coe for Copy and Paste. My Change at times.
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(3, MOTOR34_1KHZ);
AF_DCMotor motor3(2, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
//Farbe
const int s0 = 47; //Conection for Sensor
const int s1 = 49;

const int s2L = A14;
const int s3L = A13;
const int outL = A15;

int redL = 0; //Variablen for Left LEDs named
int greenL = 0;
int blueL = 0;

const int s2R = A11;
const int s3R = A10;
const int outR = A12;

int redR = 0; //Variablen for Right LEDs named
int greenR = 0;
int blueR = 0;

const int s2b = A8;
const int s3b = A7;
const int outb = A9;

int redb = 0; //Variablen for Right LEDs named
int greenb = 0;
int blueb = 0;

int sensor_left = 0;
int sensor_right = 0;
int sensor_back = 0;

void setup()

{

  Serial.begin(9600); //Serielle Kommunikation 

  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);

  pinMode(s2L, OUTPUT);
  pinMode(s3L, OUTPUT);
  pinMode(outL, INPUT);

  pinMode(s2R, OUTPUT);
  pinMode(s3R, OUTPUT);
  pinMode(outR, INPUT);

  pinMode(s2b, OUTPUT);
  pinMode(s3b, OUTPUT);
  pinMode(outb, INPUT);



}

void loop()
{
  color();

  { //detection
    { //Sensor Left
      if (blueL <= 10 && redL >= 11 && greenL >= 11 && blueL < redL && blueL < greenL)

      {
        sensor_left = 1;
      }
      else
      {
        sensor_left = 0;
      }
    }

    { //Sensor Right
      if (blueR <= 10 && redR >= 11 && greenR >= 11 && blueR < redR && blueR < greenR)

      {
        sensor_right = 1;
      }
      else
      {
        sensor_right = 0;
      }
    }
      { //Sensor Back
      if (blueb <= 10 && redb >= 11 && greenb >= 11 && blueb < redb && blueb < greenb)

      {
        sensor_back = 1;
      }
      else
      {
        sensor_back = 0;
      }
    }
  }
  { //Drive
if (sensor_left ==1 or sensor_right==1)
{
    Found:
if (sensor_left == 1 && sensor_right == 1 or sensor_left == 1 && sensor_right == 1 && sensor_back == 0)
    {//Straightforward
      Straightforward:
      motor1.setSpeed(150);
      motor2.setSpeed(150);
      motor3.setSpeed(150);
      motor4.setSpeed(150);
      motor1.run(FORWARD);
      motor2.run(FORWARD);
      motor3.run(FORWARD);
      motor4.run(FORWARD);
      delay(7);
      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
      Serial.print(" Straightforward ");
    }
    else if (sensor_left == 0 && sensor_right == 1)
    {//Right Curve
      motor1.setSpeed(225);
      motor2.setSpeed(225);
      motor3.setSpeed(200);
      motor4.setSpeed(200);
      motor1.run(BACKWARD);
      motor2.run(BACKWARD);
      motor3.run(FORWARD);
      motor4.run(FORWARD);
      delay(7);
      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
      Serial.print(" Right Curve ");
    }
    else if (sensor_left == 1 && sensor_right == 0)
    {//Left Curve
      motor1.setSpeed(200);
      motor2.setSpeed(200);
      motor3.setSpeed(225);
      motor4.setSpeed(225);
      motor1.run(FORWARD);
      motor2.run(FORWARD);
      motor3.run(BACKWARD);
      motor4.run(BACKWARD);
      delay(7);
      motor1.run(RELEASE);
      motor2.run(RELEASE);
      motor3.run(RELEASE);
      motor4.run(RELEASE);
      Serial.print(" Left Curve "); 
    }
  } 
  else if (sensor_left == 0 && sensor_right == 0 && sensor_back == 1)
  {
//Backward
      motor1.setSpeed(160);
      motor2.setSpeed(160);
      motor3.setSpeed(150);
      motor4.setSpeed(150);
      motor1.run(BACKWARD);
      motor2.run(BACKWARD);
      motor3.run(BACKWARD);
      motor4.run(BACKWARD);
      delay (7);
      Serial.print(" Backward "); 
  }
  else
  {
    
  }
  }
}
void color() 

{

  { //Left Sensor
    digitalWrite(s2L, LOW);
    digitalWrite(s3L, LOW);
    redL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH);
    digitalWrite(s3L, HIGH);
    blueL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH);
    digitalWrite(s2L, HIGH);
    greenL = pulseIn(outL, digitalRead(outL) == HIGH ? LOW : HIGH);
  }

  { //Right Sensor
    digitalWrite(s2R, LOW);
    digitalWrite(s3R, LOW);
    redR = pulseIn(outR, digitalRead(outR) == HIGH ? LOW : HIGH);
    digitalWrite(s3R, HIGH);
    blueR = pulseIn(outR, digitalRead(outR) == HIGH ? LOW : HIGH);
    digitalWrite(s2R, HIGH);
    greenR = pulseIn(outR, digitalRead(outR) == HIGH ? LOW : HIGH);
  }
    { //Back Sensor
    digitalWrite(s2b, LOW);
    digitalWrite(s3b, LOW);
    redb = pulseIn(outb, digitalRead(outb) == HIGH ? LOW : HIGH);
    digitalWrite(s3b, HIGH);
    blueb = pulseIn(outb, digitalRead(outb) == HIGH ? LOW : HIGH);
    digitalWrite(s2b, HIGH);
    greenb = pulseIn(outb, digitalRead(outb) == HIGH ? LOW : HIGH);
  }
}
Programm V17
Thats the Programm for the Car

Schematics

Schema
Schema in a Fritzing format
schema_AoAHyg90ZO.fzz
Schema
Schema in a JPG
Schema steckplatine 6wc5rqeqih

Comments

Similar projects you might like

Turn your RC Car to Bluetooth RC car

Project tutorial by Prajjwal Nag

  • 12,850 views
  • 2 comments
  • 15 respects

Color Sensor Prototype: Detect Color Name from Objects

Project tutorial by moretticb

  • 3,035 views
  • 4 comments
  • 23 respects

Flame Car

Project showcase by giorgiorosso

  • 572 views
  • 1 comment
  • 1 respect

LED Color Sequencer

Project tutorial by Team thefantasticode.Group

  • 5,881 views
  • 10 comments
  • 21 respects

RC Car to BT Car Controlled with Blynk

Project tutorial by Team Daxes Hacks

  • 3,624 views
  • 2 comments
  • 6 respects

Remote Control Car w/ PocketBeagle and Arduino

Project tutorial by Christina Mezmur

  • 3,281 views
  • 3 comments
  • 12 respects
Add projectSign up / Login