Not working color line following robot

Hello there, I had been struggling with building a robot that follows along color line using two color sensors with 4 motors. Not sure it's software or hardware problem but the car is not moving at all. I give the supply of 12V with external adjustable power supply. Please, can someone help with this problem. Here is the code I'm using-

#define enA 10//Enable1 L298 Pin enA 
#define in1 9 //Motor1  L298 Pin in1 
#define in2 8 //Motor1  L298 Pin in1 
#define in3 7 //Motor2  L298 Pin in1 
#define in4 6 //Motor2  L298 Pin in1 
#define enB 11//Enable2 L298 Pin enB 
const int speed = 50;
//Color Sensor
const int s0 = 4; //Conection for Sensor
const int s1 = 5;

const int s2L = A0;
const int s3L = A1;
const int outL = A2;

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

const int s2R = A3;
const int s3R = A4;
const int outR = A5;

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

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

void setup()

{

  Serial.begin(9600); //Serielle Kommunikation
  //MOTOR PINS
  pinMode(enA, OUTPUT); // declare as output for L298 Pin enA 
  pinMode(in1, OUTPUT); // declare as output for L298 Pin in1 
  pinMode(in2, OUTPUT); // declare as output for L298 Pin in2 
  pinMode(in3, OUTPUT); // declare as output for L298 Pin in3   
  pinMode(in4, OUTPUT); // declare as output for L298 Pin in4 
  pinMode(enB, OUTPUT); // declare as output for L298 Pin enB 
  //analogWrite(enA, 200); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1 Speed 
  //analogWrite(enB, 200); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2 Speed 
  //COLOR Sensor 
  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);
}

void loop()
{
  color();
  Serial.print("LEft_");
  Serial.print(redL);
  Serial.print(",");
  Serial.print(greenL);
  Serial.print(",");
  Serial.print(blueL);
  Serial.print(",");
  Serial.print("Right_");
  Serial.print(redR);
  Serial.print(",");
  Serial.print(greenR);
  Serial.print(",");
  Serial.println(blueR);
  delay(1000);

  { //detection
    { //Sensor Left
      if (redL <= 13 && blueL >= 15 && greenL >= 15 && redL < blueL && redL < greenL)
      {
        sensor_left = 1;
      }
      else
      {
        sensor_left = 0;
      }

    }

    { //Sensor Right
      if (redR <= 13 && blueR >= 15 && greenR >= 15 && redR < blueR && redR < greenR)
      {
        sensor_right = 1;
      }
      else
      {
        sensor_right = 0;
      }

    }
  }
  { //Drive
   if (sensor_left ==1 or sensor_right==1)
    {
     Found:
     if (sensor_left == 1 && sensor_right == 1)
     {//forward
      forward();
      delay(7);
      stop();
      Serial.print("Going forward ");
     }
     else if (sensor_left == 0 && sensor_right == 1)
     {//Right Curve
      turnRight();
      delay(7);
      stop();
      Serial.print("Turning Right");
     }
     else if (sensor_left == 1 && sensor_right == 0)
     {//Left Curve
      turnLeft();
      delay(7);
      stop();
      Serial.print("Turning Left"); 
     }
    } 
   else
    {
      Serial.println("Color detection ERROR!");
    
    }
  }
}
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);
  }
}
//MOTOR
void forward()
{
 digitalWrite(in1, HIGH);
 digitalWrite(in2, LOW);
 analogWrite(enA, speed);

 digitalWrite(in3, HIGH);
 digitalWrite(in4, LOW);
 analogWrite(enB, speed);
}
void backward()
{
 digitalWrite(in1, LOW);
 digitalWrite(in2, HIGH);
 analogWrite(enA, speed);

 digitalWrite(in3, LOW);
 digitalWrite(in4, HIGH);
 analogWrite(enB, speed);
}
void stop()
{
 digitalWrite(in1, LOW);
 digitalWrite(in2, LOW);
 digitalWrite(in3, LOW);
 digitalWrite(in4, LOW);
} 
void turnLeft()
{
 digitalWrite(in1, HIGH);
 digitalWrite(in2, LOW);
 analogWrite(enA, speed);

 digitalWrite(in3, LOW);
 digitalWrite(in4, HIGH);
 analogWrite(enB, speed);

}
void turnRight()
{
 digitalWrite(in1, LOW); //Left Motor backword Pin 
 digitalWrite(in2, HIGH); //Left Motor forword Pin 
 analogWrite(enA, speed);

 digitalWrite(in3, HIGH);
 digitalWrite(in4, LOW);
 analogWrite(enB, speed);
}

Hello

   if (sensor_left ==1 or sensor_right==1)
    {
     Found:
     if (sensor_left == 1 && sensor_right == 1)
     {//forward
      forward();
      delay(7);
      stop();

Regarding the code. What is this "Found:" for? And please increase the time for the delay from 7 (it is in ms) to 500. Just to give the motor more time to run if so.
If motors not running just save the made sketch. Then give a new name and kick all out what is not needed for just a motor moving. Then find out step by step what is working or not. Take a multimeter and measure the voltage at the motors.

Best regards Mascho11

That Found: is the target for a goto

Will you show how your power supply is connected? The power supply should be directly connected to the motor driver board, sharing a ground with the Arduino.

"Found:" is just a label for that section but yah that's not needed. I also increased the delay time, did all like you said and everything worked fine in respective tests. but when i combine motor and color sensor, it didn't work as before and I found that the motors didn't get enough current they get like 2.5V and so. how can I fix this? Should I give the voltage over 12V?

Hi, @derek_jeriyo
Can you please post a copy of your new working code?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Not a Fritzy.

Can you please post some pictures of your project?
Include your power supply.
So we can see your component layout.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.