Respected members,
I had modified a code that i have attached to this post and the modified code is as written below.
This code is to read a RFID tag using an EM18 RFID module and shows a necessary output.
This code compiles but does not give the necessary output as the original code. Can you kindly point out the mistakes that i have made while modifying it?
Please help.
char tag[] ="1C002492359F";
char input[12];
int count=0;
boolean flag=0;
int led=11;
int led2=8;
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop()
{
if(Serial.available())
{
count=0;
flag=1;
while(count<12 && flag!=0)
{
input[count] = Serial.read();
if(input[count] == tag[count])
flag=1;
else
flag=0;
++count;
}
if(flag==1)
{
Serial.println("Access Allowed!");
digitalWrite(led2,HIGH);
delay(4000);
digitalWrite(led2,LOW);
}
else
{
Serial.println("Access Denied");
digitalWrite(led,HIGH);
delay(4000);
digitalWrite(led,LOW);
}
for(count=0; count<12; count++)
input[count]= 'F';
}
}
Rfid_em18_real.ino (1018 Bytes)
if (Serial.available())
{
count = 0;
flag = 1;
while (count < 12 && flag != 0)
{
input[count] = Serial.read();
You check whether there is Serial data available and if there is (it may only be 1 byte) you read from Serial 12 times. Have you tried printing each char as it is received ?
The RFID tag contains 12 bytes of data and this is why i have to read from serial 12 times. It is used for comparing with a pre-provided RFID tag number. If the two serial numbers match, the output is shown.
Your presented code and the attached code don't seem to match. The attached code
char tag[] ="1C002492359F";
char input[12];
int count = 0;
boolean flag = 0;
int led=9;
int led2=8;
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop()
{
if(Serial.available())
{
count = 0;
while(Serial.available() && count < 12)
{
input[count] = Serial.read();
count++;
delay(5);
}
if(count == 12)
{
count =0;
flag = 1;
while(count<12)
{
if(input[count]==tag[count])
flag = 1;
else
flag= 0;
count++;
}
}
if(flag == 1)
{
Serial.println("Access Allowed!");
digitalWrite(led2,HIGH);
delay(4000);
digitalWrite(led2,LOW);
}
else
{
Serial.println("Access Denied");
digitalWrite(led,HIGH);
delay(4000);
digitalWrite(led,LOW);
}
for(count=0; count<12; count++)
input[count]= 'F';
count = 0;
}
}
So which one do you need comments on? For the above, print the received characters to serial monitor
void loop()
{
if(Serial.available())
{
count = 0;
while(Serial.available() && count < 12)
{
input[count] = Serial.read();
Serial.println(input[count]);
count++;
delay(5);
}
Serial.print("Receive finished; count = ");
Serial.println(count);
...
...
Does it consistently print 12 for the count?
And when posting code in a post, please use code tags
Type
** **[code]** **
before pasted code
Type
** **[/code]** **
after pasted code