Go Down

Topic: RFID Line Follower Robot (Read 842 times) previous topic - next topic

vinceherman

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.

neiklot

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




MohamedMostafaRagab



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

MohamedMostafaRagab

i found this tutorial 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:



neiklot

#19
Mar 27, 2019, 06:18 am Last Edit: Mar 27, 2019, 06:19 am by neiklot
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.



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.



MohamedMostafaRagab

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

Go Up