Go Down

Topic: compare char to char (Read 761 times) previous topic - next topic

smallpoul

I'm trying to do something when rfid char match. here the code
Code: [Select]
// RFID reader for Arduino
// Wiring version by BARRAGAN <http://people.interaction-ivrea.it/h.barragan>
// Modified for Arudino by djmatic
// Re-Modified for Arduino and 125Khz JsxzLz RFID Kit from Ebay by Biohazard

// RFID reader comes with 11 pins: D3 D2 D1 Rest Mcst Gnd L1 L2 PC TX VCC
// We need these to be connected:
// Rest to Arduino pin 2
// Gnd to Arduino GND
// L1 and L2 to the antenna
// TX to Arduino RX0
// VCC To Arduino 5V


int  val = 0;
char code[14];
char michael[14] = {'4', 'B', '0', '0', '1', '0', '8', '5', '1', '3', '4', '7', '0', 'F'};
// 2 digits manufacture code
// 10 digits card code
// 2 digits parity bits

int bytesread = 0;

void setup() {

  Serial.begin(9600);     // RFID reader TX pin, Baud rate: 9600, Data bits: 8, stop bit: 1.
  pinMode(2,OUTPUT);      // Set digital pin 2 as OUTPUT to connect it to the RFID RESET pin
  digitalWrite(2, HIGH);  // Activate the RFID reader



void loop() {

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

   
 
}


It give me this error when trying to copile it: error: invalid array assignment
Checked on the net without any clue
Thanks in advance

HazardsMind

What does bytesread get up to, 14 or less?
Also something is wrong with this, "if (code = michael)" can you spot the mistake?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

AWOL

Quote
Also something is wrong with this, "if (code = michael)" can you spot the mistake?

Two things
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Even if you fix the = problem (with ==), that is only going to compare the address where the two arrays start. It will NOT compare the contents of the arrays. You could use memcmp() to see if the data is the same. You can not use strcmp() since you do not have strings (NULL terminated arrays of chars).

Code: [Select]
        Serial.println(code);             // print the TAG code
This is wrong, too, because code is not a string. It is an array of chars, but it is NOT NULL terminated. And there is no room for the NULL terminator.

smallpoul

My mistake, thanks for the ==. Don't no why i done that. Now the code working and it show me the 14 digit of the card in the serial monitor. I will try the memcmp() like you said and comeback to you thanks.

smallpoul

It seems to work now but could you tell me how can i flush the array after print have been done cause now it always print after the good card have been pass.
thanks

AWOL

Can't you just set the index, and write a zero into the first location in the array?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
Can't you just set the index, and write a zero into the first location in the array?

Even better would be to make the arrays one element larger and keep them NULL terminated as data is added. That way you can safely use them as strings (including using the more intuitive strcmp()).

smallpoul

Got it work now, just one last thing, here the code:

Code: [Select]
// RFID reader for Arduino
// Wiring version by BARRAGAN <http://people.interaction-ivrea.it/h.barragan>
// Modified for Arudino by djmatic
// Re-Modified for Arduino and 125Khz JsxzLz RFID Kit from Ebay by Biohazard

// RFID reader comes with 11 pins: D3 D2 D1 Rest Mcst Gnd L1 L2 PC TX VCC
// We need these to be connected:
// Rest to Arduino pin 2
// Gnd to Arduino GND
// L1 and L2 to the antenna
// TX to Arduino RX0
// VCC To Arduino 5V


int  val = 0;
char code[14];
char michael[14] = {'4', 'B', '0', '0', '1', '0', '8', '5', '1', '3', '4', '7', '0', 'F'};
char emilie[14] =  {'0', '5', '0', '0', '1', '5', '1', '5', '6', '7', '3', '9', '0', 'D'};;


// 10 digits card code
// 2 digits parity bits

int bytesread = 0;

void setup() {

  Serial.begin(9600);     // RFID reader TX pin, Baud rate: 9600, Data bits: 8, stop bit: 1.
  pinMode(2,OUTPUT);      // Set digital pin 2 as OUTPUT to connect it to the RFID RESET pin
  digitalWrite(2, HIGH);  // Activate the RFID reader
  Serial.println("rfid ready to read");
 



void loop() {
 
   boolean e_card = true;
   boolean m_card = true;
  if(Serial.available() > 0) {            // if data available from reader
    if((val = Serial.read()) == 10) {     // check for header
      bytesread = 0;
      while(bytesread < 14) {             // read 14 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 == 14) {         // if 14 digit read is complete
        if( memcmp(code, michael,14) != 0) m_card = false;
        if( memcmp(code, emilie,14) != 0) e_card = false;
       
        if (m_card)   
          {
            Serial.println("welcome Michael");
          }
         else if (e_card)
            {
            Serial.println("welcome Emilie");
            }
         else;
            {
            Serial.println("Unknow tag"); 
            }
           
      }
      bytesread = 0;
      digitalWrite(2, LOW);               // deactivate the RFID reader for a moment so it will not flood
      delay(1500);                        // wait for a bit
      digitalWrite(2, HIGH);              // Activate the RFID reader
    }
  }
 
  }
 



Why i always getting unknow tag event if it a valide one.

If i pass a good one it also print in unknow tag
rfid ready to read
welcome Emilie
Unknow tag

Thanks

PaulS

Code: [Select]
         else;
            {
            Serial.println("Unknow tag"); 
            }

What's that semicolon doing after the else statement?

smallpoul

Thanks, sorry for the newbie question, thanks everything is working.

Go Up