help with arduino rfid


i am trying to make an rfid lock using the code below. the code reads the tag fine, but it won’t run the servo when it detects the correct card.

int  val = 0; 
char code[10]; //stores the code from card
int bytesread = 0; 
char correctCode[] = {"0F03028CF8"}; // correct code that should turn servo

#include <Servo.h>
Servo myservo;

void setup() { 

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps 
pinMode(2,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin 
digitalWrite(2, LOW);                  // Activate the RFID reader
myservo.attach (9);

 void loop() { 

  if(Serial.available() > 0) {          // if data available from reader 
    if((val = == 10) {   // check for header 
      bytesread = 0; 
      while(bytesread<10) {              // read 10 digit code 
        if( Serial.available() > 0) { 
          val =; 
          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 
        Serial.print("TAG code is: ");   // possibly a good TAG 
        Serial.println(code);         // print the TAG code 
        if (code == correctCode) {
        myservo.write (180);
       delay (1000);
      myservo.write (90);
      bytesread = 0; 
      digitalWrite(2, HIGH);                  // deactivate the RFID reader for a moment so it will not flood
           delay(1500);                       // wait for a bit 
           digitalWrite(2, LOW);                  // Activate the RFID reader

and when i add the word HEX, it turns the servo after reading any card:

if (code == correctCode, HEX)

please help.


        if (code == correctCode) {

code is the address of a block of memory. correctCode is the address of another block of memory. Do you really expect them to match?

correctCode is a string. To compare strings, you use the strcmp() function. Unfortunately, you can't do that, because code is NOT a string. code is an array of chars. You could make it a string, if you properly NULL terminated it.

thank yo so much Paul. It worked!! sorry about the multiple threads. i am new to forums. sorry for any annoyance.