robot car with TCS230 color recognition

i started a project [ARDUINO use in traffic light]
with a robot car 4WD [ L298N Motor Module , Arduino Sensor Shield v5.0 ,HC-SR04 Ultrasonic Sensor , Arduino Uno board ] with a color recognition TCS 3200 when sensor see red color car must stop and when the light is green continue moving
but i have wrong in my code and car moving only left
could you give me some help and advice
thanks a lot…

#include <Ultrasonic.h>
#include <Servo.h>
#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);

Servo myservo;

int pos = 90;
Ultrasonic ultrasonic(8, 9);

const int s0 = 13; // sensor pins
const int s1 = 12;
const int s2 = 11;
const int s3 = 10;
const int out = 0; // TCS230 output

const int RED_PIN = 0;
const int GREEN_PIN = 1;
const int BLUE_PIN = 2;

int r = 0;
int g = 0;
int b = 0;
int color;

int dist = 10; 


void setup() {

  myservo.attach(7);
  myservo.write(90);

  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);


  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(out, INPUT);

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

  Serial.begin(9600);    

  motor1.setSpeed(200);     
  motor2.setSpeed(200);
  motor3.setSpeed(200);     
  motor4.setSpeed(200);
}

void loop()
{
  Color();
  RGB_LED();
  if (ultrasonic.Ranging(CM) <= dist)
  {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    motor3.run(RELEASE);
    motor4.run(RELEASE);
    delay(1000);
    check_direction();
  }
  delay(50);
}

void Color()
{
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
  //   count OUT, pRed, RED
  r = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s3, HIGH);
  //count OUT, pBLUE, BLUE
  b = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
  digitalWrite(s2, HIGH);
  // count OUT, pGreen, GREEN
  g = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);


  if ((r >= 50 && r <= 65) && (g >= 55 && g <= 65) && (b >= 15 && b <= 20)) {
    Serial.print(0);
    color = 0;
  }

  else if ((r >= 50 && r <= 65) && (g >= 40 && g <= 65) && (b >= 10 && b <= 20 )) {
    Serial.print(1);
    color = 1;
  }

  else if ((r >= 13 && r <= 16) && (g >= 12 && g <= 15) && (b >= 3 && b <= 4)) {
    Serial.print(2);
    color = 2;
  }

  else if ((r >= 50 && r <= 60) && (g >= 30 && g <= 41) && (b >= 10 && b <= 15)) {
    Serial.print(3);
    color = 3;
  }

  else if ((r >= 15 && r <= 16) && (g >= 16 && g <= 19) && (b >= 5 && b <= 8)) {
    Serial.print(4);
    color = 4;
  }

  else if ((r >= 20 && r <= 22) && (g >= 36 && g <= 41) && (b >= 10 && b <= 11)) {
    Serial.print(5);
    color = 5;
  }

  else if ((r >= 22 && r <= 24) && (g >= 44 && g <= 50) && (b >= 10 && b <= 12)) {
    Serial.print(6);
    color = 6;
  }

  else if ((r >= 33 && r <= 39) && (g >= 18 && g <= 22) && (b >= 4 && b <= 8)) {
    Serial.print(7);
    color = 7;
  }

  else if ((r >= 20 && r <= 24) && (g >= 40 && g <= 50) && (b >= 10 && b <= 11)) {
    Serial.print(8);
    color = 8;
  }

  else {
    Serial.print(0);
    color = 0;
  }

}

void RGB_LED() {

  switch (color) {

    case 0:
      //Black
      digitalWrite(RED_PIN, LOW);
      digitalWrite(GREEN_PIN, LOW);
      digitalWrite(BLUE_PIN, LOW);
      break;

    case 1:
      // Blue
      digitalWrite(RED_PIN, LOW);
      digitalWrite(GREEN_PIN, LOW);
      digitalWrite(BLUE_PIN, HIGH);
      break;

    case 2:
      //White
      digitalWrite(RED_PIN, HIGH);
      digitalWrite(GREEN_PIN, HIGH);
      digitalWrite(BLUE_PIN, HIGH);
      break;

    case 3:
      //Green
      digitalWrite(RED_PIN, LOW);
      digitalWrite(GREEN_PIN, HIGH);
      digitalWrite(BLUE_PIN, LOW);
      break;

    case 4:
      //Yellow
      analogWrite(RED_PIN, 255);
      analogWrite(GREEN_PIN, 255);
      analogWrite(BLUE_PIN, 0);
      break;

    case 5:
      //Orange
      analogWrite(RED_PIN, 145);
      analogWrite(GREEN_PIN, 126);
      analogWrite(BLUE_PIN, 42);
      break;

    case 6:
      //Red
      digitalWrite(RED_PIN, HIGH);
      digitalWrite(GREEN_PIN, LOW);
      digitalWrite(BLUE_PIN, LOW);
      break;

    case 7:
      //syan
      analogWrite(RED_PIN, 0);
      analogWrite(GREEN_PIN, 255);
      analogWrite(BLUE_PIN, 255);
      break;

    case 8:
      //Purple
      analogWrite(RED_PIN, 128);
      analogWrite(GREEN_PIN, 0);
      analogWrite(BLUE_PIN, 128);
      break;


  }
}


void check_direction()
{

  long dL, dR;
  myservo.write(0);
  delay(500);
  dR = ultrasonic.Ranging(CM);
  myservo.write(180);
  delay(500);
  dL = ultrasonic.Ranging(CM);



  if (dR >= dL)
  {
    motor1.setSpeed(255);     
    motor2.setSpeed(255);
    motor3.setSpeed(255);    
    motor4.setSpeed(255);

    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
    delay(500);

    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
    delay(1200);
  }
  else if (dR <= dL)
  {
    motor1.setSpeed(255);     
    motor2.setSpeed(255);
    motor3.setSpeed(255);     
    motor4.setSpeed(255);

    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
    delay(500);

    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(BACKWARD);
    motor4.run(BACKWARD);
    delay(1200);
  } else {
    motor1.setSpeed(200);    
    motor2.setSpeed(200);
    motor3.setSpeed(255);     
    motor4.setSpeed(255);

    motor1.run(FORWARD);
    motor2.run(FORWARD);
    motor3.run(FORWARD);
    motor4.run(FORWARD);
    myservo.write(90);
    delay(2000);
  }

}

Please use [ code ] tags. The forum software has turned some of your code into smileys 8)

And if you don't know what that means please read this:- How to use this forum

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Have you written your code in stages, that is a bit at a time?

  • Write and get working correctly the motor drive.
  • Write and get working correctly the code to input the colour recognition sensor.
  • Write and get working correctly the code to drive your display RGB LED.
  • Combine the three working codes one at a time and get working.

What model Arduino are you using? How are you driving the motors? What is your colour recognition sensor, please provide a link to data/specs please?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :)

MorganS: Please use [ code ] tags. The forum software has turned some of your code into smileys 8)

sry for my mistakes know i use the tag  thanks for notice

OK, that looks simple enough. It seems that the color-sensing code does nothing to the direction it turns. Is this what you want?

The direction is controlled by the ultrasonic sensor. Presumably it’s mounted on the servo which points it left and right.

  1. Does the servo actually point the sensor left and right properly?

  2. Are you getting different readings from the left and right positions?
    If the sensor doesn’t see anything in range, it might return zero or some other strange number. Put in a few Serial.print() statements so that you can see on the serial monitor the values returned by the sensor.

  3. Are the motors wired the right way? You might have left and right reversed.

  4. Look at this code copied from your program…

if (dR >= dL)
  {
  }
  else if (dR <= dL)
  {
  } else {
  }

Can you see what’s wrong with it? If the first condition is not satisfied, then the second always is and it never ever reaches the third option.

Take the code that makes the robot move and put it into functions. That way you can write the decision code much shorter and the mistakes will be more obvious. You might have functions like goForward() and goRight().

Thank you for your advices , i will ckeck the code again and i will change the functions .