Building a line following robot Need help with programming

 

#define INA_2   10   //This is the left forward control of the robot, I defined each pwm pin that is connected to the arduino

#define INB_2   11   //This is the left backward control of the motor, e.g set this high and INB_1 high and others low you will do a reverse

#define INA_1  5    //This is the right forward control of the motor

#define INB_1  6   //This is the right backward control of the motor

 

 

 

 

void setup() {              // This is always the 1st void function in an arduino program

 

  Serial.begin(9600);

 

 

  pinMode(INA_2, OUTPUT);      //each pwm pin was placed as an output for the program

  pinMode(INB_2, OUTPUT);

  pinMode(INA_1, OUTPUT);

  pinMode(INB_1, OUTPUT);

 

}

 

 

 

void loop() {      //This is a loop shows the sensor and the driver funtion

 

  sensor();

 

  driver();

 

}

 

 

 

 

void driver()       //This is the driver function, it reads the sensors and drives the motor using pwm signal

 

{

 

  int lowVal = 60;            // i did set my sensor low value to be 60 note this is not the lowest value the sensor can go

 

  int highVal = 300;        // i did set my sensor high value to be 300 note this is not the highest value the sensor can go

 

 

 

 

 

  int s0 = analogRead(A0);            //This defines the sensor value s0, it puts the reading of analogread(A0) into s0

 

  int s1 = analogRead(A1);

 

  int s2 = analogRead(A2);

 

  int s3 = analogRead(A3);

 

  int s4 = analogRead(A4);

 

  int s5 = analogRead(A5);

 

 

 

  ////////////////////////////////////////////////

 

  if ((s2 >= highVal) && (s3 >= highVal))    //This is the 1st if statement of the program, it says if s2 and s3 see's a black line robot should move straight at highest speed

  {

    analogWrite(INA_2, 255);

    analogWrite(INB_2, 0);

    analogWrite(INA_1, 255);

    analogWrite(INB_1, 0);

  }

 

 

  else if ((s3 >= highVal) && (s4 >= highVal))   

  {

    analogWrite(INA_2, 200);

    analogWrite(INB_2, 0);

    analogWrite(INA_1,240);

    analogWrite(INB_1, 0);

  }

 

  else if ((s4 >= highVal) && (s5 >= highVal))

  {

    analogWrite(INA_2, 0);

    analogWrite(INB_2, 0);

    analogWrite(INA_1, 200);

    analogWrite(INB_1, 0);

  }

 

  else if((s1 >= highVal) && (s2 >= highVal))

  {

    analogWrite(INA_2, 200);

    analogWrite(INB_2, 0);

    analogWrite(INA_1, 255);

    analogWrite(INB_1, 0);

  }

 

  else if ((s0 >= highVal) && (s1 >= highVal))

  {

    analogWrite(INA_2, 200);

    analogWrite(INB_2, 0);

    analogWrite(INA_1, 0);

    analogWrite(INB_1, 0);

  }

 

 

 

  else if(s0 >= highVal)

 

  {

    analogWrite(INA_2, 200);

    analogWrite(INB_2, 0);

    analogWrite(INA_1,0);

    analogWrite(INB_1, 0);

  }

 

  else if(s1 >= highVal)

 

  {

    analogWrite(INA_2, 200);

    analogWrite(INB_2, 0);

    analogWrite(INA_1,0);

    analogWrite(INB_1, 0);

  }

 

 

  else if(s2 >= highVal)

 

  {

    analogWrite(INA_2, 255);

    analogWrite(INB_2, 0);

    analogWrite(INA_1,255);

    analogWrite(INB_1, 0);

  }

 

 

 

  else if(s3 >= highVal)

 

  {

    analogWrite(INA_2, 255);

    analogWrite(INB_2, 0);

    analogWrite(INA_1,255);

    analogWrite(INB_1, 0);

  }

 

 

  else if(s4 >= highVal)

 

  {

    analogWrite(INA_2, 0);

    analogWrite(INB_2, 0);

    analogWrite(INA_1, 200);

    analogWrite(INB_1, 0);

  }

 

  else if(s5 >= highVal)

 

  {

    analogWrite(INA_2, 0);

    analogWrite(INB_2, 0);

    analogWrite(INA_1,200);

    analogWrite(INB_1, 0);

  }                                                                                                                       

 

    else if((s0 <= lowVal) && (s1 <= lowVal) && (s2 <= lowVal) && (s3 <= lowVal) && (s4 <= lowVal) && (s5 <= lowVal)) //This says if its all white light the robot should make a 180 degree left turn

 

  {

    analogWrite(INA_2, 0);

    analogWrite(INB_2, 200);

    analogWrite(INA_1,200);

    analogWrite(INB_1, 0);

  }

 

 

 

 

}

 

 

 

void sensor(){               //This function basically prints the sensor reading so you can see what sensors are working. 

 

  int s0 = analogRead(A0);

 

  int s1 = analogRead(A1);

 

  int s2 = analogRead(A2);

 

  int s3 = analogRead(A3);

 

  int s4 = analogRead(A4);

 

  int s5 = analogRead(A5);

 

 

 

  Serial.print("s0 ");    //This prints out the reading for each sensor

 

  Serial.print(s0);

 

  Serial.print(" s1 ");

 

  Serial.print(s1);

 

  Serial.print(" s2 ");

 

  Serial.print(s2);

 

  Serial.print(" s3 ");

 

  Serial.print(s3);

 

  Serial.print(" s4 ");

 

  Serial.print(s4);

 

  Serial.print(" s5 ");

 

  Serial.print(s5);

 

  Serial.print(' ');

 

  Serial.println();  

 

}

 

 

https://www.youtube.com/watch?v=ge4Y7HkwRzU

Remember sensors.

Also you will need a battery holder.  Look at the other robots on this site and what they have.  Whats going to move your robot?  You are going to need motors or servos.  Here’s a link to my line follower to help you get ideas, but it also does other things.  https://www.robotshop.com/letsmakerobots/node/29214

Good Luck,

Joe

What I was attempting to explain in the shoutbox

void loop(){

                readSensors();

                // writeSensors();

                if ((rightFarSensor > 900 && rightNearSensor < 40 && rightCenterSensor < 40 &&

leftFarSensor < 40 && leftNearSensor < 40 && leftCenterSensor < 40)

|| (rightFarSensor > 900 && rightNearSensor > 900 && rightCenterSensor < 40 &&

leftFarSensor < 40 && leftNearSensor < 40 && leftCenterSensor < 40))

                                                moveright();

                else if ((rightFarSensor < 40 && rightNearSensor < 40 && rightCenterSensor < 40 &&

                                leftFarSensor > 900 && leftNearSensor > 900 && leftCenterSensor < 40)

                                || (rightFarSensor < 40 && rightNearSensor < 40 && rightCenterSensor < 40 &&

 leftFarSensor > 900 && leftNearSensor < 40 && leftCenterSensor < 40))

                                                moveleft();

                else

                                moveforward();

}

Lets try again.

void loop(){

    int rightSide, leftSide;

    readSensors();

    rightSide = rightFarSensor + rightNearSensor + rightCenterSensor;

    leftSide = leftFarSensor + leftNearSensor + leftCenterSensor;

    // writeSensors();

    if (((rightSide <= 3 * 40) && (leftFarSensor < 40) && (leftSide > 2 * 900))

        || ((rightSide <= 3 * 40) && (leftSide > 2 * 900) && (leftCenterSensor < 40))

        || ((rightSide <= 3 * 40) && (leftSide >= 3 * 900))

        || ((rightSide <= 2 * 40) && (rightCenterSensor > 900) && (leftSide >= 3 * 900))

        || ((rightSide <= 3 * 40) && (leftFarSensor > 900) && (leftSide <= 2 * 40 )))

            moveleft();

    else if (((rightFarSensor < 40) && (rightSide > 2 * 900) && (leftSide <= 3 * 40))

        || ((rightSide > 2 * 900) && (rightCenterSensor < 40) && (leftSide <= 3 * 40))

        || ((rightSide >= 3 * 900) && (leftSide <= 3 * 40))

        || ((rightSide >= 3 * 900) && (leftSide <= 2 * 40 ) && (leftCenterSensor > 900))

        || ((rightFarSensor > 900) && (rightSide <= 2 * 40) && (leftSide <= 3 * 40)))

            moveright();

    else if ((rightSide <= 3 * 40) && (leftSide <= 3 * 40))

            movebackward();

    else

            moveforward();

}

dlbonano,

I can not integrate the new code into your current code, because, the newest code is incomplete.

unsigned int position = qtra.readLine(sensorValues);

The readLine(sensorValues) is a call out to a function that you have not included in your post. I doubt I am skilled enough to integrate the code even if it was available. Making adjustments to if/else statements is one thing. Integrating someone elses code into a current code base is a whole different creature.

Now that you have updated your code again,

is there any possibility we could see a video of what is going on? Your if statements are not written properly.

Your code:
      if((s4&&s5)||(s5)||(s4) > highVal)

What it needs to be, based on what you have:
     if (((s4 > highVal) && (s5 > highVal)) || (s5 > highVal) || (s4 > highVal))

What should work given the code you have written (just my opinion):
     if ((s5 > highVal) || (s4 > highVal))

I have 6 sensors inn a line

I have 6 sensors inn a line am trying to write an if statement for the sensors to read the lines. This is the sensor http://www.pololu.com/catalog/product/960/pictures. I connected the sensors from 0 to 5 in the arduino. It is called s0 to s5 in my program. Am trying to write an if statement for the sensors reading to go straight, left or right. 

Maybe you didn’t understand my response.

Your if statements do not follow the proper syntax.

Proper:
IF (condition) {block code}
*NOTE: condition can cover any amount of equality tests and whatnot, including what you are attempting. 

What you wrote:
IF ((var&&var)||(var)||(var)condition) {block code}

For reference I wrote:
 if (((s4 > highVal) && (s5 > highVal)) || (s5 > highVal) || (s4 > highVal)) 
IF (((condition) && (condition)) || (condition) || (condition)) 

the above two statements are not the same. I am not going to rewrite all of your IF statements to make them correct.

Some of your IF statements are still wrong.

This:
   if ((s2 && s3) >= highVal)

is wrong. It should be:
   if ((s2 >= highVal)&&(s3 >= highVal))

This:
      else if(s5 >= highVal)

is correct. As I see it, only 2 of your if statements are written correctly and therefore they are likely the only responses you are currently getting.
     else if(s5 >= highVal) and else if(s0 >= highVal) are the only two statements that should be functioning. The others are meaningless and should not give a response.