Arduino Forum

Topics => Robotics => Topic started by: MohamedMostafaRagab on Mar 20, 2019, 06:27 pm

Title: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 20, 2019, 06:27 pm
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
Title: Re: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 20, 2019, 06:32 pm
ROBOT
Title: Re: RFID Line Follower Robot
Post by: PaulS on Mar 20, 2019, 06:46 pm
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?
Title: Re: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 21, 2019, 07:39 am
#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);
}
Title: Re: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 21, 2019, 08:53 am
 
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?
Title: Re: RFID Line Follower Robot
Post by: PaulS on Mar 21, 2019, 12:22 pm
Quote
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.
Title: Re: RFID Line Follower Robot
Post by: PaulS on Mar 21, 2019, 12:23 pm
The code you posted, incorrectly, does something, but you failed to say what it actually does or how that differs from what you want.
Title: Re: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 21, 2019, 06:40 pm
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
Title: Re: RFID Line Follower Robot
Post by: vinceherman on Mar 21, 2019, 09:09 pm
Help me out with your overview.
Your original pic
(http://forum.arduino.cc/index.php?action=dlattach;topic=604887.0;attach=300426)

Quote
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.
Title: Re: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 24, 2019, 12:43 am
Help me out with your overview.
Your original pic
(http://forum.arduino.cc/index.php?action=dlattach;topic=604887.0;attach=300426)

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

Title: Re: RFID Line Follower Robot
Post by: neiklot on Mar 24, 2019, 07:22 am
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:

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

Title: Re: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 24, 2019, 11:35 pm
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.
Title: Re: RFID Line Follower Robot
Post by: neiklot on Mar 25, 2019, 06:14 am
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?

Title: Re: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 25, 2019, 12:35 pm
i don't know how to manage the loops for make the robot move to exact location using line follower
Title: Re: RFID Line Follower Robot
Post by: neiklot on Mar 25, 2019, 12:57 pm
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 (https://learn.sparkfun.com/tutorials/sparkfun-line-follower-array-hookup-guide), 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...





Title: Re: RFID Line Follower Robot
Post by: vinceherman on Mar 25, 2019, 01:27 pm
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.
Title: Re: RFID Line Follower Robot
Post by: neiklot on Mar 25, 2019, 01:39 pm
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



Title: Re: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 26, 2019, 09:42 pm


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

 
Code: [Select]
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
Title: Re: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 26, 2019, 10:06 pm
i found this tutorial (https://www.theengineeringprojects.com/2016/08/line-following-robotic-waiter-using-arduino.html) it's nearest thing for what i wanna do

Code: [Select]
#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);
}





 :smiley-confuse:  :smiley-confuse:  :smiley-confuse:  :smiley-confuse:  :smiley-confuse:  :smiley-confuse:  :smiley-confuse:


Title: Re: RFID Line Follower Robot
Post by: neiklot on Mar 27, 2019, 06:18 am
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.

(https://forum.arduino.cc/index.php?action=dlattach;topic=604887.0;attach=301223)

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.


Title: Re: RFID Line Follower Robot
Post by: MohamedMostafaRagab on Mar 30, 2019, 11:08 pm
i started using this code but something happen i can't figure it out

Line Follow
Code: [Select]
#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

int mot1=3;//Left Motor
int mot2=5;//Left Motor
int mot3=6;//Right Motor
int mot4=8;//Right Motor

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

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


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  rfid_setup();
  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() {
  Serial.println("Start Loop");
  Stop(); 
  Serial.println("Robot Stop");
  rfid_scan();
  Serial.println("rfid_scan");

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; crossCount = 0;  Serial.println("crossCheck = 0");}

     
}


void readSensors(){
  rReading = digitalRead (2);
  cReading = digitalRead (4);
  lReading = digitalRead (7);
  Serial.println("rReading = ");Serial.println(rReading);
  Serial.println("cReading = ");Serial.println(cReading);
  Serial.println("lReading = ");Serial.println(lReading);

     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();}
       
else if ((rReading==0)&&(cReading==0)&&(lReading==0)){
        Serial.println("Going STOP to mange crosses");
         Stop();
         delay(100);
         forWard();
         delay(400);
         Tag1Check();
      }
       
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,120);
  digitalWrite (mot2,LOW);
  analogWrite (mot3,120);
  digitalWrite (mot4,LOW);
}

void Back(){
  digitalWrite (mot1,LOW);
  digitalWrite (mot2,HIGH);
  digitalWrite (mot3,LOW);
  digitalWrite (mot4,HIGH);
}

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

void leFt(){
  digitalWrite (mot1,LOW);
  analogWrite (mot2,70);
  analogWrite (mot3,70);
  digitalWrite (mot4,LOW);
  //delay(20);
}

void Right(){
   analogWrite (mot1,70);
   digitalWrite (mot2,LOW);
   digitalWrite (mot3,LOW);
   analogWrite (mot4,70);
   //delay(20);
 
}

void Right90(){
   analogWrite (mot1,100);
   digitalWrite (mot2,LOW);
   digitalWrite (mot3,LOW);
   analogWrite (mot4,70);
   //delay(20);
 }


void Tag1Check(){
 
      if((TargetTag) == (crossCount) == 1){
                 Serial.println("Moving to target tag1");
                 //forWard();
                 //delay(400);
                 Right90();
                 delay(1000);
                 readSensors();}
                 
 else if (((TargetTag) == 2) && ((crossCount) == 3)){
      Serial.println("Searching for target tag2");
      //forWard();
      //delay(800);
      Right90();
      delay(1000);
      readSensors();}     
     
 else if ((TargetTag) == (crossCount) == 3){
      Serial.println("Searching for target tag3");
      //forWard();
      //delay(800);
      Right90();
      delay(1000);
      readSensors();}     
     
     
     
      }
             
 
 



RFID Tab
Code: [Select]
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

String read_rfid;
String rfid_card[] = {"a52cc2ef", "6b376889", "19737828"};

/*
   Initialize.
*/

void rfid_setup() {
  SPI.begin();                // Init SPI bus
  mfrc522.PCD_Init();         // Init MFRC522 card
}

/*
   Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }
}

void rfid_scan() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()){
 
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()){
     
  }
 
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println(read_rfid);
  for (int x = 0; x < 4; x++) {
    if(read_rfid == "a52cc2ef"){
      TargetTag = 1;
      Serial.println("Target Tag = 1 === Go");
      readSensors();
     
     }
else if(read_rfid == "6b376889"){
     TargetTag = 2;
     Serial.println("Target Tag = 2 === Go");
     readSensors();
    }

else if(read_rfid == "19737828"){
     TargetTag = 3;
     Serial.println("Target Tag = 3 === Go");
     readSensors();
     }
  }
}



nedd help please