RFID Again!

If you move the RFID to the hardware serial port, you will need to move the LCD to a software serial port. Again, it's not a big deal.

In loop, you'd check to see if there was data available from the hardware serial port. If not, skip the while loop.

If there is, the while loop only runs until it has read the necessary data.

A while loop runs continuously until the terminating condition is true:

bool understanding = false;
while(!understanding)
{
   bool understoodWhatWasRead = ReadTheManual();
   if(understoodWhatWasRead)
      understanding = true;
}

That was very funny! ;D (bool ==fool)

So I should have left things the way they were and just added the keypad? :-/ I understand the concept I think, if that's true then using the original code it should end the loop after reading the tag yes? it then executes the code which prints the tag # to serial. I should then be able to add the keypad after that correct?

You can, but that would only enable the keypad after an invalid RFID tag was scanned.

I really think that you need to put the RFID reader on the hardware serial port, so that you can (effectively) ask it if it has seen an RFID tag.

Ok then Hardware it is for RFID and Softserial for LCD.

I think I’ve been asking the wrong questions, because I understand the concept, but actually using it seems to be my problem.

First what does “for(int i=0; i<ledcnt; i++)” in this part of the code? I understand it’s increasing i if less than ledcnt. Is this where it’s checking serial?

void setup()
{
Serial.begin(9600) //open serial hardware
RDID.begin(9600) //open serial software

for(int i=0; i<ledcnt; i++)
{
pinmode(LOCK, OUTPUT);
}
}

Also how do I get it to stop long enuff to check the keypad? break;?

The pinMode statement declares that a pin is either INPUT or OUTPUT. In my case I was lighting several LEDs, based on the tag that was scanned, so I put the pin numbers in an array, and used the for loop to declare each pin in the array as an OUTPUT pin.

The serial checking is done in loop, using the while loop. It continually tries to read the serial data. When the RFID.read function actually found data on the serial port, it put it in an array.

What you want to do is add an if statement in loop:

if(Serial.available() > 0)
{
   // Put the code here that actually does the reading (the while loop).
}

If there is no serial data, the block of code is skipped, so you can then check for button presses.

I was just reading up on Serial on the reference page and was thinking serial.available might work! Thanx for the tip! I'll give it a try later today. Have to do some shopping. Merry Xmas!

SubMicro

Well I've only just got started on it but it looks very promissing! :D

I spoke too soon! Technically it works but there is a problem!
When a tag is swiped by it reads

tag: [yyyyyyyyyyyy]
tag number: 0
tag: [3900A502C35D]
tag number 1

etc… etc…

And it takes a few seconds to set the pin HIGH
if you swipe multiple times thinking it didn’t read it rembers and once the pin goes HIGH it stays HIGH for the number of reads registered.
Is there a way to speed things up??

Post your code...

I just got done adding some keypad test code which doesn’t work!

#include <SoftwareSerial.h>
#include <Keypad.h>
#define rxPin 2
#define txPin 3
#define ledpin 19

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’#’,‘0’,’*’}
};

byte rowPins[ROWS] = { 11, 6, 7, 9 }; // Connect keypad ROW0, ROW1, ROW2, ROW3 to these pins.
byte colPins[COLS] = { 10, 12, 8 }; // Connect keypad COL0, COL1 and COL2 to these pins.

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 19

SoftwareSerial LCD = SoftwareSerial(rxPin, txPin); //create a Serial object LCD
char code[20];

int val = 0;
int bytesread = 0;

char Red = “3900A502C35D”; // Define valid tags
char Blue = “39009F2DAD26”; // "
char Yellow = “39005082A843”; // "
char Card1 = “140029A6079C”; // "
char Card2 = “14002943BFC1”; // "

int tagCnt = 5;
int LOCK = 4;

void setup()
{
Serial.begin(9600); //open serial hardware
LCD.begin(9600); //open serial software

pinMode(rxPin, INPUT); //set pin on arduino for receiving RFID data
pinMode(txPin, OUTPUT); //this is not important
pinMode(ledpin, OUTPUT);
digitalWrite(ledpin, HIGH);

for(int i=0; i<tagCnt; i++)
{
pinMode(LOCK, OUTPUT);
}
}

void loop()

{
val = 0;
bytesread = 0;
if (Serial.available() > 0)
while(bytesread < 12)
{

char key = kpd.getKey();
if(key) // same as if(key != NO_KEY)
{
switch (key)
{
case ‘*’:
digitalWrite(ledpin, LOW);
break;
case ‘#’:
digitalWrite(ledpin, HIGH);
break;
default:
Serial.println(key);
}
}

// read 12 digit code

val = Serial.read();
if(val == 3)
{ // if header or stop bytes before the 10 digit reading
break; // stop reading
}

if(val != 2)
{
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
code[bytesread] = ‘\0’; // add the NULL
}
}

if(bytesread >= 12)
{ // if 12 digit read is complete
Serial.print(“Tag: [”);
for(int i=0; i<bytesread; i++)
{
Serial.print(code*);*

  • }*
  • Serial.println("]"); //print the whole 13 bytes*
  • int tag = FindValidTag(code);*
  • Serial.print("Tag number: ");*
  • Serial.println(tag);*
  • digitalWrite(LOCK, LOW);*
  • if(tag > 0 && tag <= tagCnt)*
  • digitalWrite(LOCK, HIGH);*
  • delay(5000);*
  • digitalWrite(LOCK, LOW);*
  • }*
    }
    int FindValidTag(char *code)
    {
  • if(strcmp(code, Red) == 0)*
  • return 1;*
  • else if(strcmp(code, Blue) == 0)*
  • return 2;*
  • else if(strcmp(code, Yellow) == 0)*
  • return 3;*
  • else if(strcmp(code, Card1) == 0)*
  • return 4;*
  • else if(strcmp(code, Card2) == 0)*
  • return 5;*
  • else*
  • return 0;*
    }
    [/quote][/quote]
void loop()

{
  val = 0;
  bytesread = 0;
  if (Serial.available() > 0)
  {
    while(bytesread < 12)
    {
      // read 12 digit code
      
      val = Serial.read();
      if(val == 3)
      { // if header or stop bytes before the 10 digit reading
       break; // stop reading
      }
  
      if(val != 2)
      {
       code[bytesread] = val; // add the digit
       bytesread++; // ready to read next digit
       code[bytesread] = '\0'; // add the NULL
      }
    }

    if(bytesread >= 12)
    { // if 12 digit read is complete
      Serial.print("Tag: [");
      for(int i=0; i<bytesread; i++)
      {
        Serial.print(code[i]);
      }
      Serial.println("]"); //print the whole 13 bytes
      int tag = FindValidTag(code);
      Serial.print("Tag number: ");
      Serial.println(tag);
  
      digitalWrite(LOCK, LOW);
      if(tag > 0 && tag <= tagCnt)
       digitalWrite(LOCK, HIGH);
      delay(5000);
      digitalWrite(LOCK, LOW);
    }
  }
  
  char key = kpd.getKey();
  if(key)  // same as if(key != NO_KEY)
  {
    switch (key)
    {
      case '*':
        digitalWrite(ledpin, LOW);
        break;
      case '#':
        digitalWrite(ledpin, HIGH);
        break;
      default:
        Serial.println(key);
    }
  }
}

All the same statements, different order. Compare this to yours.

The if(serial.available()) block now has {}, and the code to read and process a key FOLLOWS that block. Yours was embedded IN that block.

Thanks I always wondered why there isn’t more on when and how to use brackets (), {}, Arduino is a great platform but the Documentation is designed for programmers not beginners.

OK enuff ranting.
I tried your code but it says
“In function ‘void loop()’:
error a function-definition is not allowed here before ‘{’ token”

It highlights the bracket “{” right after

int FindValidTag(char *code)

Missed a close }. Add one more just before:

  char key = kpd.getKey();

Was not declared in this scope at global scope

int tag = FindValidTag(code);

Also is there a reason you left out a whole section of code?

int FindValidTag(char *code)
{
  if(strcmp(code, Red) == 0)
    return 1;
  else if(strcmp(code, Blue) == 0)
    return 2;
  else if(strcmp(code, Yellow) == 0)
    return 3;
  else if(strcmp(code, Card1) == 0)
    return 4;
  else if(strcmp(code, Card2) == 0)
    return 5;
  else
    return 0;
}

I was only fixing the loop method, not the whole application.

Sorry

what does the error "error. "FindValidTag" was not declared in this scope" mean?? :-/

It means that you are trying to call a function, or use a variable, that has not been defined, or was defined with a different scope.

In this code:

void DoSomething(int a)
{
   if(a > 3)
   {
       int b = a + 7;
   }
   Serial.print("b = ");
   Serial.println(b);
}

You will get an error that says that b is not declared in this scope. The variable is defined only within the portion of the code between the {} of the if statement. It goes out of scope at the end of the if block, so it can not be referenced outside of that block.

So how would I fix that?

Also the project code is functional now with the test keypad code.
It still reports a null tag before reporting the tag scanned.

#include <SoftwareSerial.h>
#include <Keypad.h>
#define rxPin 2
#define txPin 3
#define ledpin 19

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’#’,‘0’,’*’}
};

byte rowPins[ROWS] = { 11, 6, 7, 9 }; // Connect keypad ROW0, ROW1, ROW2, ROW3 to these pins.
byte colPins[COLS] = { 10, 12, 8 }; // Connect keypad COL0, COL1 and COL2 to these pins.

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 19

SoftwareSerial LCD = SoftwareSerial(rxPin, txPin); //create a Serial object LCD
char code[20];

int val = 0;
int bytesread = 0;

char Red = “3900A502C35D”; // Define valid tags
char Blue = “39009F2DAD26”; // "
char Yellow = “39005082A843”; // "
char Card1 = “140029A6079C”; // "
char Card2 = “14002943BFC1”; // "

int tagCnt = 5;
int LOCK = 4;

void setup()
{
Serial.begin(9600); //open serial hardware
LCD.begin(9600); //open serial software

pinMode(rxPin, INPUT); //set pin on arduino for receiving RFID data
pinMode(txPin, OUTPUT); //this is not important
pinMode(ledpin, OUTPUT);
digitalWrite(ledpin, HIGH);

for(int i=0; i<tagCnt; i++)
{
pinMode(LOCK, OUTPUT);
}
}

void loop()

{
val = 0;
bytesread = 0;
if (Serial.available() > 0)
{
while(bytesread < 12)
{
// read 12 digit code

val = Serial.read();
if(val == 3)
{ // if header or stop bytes before the 10 digit reading
break; // stop reading
}

if(val != 2)
{
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
code[bytesread] = ‘\0’; // add the NULL
}
}

if(bytesread >= 12)
{ // if 12 digit read is complete
Serial.print(“Tag: [”);
for(int i=0; i<bytesread; i++)
{
Serial.print(code*);*

  • }*
  • Serial.println("]"); //print the whole 13 bytes*
  • int tag = FindValidTag(code);*
  • Serial.print("Tag number: ");*
  • Serial.println(tag);*
  • digitalWrite(LOCK, LOW);*
  • if(tag > 0 && tag <= tagCnt)*
  • digitalWrite(LOCK, HIGH);*
  • delay(5000);*
  • digitalWrite(LOCK, LOW);*
  • }*
  • }*
  • char key = kpd.getKey();*
  • if(key) // same as if(key != NO_KEY)*
  • {*
  • switch (key)*
  • {*
    _ case ‘*’:_
  • digitalWrite(ledpin, LOW);*
  • break;*
  • case ‘#’:*
  • digitalWrite(ledpin, HIGH);*
  • break;*
  • default:*
  • Serial.println(key);*
  • }*
  • }*
    }
    int FindValidTag(char *code)
    {
  • if(strcmp(code, Red) == 0)*
  • return 1;*
  • else if(strcmp(code, Blue) == 0)*
  • return 2;*
  • else if(strcmp(code, Yellow) == 0)*
  • return 3;*
  • else if(strcmp(code, Card1) == 0)*
  • return 4;*
  • else if(strcmp(code, Card2) == 0)*
  • return 5;*
  • else*
  • return 0;*
    }
    [/quote]

Add this code back in:

int FindValidTag(char *code)
{
  if(strcmp(code, Red) == 0)
    return 1;
  else if(strcmp(code, Blue) == 0)
    return 2;
  else if(strcmp(code, Yellow) == 0)
    return 3;
  else if(strcmp(code, Card1) == 0)
    return 4;
  else if(strcmp(code, Card2) == 0)
    return 5;
  else
    return 0;
}

Then, post the code again, and I'll look at it.