Need help in arduino RFID code

Hello All,

Im working on one of my msc project, i have adopted one of the codes and tried to change it according to my requirement.

The full code is below:

int  val = 0;
int bytesread = 0;
int redPin = 13; // Connect red LED to pin 13
int grnPin = 8; // Connect green LED to pin 8
int buzPin = 7; // Connect buzzer to pin 7
int rfidPin = 2; // RFID enable pin connected to digital pin 2
long time;
long lastUpdate=0;
char code[12];
char tagArray[12];     // the array for the tags
char User[12] = {
  '3', 'A', '0', '0', '0', '6', 'D', '2', 'B', '9'};

// '3', 'A', '0', '0', '0', '6', 'D', '2', 'B', '9'



void setup() {

  Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
  pinMode(rfidPin,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
  pinMode(redPin,OUTPUT); // Set ledPin to output
  pinMode(grnPin,OUTPUT);
  pinMode(buzPin,OUTPUT);
  digitalWrite(rfidPin, LOW); // Activate the RFID reader

}


void loop() {
  digitalWrite(buzPin,0);
  time = millis();
  Serial.print("time (loop starts again)= ");
  Serial.print(time);
  Serial.println();
  delay(1000);
  if(Serial.available() > 0) {          // if data available from reader
    if((val = Serial.read()) == 10) {   // check for header
      bytesread = 0;
      while(bytesread<10) {              // read 10 digit 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
        digitalWrite(rfidPin, HIGH);             // deactivate RFID reader 
        if(strcmp(code,User) == 0) {
          Serial.println("Kishan is in range");
          Serial.print("TAG code is: ");   // possibly a good TAG
          Serial.println(code);     // print the TAG code
          lastUpdate = millis();
          Serial.print("time elapsed = ");
          Serial.println(time);
          digitalWrite(grnPin,HIGH); // good read light green LED
          digitalWrite(redPin,LOW); // turn off red LED
          digitalWrite(rfidPin,HIGH); // turn off rfid LED

        }
        if(strcmp(code,User) !=0) {
          Serial.println("Unidentified Person Alert");
          Serial.println(code);
          lastUpdate = millis();
          Serial.print("time two = ");
          Serial.println(time);
          digitalWrite(redPin,HIGH); // not recognized tag light red LED
          digitalWrite(grnPin,LOW); // turn off green LED
          digitalWrite(rfidPin,HIGH); // turn off rfid LED
          Serial.print("delay starts now");
          Serial.println();
          delay(1000);   

        }

      }
      bytesread = 0;
      Serial.print("rfid activates now");
      Serial.println();
      digitalWrite(rfidPin,LOW); // turn off green LED

    }

  }
  if(((time - lastUpdate) >= 50000) && ((time - lastUpdate) <= 60000)) {
    Serial.print("checking for kishan");
    Serial.println();
    blinks();
  }
  else if ((time - lastUpdate) >= 60000){
    digitalWrite(redPin, HIGH);
    digitalWrite(grnPin,LOW); //turn off green led
    digitalWrite(buzPin,HIGH); //play annoying buzzer :)
    Serial.print("Help me !!!");
    Serial.println();
    delay(50);
    digitalWrite(redPin, LOW);
    delay(50);
  }
  if((strcmp(code,User) !=0) && (time >= 60000)) {
    Serial.print("Im inside");
    blink_fast();
  }
}
void blinks() {
  digitalWrite(redPin, HIGH);
  digitalWrite(grnPin,LOW); //turn off green led
  //digitalWrite(buzPin,HIGH); //play annoying buzzer :)
  Serial.print("Kishan is not in range");
  Serial.println();
  delay(250);
  digitalWrite(redPin, LOW);
  delay(250);
}

void blink_fast() {
  digitalWrite(buzPin,HIGH); //play annoying buzzer :)
  Serial.println("Kishan, You left me behind!!! ");
  Serial.print("Time = ");
  Serial.println(time);
  digitalWrite(grnPin,LOW); //turn off green led
  digitalWrite(redPin, HIGH);
  delay(50);
  digitalWrite(redPin, LOW);
  delay(50);
}

this code tries to detect rfid tags and if its the right one then green light turns on if its a wrong tag then red light turn on. it can starts count down in both cases and if the tags are not detected for certain period of time then the red light starts blinking and buzzer makes noise until you place the right tag.

My problem is with this section of the code as below:

 if(((time - lastUpdate) >= 50000) && ((time - lastUpdate) <= 60000)) {
    Serial.print("checking for kishan");
    Serial.println();
    blinks();
  }
  else if ((time - lastUpdate) >= 60000){
    digitalWrite(redPin, HIGH);
    digitalWrite(grnPin,LOW); //turn off green led
    digitalWrite(buzPin,HIGH); //play annoying buzzer :)
    Serial.print("Help me !!!");
    Serial.println();
    delay(50);
    digitalWrite(redPin, LOW);
    delay(50);
  }
  if((strcmp(code,User) !=0) && (time >= 60000)) {
    Serial.print("Im inside");
    blink_fast();
  }
}
void blinks() {
  digitalWrite(redPin, HIGH);
  digitalWrite(grnPin,LOW); //turn off green led
  //digitalWrite(buzPin,HIGH); //play annoying buzzer :)
  Serial.print("Kishan is not in range");
  Serial.println();
  delay(250);
  digitalWrite(redPin, LOW);
  delay(250);
}

void blink_fast() {
  digitalWrite(buzPin,HIGH); //play annoying buzzer :)
  Serial.println("Kishan, You left me behind!!! ");
  Serial.print("Time = ");
  Serial.println(time);
  digitalWrite(grnPin,LOW); //turn off green led
  digitalWrite(redPin, HIGH);
  delay(50);
  digitalWrite(redPin, LOW);
  delay(50);
}

the timer starts as we execute the code and enters this section of the code when no tags are presented to the reader and time reaches 50000 and less then 60000 as below:

 if(((time - lastUpdate) >= 50000) && ((time - lastUpdate) <= 60000)) {
    Serial.print("checking for kishan");
    Serial.println();
    blinks();
  }

and when the time is over 60000 it should enter the second part of the code as below:

else if ((time - lastUpdate) >= 60000){
    digitalWrite(redPin, HIGH);
    digitalWrite(grnPin,LOW); //turn off green led
    digitalWrite(buzPin,HIGH); //play annoying buzzer :)
    Serial.print("Help me !!!");
    Serial.println();
    delay(50);
    digitalWrite(redPin, LOW);
    delay(50);
  }

but instead it skips and goes to the another section of the code as below when i have given a condition that if the tag is wrong and time is over 60000 then only execute this section, im confused why it enters this section when it should choose the code above when the time goes above 60000:

  if((strcmp(code,User) !=0) && (time >= 60000)) {
    Serial.print("Im inside");
    blink_fast();
  }

Hope someone can help me with this issue and point me where im going wrong as i do not have much experience in coding.

Thank you.

Your problem is a failure to communicate. This code:

 if(((time - lastUpdate) >= 50000) && ((time - lastUpdate) <= 60000)) {

contains a fundamental communication problem.

You KNOW that 50000 is an integer and you KNOW that 60000 is an integer. The compiler seriously disagrees with you, since the maximum value that an int can hold is 32767.

See, you are thinking in terms of integer, while the compiler is thinking in terms it understands, such as int.

Is all lost? No, of course not. The compiler knows about other integer types, like long, so you just need to give the compiler a hint that it should think in broader terms.

That's what the L and UL suffixes are for.

 if(((time - lastUpdate) >= 50000[glow]UL[/glow]) && ((time - lastUpdate) <= 60000[glow]UL[/glow])) {

Now, you know, and the compiler knows, that 50000 and 60000 are integral values, and the compiler will treat the values as unsigned long instead of int, with the proper results.

Thank you so much Paul... it was one of the simplest explanation for the issue :slight_smile: i will try this out.

Hi Paul, I tried using suffix (UL) but still it is not able to identify the code :frowning: is there anything else i can try?

but still it is not able to identify the code

What does this mean?

This is a fairly large project, and you have every bit of your code in loop(). It's time you learned how to define and call functions.

Look at what happens when you read a tag. What conditions have to be true in order to test the tag that was read for validity?

First, there must be serial data present to read. Then, the first byte read must be a 10. Then, you must have read 10 bytes.

This is overly complex. How does there get to be 10 bytes read? There must be serial data to read. So, create a function to read all serial data available. Return from that function when there is no more serial data to read, or when the end-of-tag marker has been read.

Then, if there are 10 bytes, call a function to verify that the tag is good. Record in a boolean variable whether the tag is good or not. Don't test it every time you want to know. It's either good or bad. It doesn't change until a new tag is scanned.

Write some code to test each function.

It isn't clear to me why you are turning the RFID reader off and on, or how setting the pin LOW turns it on and setting it HIGH turns it off.

It isn't clear what all the delay() calls are for, either.

Add comments that explain what each section is supposed to do. Comments at the end of a statement that tell what the statement did are not very useful.

          digitalWrite(redPin,LOW); // turn off red LED

By the time I get to the end of the statement, I know that if turned a pin off. I don't need to be told that.

// Read all serial data

// If enough data has been read, test whether the tag is valid

// If the tag is valid, do this or that

Comments like this are useful for defining the structure of the program.

  if(((time - lastUpdate) >= 50000) && ((time - lastUpdate) <= 60000)) {
    Serial.print("checking for kishan");
    Serial.println();
    blinks();
  }
  else if ((time - lastUpdate) >= 60000){

In 6 lines, you've calculated time - lastUpdate 3 times, and neither variable has changed. Is this really necessary? Do it one, and store it.

What is a kishan?

What is a kishan?

Illegal alien? What...well it is a security application. ;D

Lefty

It's funny how you often overlook the simplest things. After asking wat a kishan is, then seeing your reply, I noticed that OP's user ID is kishan. I guess that explains what a kishan is.

I saw that too, but still can't figure out how the code is going to check with him? If a message is displayed in the forest with nobody around, was there really a message sent?

Lefty