Pages: [1]   Go Down
Author Topic: Line Following Issue (Code Fix?) RESOLVED  (Read 445 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
#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.
« Last Edit: December 09, 2012, 08:19:16 pm by brucecmpblsoup » Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2302
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Logical and is && not &.

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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???
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
  if(S1R==0&&S2R==1&&S3R==0)
Hard to read.

Code:
  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:
Code:
  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?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26302
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
int S1 = analogRead(QRE1113S1_Pin);
  Serial.print(S1R);
what is the intention here, please?
« Last Edit: December 09, 2012, 02:19:55 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: