RFID will only read tag once. Help.

Hello,

I have a program written that will text a different message based on what tag is read. Everything works out, except the RFID will only scan one tag, and the green light stays lit. The only way I have found to reset it is to open up the Serial Monitor again. I will post the code and any insight would be appreciated.

Thanks

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7,8);
char card[12] = {'7','2','0','0','7','7','5','2','4','9'};
char card2[12] = {'7','2','0','0','7','7','4','9','E','B'};
char code[12];
int bytesread = 0;
int rfidPin = 2; // RFID enable pin connected to digital pin 2
int val=0;


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
  digitalWrite(rfidPin, LOW); // Activate the RFID reader
  SIM900.begin(19200);
  SIM900power();
  delay(10000);
}
void SIM900power()
{digitalWrite(9,HIGH);
delay(1000);
digitalWrite(9,LOW);
delay(3000);
}
void SendSMS()
{          
  SIM900.println("Code is great: ");
  SIM900.print("AT+CMGF=1\r");                                                        // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+xxxxxxxxxxx\"");                                     // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Hello");   // message to send
  delay(100);
  SIM900.println((char)26);                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(5000);                                     // give module time to send SMS
  SIM900power();                                   // turn off module
}
void SendSMS2()
{
            SIM900.println("Code is good: ");
          SIM900.print("AT+CMGF=1\r");                                                        // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+xxxxxxxxxxx\"");                                     // recipient's mobile number, in international format
  delay(100);
  SIM900.println("Howdy");   // message to send
  delay(100);
  SIM900.println((char)26);                       // End AT command with a ^Z, ASCII code 26
  delay(100); 
  SIM900.println();
  delay(5000);                                     // give module time to send SMS
  SIM900power();                                   // turn off module
}
void loop() 
{
  digitalWrite(rfidPin, LOW);

  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);        // De-Activate the RFID reader

        if(strcmp(code, card) == 0) 
        {
          Serial.print("Code is accecpted: ");
          Serial.println(code);
          SendSMS();
          do {} while (1);
          
        }
         if(strcmp(code, card2) == 0) 
        {
          Serial.print("Code is alright: ");
          Serial.println(code);
          SendSMS2();
          do {} while (1);
          
        }
        
 
        else 
        {
          Serial.print(code);
          Serial.println(" is not valid!");
      
        }
      }
      bytesread = 0;
      delay(500);                // wait for a second
     }
  }
}

WORKINGCODE2.ino (3.8 KB)

Could you post the code using code tags.

I don't want to have to:
Download the file,
Store it in a folder with the correct name.
Open the Arduino IDE to view the file.
And then delete the file after viewing it.

I'd rather just read it on this page.

You could also use Notepad++ to view it. Then it can be stored anyplace.

Reading on screen in the forum is great when it's not too big. And within code tags as JL says, keeps the ( )s and { }s from creating squirrelly characters.

Hi, I'm using the Arduino Mega 2560 with rfid reader yhy502.
I'm not able to read my card serial, all I get is "-1" (card not available).

I added the code using code tags. So what do you think?

if(strcmp(code, card) == 0) 
        {
          Serial.print("Code is accecpted: ");
          Serial.println(code);
          SendSMS();
          do {} while (1);
          
        }

So this is the code that runs when you read a tag that matches. It sends an SMS. And then what do you think that last line there does?

The SMS code came to fruition after some of my fellow classmates tried to come up with a code to get it to send a message to begin with. The only one that worked efficiently was the one that had that line of code in it, although I'm not sure what that line does.

That line says, " Do nothing as long as 1 is not 0." That means the Arduino will hit that line and dutifully do nothing forever. That's the end for this program until you reset the board.