Go Down

Topic: Line sensor (5 ir sensors) (Read 622 times) previous topic - next topic

kashika

May 08, 2018, 09:38 pm Last Edit: May 08, 2018, 11:45 pm by kashika
I am building a line following robot that has 5 ir sensors, everything seems to work fine with the code until my robot gets to a turn, instead of following the code and turning it just stops. I believe this has something to do with the code but I can't seem to find the problem. If any of you could find a problem with my code thank you in advance.

Code: [Select]
#include<Servo.h>
Servo motorL;
Servo motorR;
Servo motorP;
const int rsensor = 7;
const int midsensor = 8;
const int lsensor = 12;
const int flsensor = 10;
const int frsensor = 5;
const int rled = A2;
const int gled = A1;
void setup() {
  // put your setup code here, to run once:
  motorL.attach(4);
  motorR.attach(2);
  motorP.attach(3);
  pinMode(rsensor, INPUT);
  pinMode(midsensor, INPUT);
  pinMode(lsensor, INPUT);
  pinMode(frsensor, INPUT);
  pinMode(flsensor, INPUT);
  Serial.begin(9600);

}

void loop() {
  int turns = 0;
  // put your main code here, to run repeatedly:
  while (!digitalRead(midsensor) && !digitalRead(rsensor) && !digitalRead(lsensor) && !digitalRead(flsensor) && !digitalRead(frsensor))
  {
    both_wheel_stop();
  }
 if (digitalRead(midsensor) && digitalRead(lsensor) && digitalRead(rsensor) && digitalRead(frsensor) && digitalRead(flsensor))
  {
    turnLeft();
    turns++;
  }
  else if (digitalRead(midsensor) && digitalRead(lsensor) && digitalRead(flsensor))
  {
    turnLeft();
    turns++;
  }
 else if (digitalRead(midsensor) && digitalRead(rsensor) && digitalRead(frsensor))
  {
    if (turns == 4 && digitalRead(rsensor) && digitalRead(frsensor))
    {
     straight_fast();
    }
    else
    {
      turnRight();
      turns++;
    }
  }
else if (!digitalRead(midsensor) && !digitalRead(rsensor))
  {

    while (!digitalRead(midsensor))
    {
      adjustLeft();
      delay(300);
    }
  }

 else if (!digitalRead(midsensor) && !digitalRead(lsensor))
  {
    while (!digitalRead(midsensor))
    {
      adjustRight();
      delay(300);
    }

  }
 else if(turns == 6)
  {
    for(int k = 0; k <= 2; k++)
    {
      straight_fast();
    }
    motorP.detach();
      motorL.detach();
      motorR.detach();
  }
 else
 {
   straight_slow();
 }
}




void straight_slow()
{
  motorL.write(100);   // Slow speed straight
  motorR.write(80);
}

void straight_fast()
{
  motorL.write(180);   // full speed straight
  motorR.write(0);
}

void both_wheel_stop()
{
  motorL.write(90);   // stop both servo
  motorR.write(90);
}

void adjustLeft()
{
  motorL.write(100);   // Left wheel stop
  motorR.write(90);
  delay(50);
}

void adjustRight()
{
  motorL.write(90);   // right wheel stop
  motorR.write(80);
  delay(50);
}

void turnRight()
{
 while (!digitalRead(midsensor))
  {
    motorL.write(100);
    motorR.write(80);
  }
}

void turnLeft()
{
  while (!digitalRead(midsensor))
  {
    motorL.write(80);
    motorR.write(100);
  }
}

AWOL

Please just post your code in code tags.
People on mobile devices may not be able to open .ino files.

kashika

Sorry about that, I changed it to code tags so it would be more accessible

PaulS

Quote
instead of following the code and turning
I presume that you mean "instead of following the line...".

Think about what "on the line" means, in terms of sensor output. If you put the sensor pins in an array, and read all 5 sensors ONCE on each pass through loop, and put the values in one variable, using bitSet(), it would be MUCH simpler to write your program.

You'd get a value like b00000 (or b11111) when the robot was on (or off) the line. It would be simple to determine whether b00000 or b11111 meant that all sensors were on the line.

Suppose that b11111 meant that all the sensors are on the line. What would b11110 mean? What would b11100 mean? What would b01111 mean? What would b00111 mean?

Obviously, the more 0s on one end means that you are farther from the line. The end with the 0s tells you which way to turn, to get back on the line.

There are only 32 possible values, so a switch statement with 32 cases would cover all possible values.

Now, clearly some of those values, while theoretically possible, are very unlikely. b10001 would mean that the edge sensors are on the line, but the center sensors are not. Very unlikely, so you can safely ignore some cases. A lot of them actually.
The art of getting good answers lies in asking good questions.

kashika

I don't know much about bitSet() and how it works, how would you assign multiple numbers like that to one integer value

AWOL

Do you understand binary numbers?

kashika

Yes, i know about binary numbers, I know they have that same type of outline but i don't know how to assign the values all into one int without adding them

AWOL

Hint:
Code: [Select]
int x =0;
bitWrite(x, 0,1);
bitWrite(x, 2,1);


Will result in the variable x containing the value 5.

Quote
I know they have that same type of outline
I don't understand that phrase.

Go Up