Line Following Issue (Code Fix?) RESOLVED

I am Attempting to use 3 QRE1113 line following sensors to detect a Black line on a White track. I am not too good at these sort of things so I tried to use comparison logic to make booleans and then used those to evaluate if statements.

#include <Servo.h> // Make code in Servo.h available to this sketch
Servo Left;
Servo Right;
int QRE1113S1_Pin = 0; //connected to analog 0
int QRE1113S2_Pin = 1; //connected to analog 1
int QRE1113S3_Pin = 2; //connected to analog 2
int Left_Pin = 9; // Connected to Digital PWM output
int Right_Pin = 10;// Connected to Digital PWM output
int S1R = 0;
int S2R = 0;
int S3R = 0;

void setup()
{
  Serial.begin(9600);
  Left.attach(Left_Pin);
  Right.attach(Right_Pin);
}

void loop()
{

  int S1 = analogRead(QRE1113S1_Pin);
  Serial.print(S1R); 
  int S2 = analogRead(QRE1113S2_Pin);
  Serial.print(S2R); 
  int S3 = analogRead(QRE1113S3_Pin);
  Serial.println(S3R);
  
  if(S1>500)
  {
    S1R=1;
  }
  if(S2>500)
  {
    S2R=1;
  }
  if(S3>500)
  {
    S3R=1;
  }
  if(S1R==0&S2R==1&S3R==0)
    { 
      Left.write(30);
    // standard drive forward 
      Right.write(150);
    }
    if (S1R==0&S2R==0&S3R==1)
    {
      Left.write(90);
      //veering right. stops left and accelerates right side 
      Right.write(150);
}
  if (S1R==1&S2R==0&S3R==0)
  {
    Left.write(30);
    //veering left. stops right and accelerates left side side to compensate
    Right.write(90);
  }
  if (S1R==1&S2R==1&S3R==0)
  {
    Left.write(90);
    // left turn. stops left side accelerates right side
    Right.write(180);
  }
  if (S1R==0&S2R==1&S3R==1)
  {
    Left.write(0);
    // senses a right turn coming up. stops right side drive train and accelerates left side
    Right.write(90);
  }
}

I know it is a jumbled mess, but I am at a loss. my sensors keep reading (111) in the serial monitor even when I am holding a white piece of paper to a single sensor.

Am I not updating my analogReads in the right scope?

If there is any more information needed just ask and I will provide.

Logical and is && not &.

Mark

thanks. I just noticed that my dingbat self didn't put in the else statements to default the bools to 0. fp

  int S1 = analogRead(QRE1113S1_Pin);
  Serial.print(S1R); 
  int S2 = analogRead(QRE1113S2_Pin);
  Serial.print(S2R); 
  int S3 = analogRead(QRE1113S3_Pin);
  Serial.println(S3R);

Read a value. Print something else. Repeat twice more. Mind if I ask why?

PaulS:

  int S1 = analogRead(QRE1113S1_Pin);

Serial.print(S1R);
  int S2 = analogRead(QRE1113S2_Pin);
  Serial.print(S2R);
  int S3 = analogRead(QRE1113S3_Pin);
  Serial.println(S3R);



Read a value. Print something else. Repeat twice more. Mind if I ask why?

I am printing the boolean for that sensor.
It helps me figure out what state the arduino "thinks" it is in.
Example
010 = straight
100 = left
001= right
I later added a var called statetest where it is changed to a number depending on what state the robot is in.

hmm... my servos aren't even moving at this point. and the statetest var is evaluating to the drive straight case where both servos are getting their max value.

Even made a program that was just to operate servos, worked.
Very concerned over what is inhibiting the Servo movement.
What is preventing the write command???

What is preventing the write command???

You've made some code changes, right? You didn't post that revised code, right? You want us to guess what is still wrong, right? Not me, thanks.

brucecmpblsoup:
I am printing the boolean for that sensor.

Doesn't really answer the question. You're updating one variable and printing another. The variable you print has not been updated. Is there actually intended to be any relationship between the result of the analogRead() and the value you print out? If not, why are you doing them in pairs like this?

//Code for the QRE1113 Analog board from bildr.com
//Outputs via the serial terminal - Lower numbers mean more reflected
#include <Servo.h> // Make code in Servo.h available to this sketch
Servo Left;
Servo Right;
int QRE1113S1_Pin = 0; //connected to analog 0
int QRE1113S2_Pin = 1; //connected to analog 1
int QRE1113S3_Pin = 2; //connected to analog 2
int Left_Pin = 9; // Connected to Digital PWM output
int Right_Pin = 10;// Connected to Digital PWM output
int S1R = 0;
int S2R = 0;
int S3R = 0;
int statetest = 0;

void setup()
{
  Serial.begin(9600);
  Left.attach(Left_Pin);
  Right.attach(Right_Pin);
}

void loop()
{

  int S1 = analogRead(QRE1113S1_Pin);
  Serial.print(S1R); 
  int S2 = analogRead(QRE1113S2_Pin);
  Serial.print(S2R); 
  int S3 = analogRead(QRE1113S3_Pin);
  Serial.println(S3R);
  Serial.println(statetest);
  
  if(S1>500)
  {
    S1R=1;
  }
  else
  {
    S1R=0;
  }
  if(S2>500)
  {
    S2R=1;
  }
  else
  {
    S2R=0;
  }
  if(S3>500)
  {
    S3R=1;
  }
  else
  {
    S3R = 0;
  }
  if(S1R==0&&S2R==1&&S3R==0)
    { 
      statetest = 1;
      Left.write(0);
    // standard drive forward 
      Right.write(180);
    }
    else if (S1R==0&&S2R==0&&S3R==1)
    {
      statetest = 2;
      Left.write(90);
      //veering right. stops left and accelerates right side 
      Right.write(180);
}
  else if (S1R==1&&S2R==0&&S3R==0)
  {
    statetest = 3;
    Left.write(0);
    //veering left. stops right and accelerates left side side to compensate
    Right.write(90);
  }
  else if (S1R==1&&S2R==1&&S3R==0)
  {
    statetest = 4;
    Left.write(90);
    // left turn. stops left side accelerates right side
    Right.write(180);
  }
  else if (S1R==0&&S2R==1&&S3R==1)
  {
    statetest = 5;
    Left.write(0);
    // senses a right turn coming up. stops right side drive train and accelerates left side
    Right.write(90);
  }
  delay(50);
}

Updated.

I do apologize for being difficult, that was not my intention.
I believe I see what you are referring to.
The value of S1R is dependent on the value of S1 compared to my color threshold.
I guess I should move the print command after the comparison logic so the variable is updated correctly.

I am still not getting a PWM from the Arduino to the Servos.
But the servos will turn and be inaccurately mitigated by the sensors while the board is OFF (odd).
Will have a friend with electrical expertise double check everything on the board.

Thank you Holmes, Peter, and Paul for helping and sorry for any inconvenience.

I am still not getting a PWM from the Arduino to the Servos.

I'm not sure why you expect to. The Servos are not controlled by PWM.

  if(S1R==0&&S2R==1&&S3R==0)

Hard to read.

  if(S1R == 0 && S2R == 1 && S3R == 0)

Easier to read.

Have you looked up operator precedence? Which is higher && or ==? Remove that decision from the compilers control:

  if((S1R == 0) && (S2R == 1) && (S3R == 0))

There are no Serial.print() statements in any of the if bodies, so you don't know that values are supposed to be written to the servos. You can fix that.

How are the servos powered?

Servos are powered by 1 6v power supply (4 coupled AA batteries)

The Servos are not controlled by PWM.

They are. They really, really are.
Just not the narrow definition of PWM generated by an unmodified Arduino analogWrite.

int S1 = analogRead(QRE1113S1_Pin);
  Serial.print(S1R);

what is the intention here, please?

To not print the raw input from the sensor but the variable that is the comparison between the raw and the color threshold.
Again, I am not the greatest at optimizing C based language programming (spent half of my High School years engrossed in LabVIEW), but I knew a basic amount about C automation logic ahead of time (RobotC, EasyC).

They are. They really, really are.
Just not the narrow definition of PWM generated by an unmodified Arduino analogWrite.

I'll agree with that. I was just too lazy to type all that out.