Update code for 3 Way Line Tracking Sensor

Hi hello every body i am from pakistan and i love arduino but it,s too hard to find the arduino boards and components in pakistan i am going to make a Maze solving robot but i am failed to find the Analog reflectance sensor array but good news is i found 3 Way Line Tracking Sensor so now i want to update the code so is it possible …i am introuble i am really in trouble .

code.txt (11.9 KB)

Please post a link to the datasheet for the line tracking sensor you are using.

...R

here is the link of 3 Way Line Tracking Sensor http://proveedor.com.pk/3-Way-Line-Tracking-Following-Sensor-Module-CTRT5000-TCRT5000

There is very little information in that link. It does not even say what each of the pins is for.

Is that all the data you can find?
Can you contact the supplier for more info?

…R

The picture of the back of the board shows the labels for the pins
VCC
GND
L
C
R

From that little information I would guess that the sensors are digital devices. Connecting each of the L, C, R pins to Arduino digital inputs and VCC and GND to the respective Arduino pins would be a start. Write a small program to read L, C and R in turn and base your program on the results assuming that light/dark into each sensor corresponds to HIGH/LOW (or vice versa) on the Arduino inputs.

Abidjamal: here is the link of 3 Way Line Tracking Sensor http://proveedor.com.pk/3-Way-Line-Tracking-Following-Sensor-Module-CTRT5000-TCRT5000

A code example for reading three analogue sensors (center, left, right) can be found here: http://www.azmeer.info/arduino-line-following-robot-part-3/ If you not only want to read the three sensors, but build a complete line-follower robot, perhaps read all 5 parts of the blog.

P.S.: I've just read a description about your module at eBay. Your module provides not analogue output, but already a digital signal for each of the three sensors (left, center, right):

black line output low level white line output high level

So you cannot use your code as "analogRead()" will just provide two values: 0 or 1023.

So you better use digitalRead and create a different programming logic that can deal with digital input values (0 or 1 / LOW or HIGH).

I had not seen the printing on the board.

That suggests that the programming can be very simple using digitalRead() as @Jurs suggests.

Write a short program that displays the three values on the Serial Monitor and experiment by moving the sensor over the line for the maze so you can see how it reacts.

...R

i know it,s digital but the code is for Analog sensor reflectance array can somebody update this code please i tried alot but i am failed

Abidjamal: i know it,s digital but the code is for Analog sensor reflectance array can somebody update this code please i tried alot but i am failed

Rather than asking us to solve it for you. Where are you stuck in changing the code? The goal should be us helping you solve it, so the next time you can do it without our help.

Abidjamal: i know it,s digital but the code is for Analog sensor reflectance array can somebody update this code please i tried alot but i am failed

Have you tried writing the short program I suggested in Reply #6

It would probably only take you 20 or 30 minutes and it would teach you most of what you need to know.

...R

Abidjamal:
i know it,s digital but the code is for Analog sensor reflectance array can somebody update this code please i tried alot but i am failed

You cannot do all at once.

First do a sensor test.

If the sensors work as expected, read sensors and send intended driving directions to Serial.

If that is OK, put it together with the motor control.

I’ve tried something that you can (possibly) use as a base frame for your project:

// connect the sensors to digital pins
#define LEFT_SENSORPIN 2
#define CENTER_SENSORPIN 3
#define RIGHT_SENSORPIN 4

// do you have black line on white ground or white line on black ground?
#define LINE LOW
#define NOLINE HIGH

// define some symbolic constants for our driving directions
enum {GO_AHEAD, GO_LEFT, GO_RIGHT, STOP, GO_POWERLEFT, GO_POWERRIGHT};

void setup() 
{
  Serial.begin(9600);
  Serial.println("*** LINE FOLLOWER ***");
  pinMode(LEFT_SENSORPIN,INPUT);
  pinMode(CENTER_SENSORPIN,INPUT);
  pinMode(RIGHT_SENSORPIN,INPUT);
}

void loop()
{
  // read input from sensors
  byte leftSensor=digitalRead(LEFT_SENSORPIN);
  byte centerSensor=digitalRead(CENTER_SENSORPIN);
  byte rightSensor=digitalRead(RIGHT_SENSORPIN);
  
  // calculate direction
  byte goDirection;
  // if only center sensor detects line, go straight ahead
  if (leftSensor==NOLINE && centerSensor==LINE && rightSensor==NOLINE)
      goDirection= GO_AHEAD;
  // if only left sensor detects line, turn left
  else if (leftSensor==LINE && centerSensor==NOLINE && rightSensor==NOLINE)
      goDirection= GO_LEFT;
  // if only right sensor detects line, turn right    
  else if (leftSensor==NOLINE && centerSensor==NOLINE  && rightSensor==LINE)
      goDirection= GO_RIGHT;
  // if no sensor detects any line: we are either finished or out of control and will stop    
  else if (leftSensor==NOLINE && centerSensor==NOLINE && rightSensor==NOLINE)
      goDirection= STOP;
  // if left and center sensor detect line, must be 90° turn to the left
  else if (leftSensor==LINE && centerSensor==LINE && rightSensor==NOLINE)
      goDirection= GO_POWERLEFT;
  // if right and center sensor detect line, must be 90° turn to the right
  else if (leftSensor==NOLINE && centerSensor==LINE && rightSensor==LINE)
      goDirection= GO_POWERRIGHT;
      
  // Now we have found the direction, 
  // show it on Serial and control motors accordingly
  switch (goDirection)
  {
    case GO_AHEAD: 
      Serial.println("Go ahead");
      // place motor code here
      break;
    case GO_RIGHT:
      Serial.println("Turn right");
      // place motor code here
      break;
    case GO_LEFT:
      Serial.println("Turn left");
      // place motor code here
      break;
    case STOP:
      Serial.println("No line detected - STOP!");
      // place motor code here
      break;
    case GO_POWERLEFT:
      Serial.println("Power-turn 90 degrees left");
      // place motor code here
      break;
    case GO_POWERRIGHT:
      Serial.println("Power-turn 90 degrees right");
      // place motor code here
      break;
  }
}

yes i have a black line on white background Thank you so Much Jur But when Robot detects no line my mean death line so it should turn over by 180 degree and will find a new short path towards a destination .......

Abidjamal: yes i have a black line on white background

OK, in that case comparing the description of your sensor:

black line output low level white line output high level with my declaration in the code:

define LINE LOW

define NOLINE HIGH

will perfectly fit a line-follower following a black line on white ground.

Abidjamal: Thank you so Much Jur But when Robot detects no line my mean death line so it should turn over by 180 degree and will find a new short path towards a destination .......

That sounds as if you want to create something more than just a line-follower.

I think you want a "maze solving robot" or something like that.

Of course, a maze solving robot ist much more complicated than a line-follower robots.

But the enumeration for different driving directions can easily be extended. For example:

enum {GO_AHEAD, GO_LEFT, GO_RIGHT, STOP, GO_POWERLEFT, GO_POWERRIGHT, GO_TURN180};

Then you would use STOP when the maze is solved and the robot has reached the final destination, and in case of a dead end of a line you would use GO_TURN180 instead.

But as I told you before: You cannot do all at once.

The keyword expression is: stepwise refinement

After one step of the program is finished, you step over to the next.