RFID Line Follower Robot

Dear All Have A nice Day
i create anew robot 2 WD and RC522 Rfid Module and PNC532 NFC Module
he is the procedure i need
insert tag no 1 to RC522 RFID Reader so the robot start to move using line follower till achieve the target tag on the floor (floor tag scanned using NFC Reader)
as the shown in the photo attached
Please Help Me in coding
BR

ROBOT

What have you tried so far?

Can you read the tag(s)? Can you read the line sensors? Can you follow the line? Can you read a tag at whatever speed the device is following the line?

#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
#include <NfcAdapter.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
//RC522

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN);

byte card_ID[4];
byte First_card_ID[4]={0x6B,0x37,0x68,0x89};
byte Second_card_ID[4]={0x19,0x73,0x78,0x28};
//byte Third_card_ID[4]={0x6B,0x37,0x68,0x89};
//byte Fourth_card_ID[4]={0x9E,0xDA,0x45,0x73};

boolean State=false;
uint8_t i=0;

int mot1=3;
int mot2=5;
int mot3=6;
int mot4=8;

int r = 2;
int c = 4;
int l = 7;

int lReading;
int cReading;
int rReading;

void setup(void) {

Serial.begin(115200);
Serial.println(“Hello!”);
//==================RC Set UP
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init();

//==================NFC Set UP
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print(“Didn’t find PN53x board”);
while (1); // halt
}
// Got ok data, print it out!
Serial.print(“Found chip PN5”); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print(’.’); Serial.println((versiondata>>8) & 0xFF, DEC);
nfc.setPassiveActivationRetries(0xFF);
nfc.SAMConfig();

Serial.println(“Waiting for Tag To Start”);

pinMode(r, INPUT);
pinMode(c, INPUT);
pinMode(l, INPUT);

pinMode(mot1,OUTPUT);
pinMode(mot2,OUTPUT);
pinMode(mot3,OUTPUT);
pinMode(mot4,OUTPUT);
}

void loop(void) {

cmp_id();

if(State==true){ Serial.println(“Accepted, Valid card”);
Serial.println(“car move”);
forWard();
// readSensors();
// if ((rReading==1)&&(cReading==0)&&(lReading==1))// forword
// { Serial.println(“Going Forward”);
// forWard();}
// else if ((rReading==0)&&(cReading==1)&&(lReading==1)){
// Serial.println(“Going RIGHT”);
// Right();}
//
// else{
// Serial.println(“STOP”);
// Stop();}

}
NFC();
if(State==false){Serial.println("denied! unValid card ! ");
Stop();
}

}

void cmp_id(){

if ( ! mfrc522.PICC_IsNewCardPresent()) {
return 0;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return 0;
}

for (byte i = 0; i < 4; i++) {
card_ID_=mfrc522.uid.uidByte*;_
_
}_
Serial.print(F("Rover Reader: "));
_
printDec(mfrc522.uid.uidByte, mfrc522.uid.size);_
_
Serial.println();_
mfrc522.PICC_HaltA();
mfrc522.PCD_StopCrypto1();*

if(card_ID==First_card_ID*){
_ State=true;
}
// else{
// State=false;
// i=5;
// }
return 1;
}
//==============NFC*

void NFC(){
* boolean success;_
uint8_t uid = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID*

* uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);*

* if (success) {*
* Serial.println(“Found a card!”);*
* Serial.print("UID Value: ");*
* for (uint8_t i=0; i < uidLength; i++)
_ {
Serial.print(" 0x");Serial.print(uid, HEX);
}
Serial.println("");
// Wait 1 second before continuing*

* //delay(1);
}
// else*

// {
// // PN532 probably timed out waiting for a card
// Serial.println(“Timed out waiting for a card”);
// }
if((uid)==Second_card_ID*){*
* Serial.println(“Stop Car for tag1”);
State=false;
return;}
}
//Print DEC Loop*

void printDec(byte *buffer, byte bufferSize) {
* for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer < 0x10 ? " 0" : " 0x");
Serial.print(buffer, HEX);
}
}
void readSensors(){
rReading = digitalRead (2);
cReading = digitalRead (4);
lReading = digitalRead (7);
Serial.print("rReading = "); Serial.println(rReading);
Serial.print("cReading = "); Serial.println(cReading);
Serial.print("lReading = "); Serial.println(lReading);
}
void forWard(){
analogWrite (mot1,140);
analogWrite (mot2,LOW);
analogWrite (mot3,140);
analogWrite (mot4,LOW);
}
void Stop(){
analogWrite (mot1,LOW);
analogWrite (mot2,LOW);
analogWrite (mot3,LOW);
analogWrite (mot4,LOW);
}
void leFt(){
analogWrite (mot1,LOW);
analogWrite (mot2,100);
analogWrite (mot3,100);
analogWrite (mot4,LOW);
}
void Right(){
analogWrite (mot1,100);
analogWrite (mot2,LOW);
analogWrite (mot3,LOW);
analogWrite (mot4,100);
}*_

PaulS:
What have you tried so far?

Can you read the tag(s)? Can you read the line sensors? Can you follow the line? Can you read a tag at whatever speed the device is following the line?

at first i have delay problem for reading RC tags
line follower delay
need to excite route to run through if you got me
can you help me?

can you help me?

No, because you can't follow the rules. EVERY section of the forum has a sticky called "How to use this forum - Please read". You obviously didn't.

Before you post code again, CORRECTLY this time, put EVERY { and EVERY } on a line BY ITSELF, and use Tools + Auto Format to fix the indenting, AND delete commented out code.

The code you posted, incorrectly, does something, but you failed to say what it actually does or how that differs from what you want.

PaulS:
No, because you can't follow the rules. EVERY section of the forum has a sticky called "How to use this forum - Please read". You obviously didn't.

Before you post code again, CORRECTLY this time, put EVERY { and EVERY } on a line BY ITSELF, and use Tools + Auto Format to fix the indenting, AND delete commented out code.

Dear Sir,
I'll check now the section "How to use this forum - Please read"
i plan to do this steps for my project

1- the robot is stopped at the start point

2- when i put the first tag (for RC522 RFID Module) the robot start moving and go to the target tag on the floor (NFC Module Scan and the robot Stop).

3- and so on

the steps i proceed with
start reading from the (RFID) the robot move forward
NFC Scan the Target Tag The Stop
in this stage i noticed that ther are delay for (RFID) scan

i can't proced for the next stage Make the line follow active and Write correct steps to the route attached in the above photo

So, if you can help me i really appreciate that
thanks again

Help me out with your overview.
Your original pic
tags.png

2- when i put the first tag (for RC522 RFID Module) the robot start moving and go to the target tag on the floor (NFC Module Scan and the robot Stop).

As I understand your description of this step, you mean that
The vehicle should be at a stop when powered up.
The vehicle should remain at a stop until is reads a card (you said i put the first card).
After a card read, the vehicle should start the line following logic common to all line followers.

Your image shows tags 1, 2 and 3.
Are these tags places so that your card reader can read them while it is moving?
Should the vehicle do something after it reads a tag?
Should the vehicle stop at some point?

Edit: A good place to start with this project is to completely ignore your card reader and work on making the vehicle work as a line follower.

vinceherman:
Help me out with your overview.
Your original pic
tags.png

As I understand your description of this step, you mean that
The vehicle should be at a stop when powered up.======= yes
The vehicle should remain at a stop until is reads a card (you said i put the first card).==== yes
After a card read, the vehicle should start the line following logic common to all line followers. ==== the vehicle go to the exact target tag if the insert tag is 1 it go through the route to achieve the target tag no1 on the floor

Your image shows tags 1, 2 and 3. ===== yes shown the target location
Are these tags places so that your card reader can read them while it is moving?===== yes
Should the vehicle do something after it reads a tag? need to make it stop and if that possible to make the robot back that will be great
Should the vehicle stop at some point? yes

Edit: A good place to start with this project is to completely ignore your card reader and work on making the vehicle work as a line follower.===== agree for your advice but i can’t figure that making the robot go through exact route

waiting your advice

OP all you did in that last post was quote vinceherman's previous post back at us and add "waiting your advice" (inside the quote, which confused me for a bit.)

But he had asked you a few questions, which you didn't answer:

vinceherman:
Your image shows tags 1, 2 and 3.
Are these tags places so that your card reader can read them while it is moving?
Should the vehicle do something after it reads a tag?
Should the vehicle stop at some point?

Apart from the fact that it's not clear what you actually want your system to do, it's not clear what it actually does so far, and how that differs from the requirement.

(I'm guessing this is a uni/college project?)

Dear All
Many thanks for your support

you can imagine my project like a waiter line follower robot
at the start the robot is stop moving in the start position
when we call tag no1 (RFID module) read the tag
the robot start moving to achieve table no 1 (he know the route)
when it arrive to the table he confirm arriving by Reading the table tag (NFC Module)
then it return back to the start point again and waiting another tag to scan.

neiklot:
Apart from the fact that it’s not clear what you actually want your system to do, it’s not clear what it actually does so far, and how that differs from the requirement.

Ok so now we know what it’s supposed to do; what does it actually do and how is that wrong?

i don't know how to manage the loops for make the robot move to exact location using line follower

MohamedMostafaRagab:
i don't know how to manage the loops for make the robot move to exact location using line follower

Have you yet got it set up and following just a line with a turn?.... never mind making decisions right now.

Have you followed any of the tutorials like this one at sparkfun, just as an example.

Once you have it following a path, you can think about how to make it decide to turn right at the first intersection or the second one...

MohamedMostafaRagab:
Edit: A good place to start with this project is to completely ignore your card reader and work on making the vehicle work as a line follower.===== agree for your advice but i can't figure that making the robot go through exact route

Don't make the robot go the exact route.
Start out by making the robot follow a line.

Ignore the card reader.

Ignore everything else.

Just make it follow the line.

This will confirm that your hardware is connected properly and that you have a simple line follower sample working.

Do that. JUST the line follower. Then come back and we can work with you on the next steps.

I agree... then give it the ability to turn right at the first intersection, or go straight.... implicit in that is that you need to add some logic to your sensors to determine it's actually at an intersection, let alone figure out how to make it turn

i do a small code for line follower and it works great make the robot follow the line

int mot1=3;
int mot2=5;
int mot3=6;
int mot4=8;

int r = 2;
int c = 4;
int l = 7;
int Sensor3 = A0;

int crossCount = 0;
int crossCheck = 0;
int TagCheck = 10;
int lReading;
int cReading;
int rReading;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(r, INPUT);
  pinMode(c, INPUT);
  pinMode(l, INPUT);
  pinMode(Sensor3, INPUT);
  pinMode(mot1,OUTPUT);
  pinMode(mot2,OUTPUT);
  pinMode(mot3,OUTPUT);
  pinMode(mot4,OUTPUT);
}

void loop() {
 

if((digitalRead(Sensor3) == LOW) && (crossCheck == 0)){crossCount++; crossCheck = 1;Serial.println("crossCount=");Serial.println(crossCount);Serial.println("crossCheck = 1");}
if((digitalRead(Sensor3) == HIGH) && (crossCheck == 1)){crossCheck = 2;Serial.println("crossCheck = 2");}
if((digitalRead(Sensor3) == LOW) && (crossCheck == 2)){crossCount++; crossCheck = 3;Serial.println("crossCount=");Serial.println(crossCount);Serial.println("crossCheck = 3");}
if((digitalRead(Sensor3) == HIGH) && (crossCheck == 3)){crossCheck = 4;Serial.println("crossCheck = 4");}
if((digitalRead(Sensor3) == LOW) && (crossCheck == 4)){crossCount++; crossCheck = 5;Serial.println("crossCount=");Serial.println(crossCount);Serial.println("crossCheck = 5");}
if((digitalRead(Sensor3) == HIGH) && (crossCheck == 5)){crossCheck = 0;  Serial.println("crossCheck = 0");}


readSensors();      
}


void readSensors(){
  rReading = digitalRead (2);
  cReading = digitalRead (4);
  lReading = digitalRead (7);

     if ((rReading==1)&&(cReading==0)&&(lReading==1)){
        Serial.println("Going Forward");
        forWard();}
else if ((rReading==0)&&(cReading==1)&&(lReading==1)){ 
        Serial.println("Going RIGHT");
        Right();delay(10);}
else if ((rReading==0)&&(cReading==0)&&(lReading==0)){ 
        Serial.println("Going RIGHT");
        Stop();        
        Right();}
else if ((rReading==0)&&(cReading==0)&&(lReading==1)){ 
        Serial.println("Going RIGHT");
        Right();}
else if ((rReading==1)&&(cReading==0)&&(lReading==0)){ 
        Serial.println("Going LEFT");
        leFt();}
else if ((rReading==1)&&(cReading==1)&&(lReading==0)){ 
        Serial.println("Going LEFT");
        leFt();}
else if ((rReading==1)&&(cReading==1)&&(lReading==1)){ 
        Serial.println("Going STOP");
        Stop();}
}

void forWard(){
  analogWrite (mot1,140);
  analogWrite (mot2,LOW);
  analogWrite (mot3,140);
  analogWrite (mot4,LOW);
}

void Back(){
  analogWrite (mot1,LOW);
  analogWrite (mot2,140);
  analogWrite (mot3,LOW);
  analogWrite (mot4,140);
}

void Stop(){
  analogWrite (mot1,0);
  analogWrite (mot2,0);
  analogWrite (mot3,0);
  analogWrite (mot4,0);
}

void leFt(){
  analogWrite (mot1,LOW); 
  analogWrite (mot2,100);
  analogWrite (mot3,100);
  analogWrite (mot4,LOW); 
}

void Right(){
   analogWrite (mot1,100); 
   analogWrite (mot2,LOW);
   analogWrite (mot3,LOW);
   analogWrite (mot4,100);
   
}

now it’s ok for me as line follow robot
i add sensor 3 to make across count maybe to use it in the logic i’m trying to figure

and i appreciate your help truly

what’s the next step

i found this tutorial it's nearest thing for what i wanna do

#define motorL1 8
#define motorL2 9
#define motorR1 10
#define motorR2 11

#define PwmLeft 5
#define PwmRight 6

#define SensorR 2
#define SensorL 3
#define Sensor3 A0
#define Sensor4 A1

#define TableA A4
#define TableB A2
#define TableC A5
#define TableD A3

int OriginalSpeed = 200;
int TableCount = 0;
int TableCheck = 0;
int RFCheck = 10;

void setup() 
{
  Serial.begin (9600);
 
  pinMode(motorR1, OUTPUT);
  pinMode(motorR2, OUTPUT);
  pinMode(motorL1, OUTPUT);
  pinMode(motorL2, OUTPUT);
  
  pinMode(PwmLeft, OUTPUT);
  pinMode(PwmRight, OUTPUT);
  
  pinMode(SensorL, INPUT);
  pinMode(SensorR, INPUT);
  pinMode(Sensor3, INPUT);
  pinMode(Sensor4, INPUT);
  
  pinMode(TableA, INPUT);
  pinMode(TableB, INPUT);
  pinMode(TableC, INPUT);
  pinMode(TableD, INPUT);
  
  MotorsStop();
  
  analogWrite(PwmLeft, 0); 
  analogWrite(PwmRight, 0);
  delay(2000); 
 // Serial.println("fghfg");
  
}

void loop() {
  MotorsForward();
if((digitalRead(Sensor3) == LOW) && (TableCheck == 0)){TableCount++; TableCheck = 1;}
if((digitalRead(Sensor3) == HIGH) && (TableCheck == 1)){TableCheck = 2;}

if((digitalRead(Sensor3) == LOW) && (TableCheck == 2)){TableCount++; TableCheck = 3;}
if((digitalRead(Sensor3) == HIGH) && (TableCheck == 3)){TableCheck = 4;}
if((digitalRead(Sensor3) == LOW) && (TableCheck == 4)){TableCount++; TableCheck = 5;}
if((digitalRead(Sensor3) == HIGH) && (TableCheck == 5)){TableCheck = 0;}

if(digitalRead(TableA) == HIGH){RFCheck = 1;}
if(digitalRead(TableB) == HIGH){RFCheck = 2;}
if(digitalRead(TableC) == HIGH){RFCheck = 3;}
if(digitalRead(TableD) == HIGH){RFCheck = 4;}

if(RFCheck == TableCount){Table1();}
PIDController();
}

void MotorsBackward()
{
    digitalWrite(motorL1, HIGH);
    digitalWrite(motorL2, LOW);
    digitalWrite(motorR1, HIGH);
    digitalWrite(motorR2, LOW);
}

void MotorsForward()
{
    digitalWrite(motorL1, LOW);
    digitalWrite(motorL2, HIGH);
    digitalWrite(motorR1, LOW);
    digitalWrite(motorR2, HIGH);
}

void MotorsStop()
{
    digitalWrite(motorL1, HIGH);
    digitalWrite(motorL2, HIGH);
    digitalWrite(motorR1, HIGH);
    digitalWrite(motorR2, HIGH);
}

void MotorsLeft()
{
    analogWrite(PwmLeft, 0); 
  analogWrite(PwmRight, 0);
    digitalWrite(motorR1, HIGH);
    digitalWrite(motorR2, HIGH);
    digitalWrite(motorL1, LOW);
    digitalWrite(motorL2, HIGH);
}

void MotorsRight()
{
      analogWrite(PwmLeft, 0); 
  analogWrite(PwmRight, 0);
    digitalWrite(motorR1, LOW);
    digitalWrite(motorR2, HIGH);
    digitalWrite(motorL1, HIGH);
    digitalWrite(motorL2, HIGH);
}

void Motors180()
{
    analogWrite(PwmLeft, 0); 
    analogWrite(PwmRight, 0);
    digitalWrite(motorL1, HIGH);
    digitalWrite(motorL2, LOW);
    digitalWrite(motorR1, LOW);
    digitalWrite(motorR2, HIGH);
}

void PIDController()
{
  if(digitalRead(SensorL) == HIGH){analogWrite(PwmRight, 250);analogWrite(PwmLeft, 0);}
  if(digitalRead(SensorR) == HIGH){analogWrite(PwmLeft, 250);analogWrite(PwmRight,0);}
  if((digitalRead(SensorL) == LOW) && (digitalRead(SensorR) == LOW)){analogWrite(PwmRight, 0);analogWrite(PwmLeft, 0);}
}

void Table1()
{
   TableCount = 0;
   MotorsRight();
   delay(4000);
   while(digitalRead(SensorR) == HIGH);
   while(digitalRead(SensorL) == HIGH);
   while(1)
   {
       MotorsForward();
       PIDController();
       if(digitalRead(Sensor3) == LOW){break;}
   }
   MotorsStop();
   delay(1000);
   Motors180();
   delay(2000);
   while(digitalRead(Sensor3) == HIGH);
   while(digitalRead(Sensor3) == LOW);
   while(digitalRead(SensorL) == HIGH);
   //delay(500);
   while(1)
   {
   MotorsForward();
   PIDController();
   if(digitalRead(Sensor3) == LOW){break;}
   }
   //delay(1000);
   MotorsLeft();
   delay(4000);
   while(digitalRead(SensorL) == HIGH);
   while(digitalRead(SensorR) == LOW);
   while(1)
   {
       MotorsForward();
       PIDController();
       if(digitalRead(Sensor3) == LOW){break;}
   }
   MotorsStop();
   delay(1000);
}

:confused: :confused: :confused: :confused: :confused: :confused: :confused:

MohamedMostafaRagab:
i do a small code for line follower and it works great make the robot follow the line
...
...
what's the next step

I would say you need to figure out how the robot will recognise an intersection. At some stage it will need to decide to go straight or turn right based on what the destination is, but first it needs to know that it can turn or go straight.

Eventually, somewhere in the logic, if it's going to tag1 it will have an instruction to take the first right or if it's headed to tag2, to ignore the first right.

Before you do that, it needs to know what makes an intersection differ from a normal bit of line. Perhaps the sensors being on or off the line? Think what the sensors are telling the robot. Are there enough sensors? Can it reliably report back to you that it arrived at an intersection, never mind the decision making of should it turn or not. Then perhaps have a counter of some description, so it can keep track of which intersection it's at.

intersection.GIF

Hint: how would you instruct a friend in words if you stood him or her on the start point?

That's just a thought... you should be doing similar thinking. This is one of those cases where you need to think of a strategy, then code it up to test. Perhaps the strategy is pretty good, and just needs cleaning up. Perhaps it's a crap strategy and needs to be thrown away and you need a complete rethink.

Don't fall into the trap of throwing code at the problem when you haven't done enough thinking.

intersection.GIF