0

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.

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();
  }
}
  • 1
    Can you describe in more detail what you mean by "I always create a loop of unlimited scans or some other inregularities."? Also, the code you posted as "yours" doesn't seem to have had any changes that would avoid checking the new value against the previous, so what exactly did you modify to try to work around it? – jose can u c Mar 28 '18 at 15:03
  • I have tried using the millis function to allow the tagCompare function to be used when DisplayTAG equald RFIDTAG. I did this using the '&&' modifier but it always seemed to ignore the millis function. I also tried changing the previously mentioned values but that executed the function twice for every tag scanned. I left the wrong code out of my question, but I could rewrite it for you if necessary. – Wardk Mar 28 '18 at 15:40
  • Please do put in the code you tried. "using the millis function" doesn't explain how you used it, etc. – jose can u c Mar 28 '18 at 16:38
  • @josecanuc, I added the code to the main question. – Wardk Mar 28 '18 at 17:53
  • But I need that for my project. .... what are you talking about here? – jsotola Mar 28 '18 at 20:10
1

If you wish to allow even for duplicate scans, but only want to check it if the RFID reader returns a new scan string, then you don't even need millis()

void loop() {
  static bool pendingTag = false;
  if (Serial.available() > 0)
  {
    ReadSerial(RFIDTAG);
    pendingTag = true;   // Indicate that this is a new scan to check
  }
  if (pendingTag)
  {
    tagCompare(RFIDTAG);
    pendingTag = false;  // The tag is checked, no need to check again
    DisplayTAG=RFIDTAG;  // Not even sure if you use this anywhere else.
  }
}
  • Thanks for the reply! Unfortunately this doesn't quite work. I'm able to scan the same tag twice, but it randomly opens multiple times for each tag scanned. Any way to fix this? – Wardk Mar 29 '18 at 13:16
  • I can't give a fix based on that comment. You have to track down what is triggering that, because it's likely not truly random. The code in the answer says to call tagCompare() if there was data on the serial port, essentially. What else might put data on the serial port? – jose can u c Mar 29 '18 at 13:23
  • The only thing connected to the serial port is the RFID-reader and I have tried clearing RFIDTAG after comparing it, this nearly fixes the problem, but occasionally tagCompare() still gets called twice. – Wardk Mar 29 '18 at 13:25
  • So... add some debugging statements to print what comes in on the serial port so you can figure out what's going on. No one here can guess what is happening "randomly" on your hardware setup. You have to dig deep into your setup and figure out what is going on. – jose can u c Mar 29 '18 at 13:40
  • I think the RFID holds the tag that was last scanned, so it just keeps on sending the same tag to the serial port, I have no clue on how to fix this, so I'm just going to require another tag to be scanned before the first one can be scanned again. Thanks for your help anyway. – Wardk Mar 29 '18 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.