Skip to main content
added 558 characters in body
Source Link
Wardk
  • 21
  • 2

Edit: here's the changes I made to the loop():

void loop() {
  if (Serial.available() > 0)
  {
    ReadSerial(RFIDTAG);
  }
  unsigned long millisScanned;
  if (DisplayTAG != RFIDTAG)
  { 
    DisplayTAG=RFIDTAG;
    tagCompare(RFIDTAG);
    millisScanned = millis();
  }
  unsigned long millisReference = millisScanned + 10000;
  if ((DisplayTAG == RFIDTAG) && (millis() >= millisReference))
  {
    tagCompare(RFIDTAG);
    millisScanned = millis();
  }
}

Edit: here's the changes I made to the loop():

void loop() {
  if (Serial.available() > 0)
  {
    ReadSerial(RFIDTAG);
  }
  unsigned long millisScanned;
  if (DisplayTAG != RFIDTAG)
  { 
    DisplayTAG=RFIDTAG;
    tagCompare(RFIDTAG);
    millisScanned = millis();
  }
  unsigned long millisReference = millisScanned + 10000;
  if ((DisplayTAG == RFIDTAG) && (millis() >= millisReference))
  {
    tagCompare(RFIDTAG);
    millisScanned = millis();
  }
}
Source Link
Wardk
  • 21
  • 2

Arduino RFID tag scanning problems

For a school project, I'm using an Arduino Uno and a Parallax RFID together with some other components, I used a script I got from Instructables for reading my serial input from the RFID (link: http://www.instructables.com/id/Wiring-and-programming-the-Parallax-RFID-Serial-Ca/) To use this serial data, you need to include an if loop in your loop function to check if the scanned tag equals the previous one. If it doesn't, the tag gets used further in the program. This doesn't allow scanning the same tag twice in a row. But I need that for my project. I have tried to work around it, but I can't seem to get it working, I always create a loop of unlimited scans or some other inregularities. Here's my code:

/* Libraries that need to be manually installed:
Blynk libraries: https://github.com/blynkkk/blynk-library/releases/download/v0.5.1/Blynk_Release_v0.5.1.zip
LiquidCrystal_I2C library: https://cdn.instructables.com/ORIG/FVH/K8OQ/J8UH0B9U/FVHK8OQJ8UH0B9U.zip
 */
#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <ESP8266_Lib.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
//#include <ThingSpeak.h>

//Setting up the Blynk wifi connection
#define ESP8266_BAUD 9600
char auth[] = "auth";

//Setting up the virtual pins
WidgetTerminal terminal(V1);
BLYNK_WRITE(V1){}
int freeSpots = 5;
BLYNK_READ(V2) 
{
  Blynk.virtualWrite(V2, freeSpots); 
}

//Setting up the RFID
#define RFIDEnablePin 8
#define RFIDSerialRate 2400 
String RFIDTAG=""; //Holds the RFID Code read from a tag
String DisplayTAG = ""; //Holds the last displayed RFID Tag

//Setting up the LCD
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  
//Setting up the serial connection
SoftwareSerial EspSerial(2, 3);
ESP8266 wifi(&EspSerial);

//Setting up the servo
Servo slagboom;

//Tag database
char tags[7][5] = {"6196", "6753", "5655", "69EC", "9FFC"};
char owners[7][5] = {"per1", "per2", "per3", "per4", "per5"};
int statuses[7] ={0};

//unsigned long channelNumber = 461353;
//const char * APIKey = "IHFFREPE9XUNSN94";

void setup() {
  //Serial communication
  Serial.begin(RFIDSerialRate);
  EspSerial.begin(ESP8266_BAUD);
  //RFID pin setup
  pinMode(RFIDEnablePin,OUTPUT);
  digitalWrite(RFIDEnablePin, LOW);
  //Blynk startup
  Blynk.begin(auth, wifi, "ssid", "pass");
  //LCD startup
  lcd.begin(16,2);//16 kolommen, 2 rijen
  lcd.backlight();
  //Servo startup
  slagboom.attach(10);
  slagboom.write(35);
  
  //ThingSpeak.begin(client);
  
  //Blynk terminal printing test
  delay(1000);
  slagboom.detach();
  terminal.println(F("Terminal printing succesfull"));
  terminal.flush();
 
}

void loop() {
  if (Serial.available() > 0)
  {
    ReadSerial(RFIDTAG);
  }
  if (DisplayTAG != RFIDTAG)
  { 
    DisplayTAG=RFIDTAG;
    tagCompare(RFIDTAG);
  }
}


//Function for comparing the tag to the database
void tagCompare(String tagToCompare)
{
  char lastTag[5];
  tagToCompare.toCharArray(lastTag, 5);
  int i = 0;
  while (i < 5)
    {
      if (strcmp(tags[i], lastTag) == 0)
        {
          terminal.print(owners[i]);
          if (statuses[i] == 0)
          {
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("Welkom, ");
            lcd.setCursor(0,1);
            lcd.print(owners[i]);
            statuses[i] = 1;
            freeSpots--;
            digitalWrite(RFIDEnablePin, HIGH);
            slagboom.attach(10);
            slagboom.write(145);
            delay(1000);
            digitalWrite(RFIDEnablePin, LOW);
            slagboom.write(35);
            delay(500);
            slagboom.detach();
            terminal.println(F(" heeft de parking betreden."));
            terminal.flush();
          }
          else
          {
            lcd.clear();
            lcd.setCursor(0,0);
            lcd.print("Tot ziens ");
            lcd.setCursor(0,1);
            lcd.print(owners[i]);
            statuses[i] = 0;
            freeSpots++;
            digitalWrite(RFIDEnablePin, HIGH);
            slagboom.attach(10);
            slagboom.write(145);
            delay(1000);
            digitalWrite(RFIDEnablePin, LOW);
            slagboom.write(35);
            delay(500);
            slagboom.detach();
            terminal.println(F(" heeft de parking verlaten."));
            terminal.flush();
          }
          //ThingSpeak.writeField(channelNumber, 1, i, APIKey);
          Blynk.run();
        }
      i++;
      }
  i = 0;
}

//Function for reading the tag 
void ReadSerial(String &ReadTagString)
{
  int bytesread = 0;
  int val = 0; 
  char code[10];
  String TagCode="";
  
  if(Serial.available() > 0)
  {  
    if((val = Serial.read()) == 10)
    {
      bytesread = 0; 
      while(bytesread<10) // Reads the tag code
      { 
        if( Serial.available() > 0) 
        { 
          val = Serial.read(); 
          if((val == 10)||(val == 13)) // If header or stop bytes before the 10 digit reading
          {  
            break; // Stop reading 
          } 
          code[bytesread] = val; // Add the digit 
          bytesread++; // Ready to read next digit 
        } 
      } 
      if(bytesread == 10)  // If 10 digit read is complete
      {
        for(int x=6;x<10;x++) //Copy the Chars to a String
        {
          TagCode += code[x];
        }
        ReadTagString = TagCode; //Returns the tag ID
        while(Serial.available() > 0) //Burn off any characters still in the buffer
        {
        Serial.read();
        }
        
      } 
      bytesread = 0;
      TagCode="";
    } 
  } 
}

Any help would be very much appreciated. Thanks in advance.