Write in the eerpom of ArduinoUno a code RFID (ID20)

I have did it and I can see EEPROM 2400CC572897 = tag read 2400CC572897, picture attached 8)

                  //test EEPROM values
                   Serial.println("string saved in EEPROM between 10 to 24: ");
             // it show me string saved in EEPROM tag 2400CC572897
                   Serial.print(EEPROM.read(12));
                   Serial.print(EEPROM.read(13));
                   Serial.print(EEPROM.read(14));
                   Serial.print(EEPROM.read(15));
                   Serial.print(EEPROM.read(16));
                   Serial.print(EEPROM.read(17));
                   Serial.print(EEPROM.read(18));
                   Serial.print(EEPROM.read(19));
                   Serial.print(EEPROM.read(20));
                   Serial.print(EEPROM.read(21));
                   Serial.print(EEPROM.read(22));
                   Serial.print(EEPROM.read(23));
                   Serial.println(EEPROM.read(24));
                   /*Serial.println(EEPROM.read(25));
                   Serial.println(EEPROM.read(26));
                   Serial.println(EEPROM.read(27));*/
                  //Serial.println(code[1]);
              Serial.println("string read: ");    
             //tag read 2400CC572897
                  Serial.print(code[2]);
                  Serial.print(code[3]);
                  Serial.print(code[4]);
                  Serial.print(code[5]);
                  Serial.print(code[6]);
                  Serial.print(code[7]);
                  Serial.print(code[8]);
                  Serial.print(code[9]);
                  Serial.print(code[10]);
                  Serial.print(code[11]);
                  Serial.print(code[12]);
                  Serial.println(code[13]);

How I am sure that
my tag 2400CC572897
then I have read
Serial.print(EEPROM.read(24)); //here is 7
Serial.print(code[13]); //here is 7

then I have tryed

if (value1[24] == code[13])  {
    Serial.println("ok");
}

my error is

In function 'void loop()':
error: invalid types 'byte[int]' for array subscript

if (value1[24] == code[13])  {

Apparently, value1 isn't an array.

You didn't print values from an array when you printed values read from addresses in EEPROM.

You could do this:

byte offs = 11;
bool match = true;
for(byte b=2; b<14; b++)
{
  if(EEPROM.read(b+offs) != code[b])
  {
    match = false;
    break;
  }
}

if(match)
{
   // found a match
}

You might need to adjust the value of offs and the for loop values.

oh thanks..
but I cant understand your code =(
so I cant do work it,

EEPROM.read(b+offs) != code[b]

Do you mean:

code[b] will read from 2 to 14 values from a tag 
b+offs   will read from 13(2+11=13) to 25 (11+14=25) in EEPROM

can you explain a little bit more please.. XD

if(match)
{
   // found a match
}

The code snippet that Paul posted will read from EEPROM addresses 13 to 24

The match variable is initially set to true then each byte read is compared with the corresponding byte of the code array and if they are not equal then the match variable is set to false and the loop is exited as there is no point in carrying on.

So, after the loop the match variable is true if the whole set of bytes match or false if any of them don't match.

if (match)

returns true if match is true or false if it is false. It is a quick way of writing

if (match == true)

ok, thanks, I will try again XD

Edit: great 8) I will check my code but it is fine, picture attached

Untitled.jpg

Hello everyone again :slight_smile:

finally my code is working fine, but just for one card.. =(
if I am using a second card it work so so..

  • after cleaned my EEPROM I have programming my Arduino. Then:
  • start-> read a tag1 -> save it in EEPROM (position1, 10 to 24 in EEPROM)
  • when read a second tag2-> save it in EEPROM (position2, 30 to 44 in EEPROM)
  • but when it has read a tag saved before in position 10 to 24 in EEPROM, my code clean the EEPROM from 10 to 44, but here my code must clean just between 10 to 24
//ArduinoUNO
//code_test_7.5.4

#include <EEPROM.h>

char code[15];
char valor[12];

int  val = 0; 
byte value1;
byte value2;

int bytesread = 0; 
int led = 13;

int addr = 10;
int addr2 = 30;
int a = 0;
byte x= 1;        // (00000001)

void setup() 
  { 
  Serial.begin(9600); 
  pinMode(13, OUTPUT);   
  pinMode(12, OUTPUT);
  } 

void loop() 
{ 
        if(Serial.available() > 0) 
        {                            
          if((val = Serial.read()) == 10) 
                  {     
                    bytesread = 0; 
                            while(bytesread < 15) 
                            {             
                                      if( Serial.available() > 0) 
                                      { 
                                        val = Serial.read(); 
                                        if((val == 10)||(val == 15)) 
                                            {  
                                            break;                        
                                            } 
                                        code[bytesread] = val;          
                                        bytesread++; 
                                      } 
                            }                             
                     Serial.print("tag read: ");
                     Serial.println(code);   //tag1 x2400CC392AFBxxx   tag2 x2400CC572897xxx

                       Serial.print(" bitRead: ");
               //test read bit to bit (00000001)
                       Serial.print(bitRead(x,7));
                       Serial.print(bitRead(x,6));
                       Serial.print(bitRead(x,5));
                       Serial.print(bitRead(x,4));
                       Serial.print(bitRead(x,3));
                       Serial.print(bitRead(x,2));
                       Serial.print(bitRead(x,1));
                       Serial.println(bitRead(x,0));

              //test read eeprom 10 to 49
                         Serial.print("EEPROM desde 10 to 49: ");
                         for (int k=10; k<49; k++)
                         {
                           value1 = EEPROM.read(k);
                           Serial.print(value1);       
                         } 
                         Serial.println(" ");
                   
    //comparation value in eeprom with value read tag
                          bool match1 = true;
                          byte offs1=10;
                          bool match2 = true;
                          byte offs2=30;

                          for (byte b=2; b<13; b++)
                               {
                                 if (EEPROM.read( b + offs1) != code [b])
                                 {
                                 match1 = false;
                                 break;
                                 }
                               }       
                           if (match1 == true)
                           {
                             Serial.println("ok position1 - tag deleted from position1, EERPOM 10 to 29");
                             digitalWrite(13, HIGH);
                             delay(250);
                             digitalWrite(13, LOW);
                             delay(250);
                             bitWrite(x, 1, 0);
                          //clean EEPROM 10 to 29
                             for (int i = 10; i < 29; i++)
                             EEPROM.write(i, 0);                          
                           } 
                           
                           else
                           
    //comparation value in eeprom with value read tag
                          

                          for (byte b=2; b<13; b++)
                               {
                                 if (EEPROM.read( b + offs2) != code [b])
                                 {
                                 match2 = false;
                                 break;
                                 }
                               }       
                           if (match2 == true)
                           {
                             Serial.println("ok position2 - tag deleted from position2, EERPOM 30 to 49");
                             digitalWrite(12, HIGH);
                             delay(250);
                             digitalWrite(12, LOW);
                             delay(250);
                             bitWrite(x, 2, 0);
                             //clean EEPROM 30 to 49
                             for (int i = 30; i < 49; i++)
                             EEPROM.write(i, 0);                          
                           } 
                           
                           else
              //bitread position 1
                             if (bitRead(x,1) == 0)
                               {
                                //write 1       
                                  bitWrite(x, 1, 1);
                                 //write in eerpom
                                  for(byte b=0; b<bytesread; b++)
                                  { //addr = star in position 10
                                     EEPROM.write(addr + b, code[b]);
                                  }                     
                                //turn on led pin13  
                                  digitalWrite(13, HIGH);
                                  delay(250);
                                  digitalWrite(13, LOW);
                                  delay(250);
                                  Serial.println("1 at position 1 of X - Tag saved at position1, 10 to 24 in EEPROM");
                               } 
              
               //bitread position 2
                         else    if (bitRead(x,2) == 0)
                               {
                                //write 1       
                                  bitWrite(x, 2, 1);
                                 //write in eerpom
                                  for(byte b=0; b<bytesread; b++)
                                  { //addr2 = star in position 30
                                     EEPROM.write(addr2 + b, code[b]);
                                  }                     
                                //turn on led pin12  
                                  digitalWrite(12, HIGH);
                                  delay(250);
                                  digitalWrite(12, LOW);
                                  delay(250);
                                  Serial.println("1 at position 2 of X - Tag saved at position2, 30 to 44 in EEPROM");
                               } 
                               
               //test read bit to bit (00000001)
                       Serial.print(" bitRead: ");
                       Serial.print(bitRead(x,7));
                       Serial.print(bitRead(x,6));
                       Serial.print(bitRead(x,5));
                       Serial.print(bitRead(x,4));
                       Serial.print(bitRead(x,3));
                       Serial.print(bitRead(x,2));
                       Serial.print(bitRead(x,1));
                       Serial.println(bitRead(x,0)); 
                
                //test read EEPROM      
                       Serial.print("EEPROM desde 0 to 44: ");
                         for (int k=0; k<44; k++)
                         {
                        Serial.print(EEPROM.read(k)); 
                         
                         } 
                         Serial.println(" ");   
                         Serial.println("__________________________");                  
                  }      
        }       
}

      //read eerpom 10 to 24
      Serial.print("EEPROM desde 10 to 50: ");
      for (int k=10; k<49; k++)

Should this code match the comments (either of them) or should the comments match the code ?

oh.. sorry.. yes, the code is fine..

//read eerpom 10 to 49
      Serial.print("EEPROM desde 10 to 49: ");
      for (int k=10; k<49; k++)

my problem is using "match1" and "match2" with two cards as I have explained =(

oh.. sorry.. yes, the code is fine..

Really? You want to clear all of EEPROM? That seems to contradict the desire to clear only that one tag's space in EEPROM.

When you make up your mind, feel free to let us know.

:astonished:
I mean it is fine just here

Should this code match the comments (either of them) or should the comments match the code ?

oh.. sorry.. yes, the code is fine..

//read eerpom 10 to 49
      Serial.print("EEPROM desde 10 to 49: ");
      for (int k=10; k<49; k++)

Hello, thanks everyone for help me..

it's a resume:
I have an ArduinoUno + ID20 and I am trying:

  • Turn on a led(pulse-pin13) when a tag1 is read and then save it in EEPROM in position 10 to 29 (it is ok :smiley: )
  • when a second tag2 is read save it in EEPROM in position 30 to 49 and pulse pin12 (it is ok :smiley: )
  • when the tag read is already saved before is EEPROM delete it from position (10 to 29 or 30 to 49) and give a pulse (pin12 or pin13) (it is my problem =()

for example if tag1 is saved in position 10 to 29 and when this tag is read again my code must delete the EEPPROM just between 10 to 29, but at the moment my code delete the EEPROM between 10 to 49, it isn't correct =(

#include <EEPROM.h>

char code[15];

int  val = 0; 
int bytesread = 0; 
int led = 13;
int addr = 10;
int addr2 = 30;

byte x= 1;        // (00000001)

void setup() 
  { 
  Serial.begin(9600); 
  pinMode(13, OUTPUT);   
  pinMode(12, OUTPUT);
  } 

void loop() 
{ 
        if(Serial.available() > 0) 
        {                            
          if((val = Serial.read()) == 10) 
                  {     
                    bytesread = 0; 
                            while(bytesread < 15) 
                            {             
                                      if( Serial.available() > 0) 
                                      { 
                                        val = Serial.read(); 
                                        if((val == 10)||(val == 15)) 
                                            {  
                                            break;                        
                                            } 
                                        code[bytesread] = val;          
                                        bytesread++; 
                                      } 
                            }                             
                     Serial.print("tag read: ");
                     Serial.println(code);   //tag1 x2400CC392AFBxxx   tag2 x2400CC572897xxx

                   
    //comparation value in eeprom with value read-tag
                          bool match1 = true;
                          byte offs1=10;
                          bool match2 = true;
                          byte offs2=30;

                          for (byte b=2; b<13; b++)
                               {
                                 if (EEPROM.read( b + offs1) != code [b])
                                 {
                                 match1 = false;
                                 break;
                                 }
                               }       
                           if (match1 == true)
                           {
                             Serial.println("ok position1 - tag deleted from position1, EERPOM 10 to 29");
                             digitalWrite(13, HIGH);
                             delay(250);
                             digitalWrite(13, LOW);
                             delay(250);
                             bitWrite(x, 1, 0);
                          //clean EEPROM 10 to 29
                             for (int i = 10; i < 29; i++)
                             EEPROM.write(i, 0);                          
                           } 
                           
                           else
                           
    //comparation value in eeprom with value read-tag

                          for (byte b=2; b<13; b++)
                               {
                                 if (EEPROM.read( b + offs2) != code [b])
                                 {
                                 match2 = false;
                                 break;
                                 }
                               }       
                           if (match2 == true)
                           {
                             Serial.println("ok position2 - tag deleted from position2, EERPOM 30 to 49");
                             digitalWrite(12, HIGH);
                             delay(250);
                             digitalWrite(12, LOW);
                             delay(250);
                             bitWrite(x, 2, 0);
                             //clean EEPROM 30 to 49
                             for (int i = 30; i < 49; i++)
                             EEPROM.write(i, 0);                          
                           } 
                           
                           else
              //bitread position 1
                             if (bitRead(x,1) == 0)
                               {
                                //write 1       
                                  bitWrite(x, 1, 1);
                                 //write in eerpom
                                  for(byte b=0; b<bytesread; b++)
                                  { //addr = start in position 10
                                     EEPROM.write(addr + b, code[b]);
                                  }                     
                                //turn on led pin13  
                                  digitalWrite(13, HIGH);
                                  delay(250);
                                  digitalWrite(13, LOW);
                                  delay(250);
                                  Serial.println("1 at position 1 of X - Tag saved at position1, 10 to 24 in EEPROM");
                               } 
              
               //bitread position 2
                         else    if (bitRead(x,2) == 0)
                               {
                                //write 1       
                                  bitWrite(x, 2, 1);
                                 //write in eerpom
                                  for(byte b=0; b<bytesread; b++)
                                  { //addr2 = start in position 30
                                     EEPROM.write(addr2 + b, code[b]);
                                  }                     
                                //turn on led pin12  
                                  digitalWrite(12, HIGH);
                                  delay(250);
                                  digitalWrite(12, LOW);
                                  delay(250);
                                  Serial.println("1 at position 2 of X - Tag saved at position2, 30 to 44 in EEPROM");
                               }                 
                  }      
        }       
}

There are some very peculiar things about parts of your code
What is this line supposed to do ?
What does it actually do ?

if((val = Serial.read()) == 10)

There are places in the code where the comments do not match the code, such as

        //clean EEPROM 30 to 49
        for (int i = 30; i < 49; i++)
          EEPROM.write(i, 0);                          
      }

If I were you I would put some Serial.prints in every for loop so that you can see what is going on. Print a label so that you know where the print is coming from.

Overall you seem to be approaching this in a clumsy way. I would write functions to read from the EEPROM to an array from a range of addresses, having passed the start address as a parameter, to write an array to the EEPROM, again passing the start address as a parameter, to compare 2 arrays and return a value indicating whether they matched and, if you need to, to clear a range of EEPROM addresses having passed the start address as a parameter.

Doing it this way would allow you to test each function to make sure it did what you want/expect

thanks, and yes, I have done it Serial.println

I have attached a pic, because my problem is just when read a tag saved before in position 10 to 24 of the EEPROM, here delete the EEPROM between 10 to 44 but must delete only 10 to 24

if tag read is saved between 10 to 24 in the EEPROM must delete it

for (int i = 10; i < 29; i++) //Iam using 10 to 29 or 10 to 24 any is fine
          EEPROM.write(i, 0);

if tag read is saved between 30 to 44 in the EEPROM must delete it

for (int i = 30; i < 49; i++) //Iam using 30 to 49 or 30 to 44 any is fine
          EEPROM.write(i, 0);

when it read a tag saved in eeprom in position between 30 to 44 is fine because just delete 30 to 44 and dont delete 10 to 24
then I have a problem around here

//comparation value in eeprom with value read-tag
                          bool match1 = true;
                          byte offs1=10;
                          bool match2 = true;
                          byte offs2=30;

                          for (byte b=2; b<13; b++)
                               {
                                 if (EEPROM.read( b + offs1) != code [b])
                                 {
                                 match1 = false;
                                 break;
                                 }
                               }       
                           if (match1 == true)
                           {
                             Serial.println("ok position1 - tag deleted from position1, EERPOM 10 to 29");
                             digitalWrite(13, HIGH);
                             delay(250);
                             digitalWrite(13, LOW);
                             delay(250);
                             bitWrite(x, 1, 0);
                          //clean EEPROM 10 to 29
                             for (int i = 10; i < 29; i++)
                             EEPROM.write(i, 0);                          
                           } 
                           
                           else
                           
    //comparation value in eeprom with value read-tag

                          for (byte b=2; b<13; b++)
                               {
                                 if (EEPROM.read( b + offs2) != code [b])
                                 {
                                 match2 = false;
                                 break;
                                 }
                               }       
                           if (match2 == true)
                           {
                             Serial.println("ok position2 - tag deleted from position2, EERPOM 30 to 49");
                             digitalWrite(12, HIGH);
                             delay(250);
                             digitalWrite(12, LOW);
                             delay(250);
                             bitWrite(x, 2, 0);
                             //clean EEPROM 30 to 49
                             for (int i = 30; i < 49; i++)
                             EEPROM.write(i, 0);                          
                           }

thanks for any help