Maxconnor
Published © GPL3+

Color Following Car

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.

AdvancedFull instructions provided8,962
Color Following Car

Things used in this project

Hardware components

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

Software apps and online services

Arduino IDE
Arduino IDE
Arduino Web Editor
Arduino Web Editor

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schema

Schema in a Fritzing format

Schema

Schema in a JPG

Code

Copie and Paste Auto Code V17

Arduino
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

Credits

Maxconnor
1 project • 1 follower

Comments