Pages: 1 [2] 3   Go Down
Author Topic: Write in the eerpom of ArduinoUno a code RFID (ID20)  (Read 2097 times)
0 Members and 1 Guest are viewing this topic.
NZ
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello everyone..

finally my code is going fine.. just now I want to do a verification "value1==code"
value1 is my value saved in EEMPRON
code   is my serial value from tag

here my idea (bad idea  smiley-confuse) )
Code:
                  //comparation value in eeprom with value read tag
                             if (value1 == code)
                               {
                                 Serial.println("ok");
                               }

I dont know how do it
Code:
#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));

              //read eerpom 10 to 24
                         Serial.print("EEPROM desde 10 to 24: ");
                         for (int k=10; k<24; k++)
                         {
                        // Serial.print(EEPROM.read(k));
                         value1 = EEPROM.read(k);
                         Serial.print(value1);      
                         }
                         Serial.println(" ");
                //read eerpom   30 to 44          
                         Serial.print("EEPROM desde 30 to 44: ");
                         for (int k=30; k<44; k++)
                         {
                         value2 = EEPROM.read(k);
                         Serial.print(value2);
                         }
                         Serial.println(" ");
                  
                   //comparation value in eeprom with value read tag
                             if (value1 == code) //here is my problem
                               {
                                 Serial.println("ok");
                               }
                    
                    //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");
                               }
                    
                     //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 pin13  
                                  digitalWrite(12, HIGH);
                                  delay(250);
                                  digitalWrite(12, LOW);
                                  delay(250);
                                  Serial.println("1 at position 2 of X");
                               }                      
 
               //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("__________________________");                  
                  }      
        }      
}
« Last Edit: March 16, 2013, 03:46:28 am by f0raster0 » Logged

keep calm & carry on

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4261
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The idea sounds OK in general.  What happens when you run the code ?

Try printing the 2 variables that you are trying to compare just before the comparison code and print something immediately before and after them, perhaps > and < so that you can see any leading or training spaces if they are there.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

NZ
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, I think its fine but "value1 = code" dont work

after programming my firmware and after I have cleaned my eeprom
1) x = 00000001
2) when read the first card:
 -read 2400CC392AFB (tag1)
 -if position 1 of x = 0 then save tag1 in EEPROM using postion 10 to 24
 -  now write 1 in position 1 of x (then x = 00000011)
 - give a pulse pin13
3) when read a second card, ask if position 1 of x = 0 if also
 -read 2400CC572897 (tag2)
 - then save tag2 in EEPROM using postion 30 to 44
 -  now write 1 in position 2 of x (then x = 00000111)
 - give a pulse pin12

until here is fine i can see it in serial monitor and in my leds
but I want to write "ok" (for now) in serial monitor if some card is already saved in eeprom

value1 == code isnt working because I cant see "ok" in serial monitor  smiley-cry

« Last Edit: March 17, 2013, 05:58:37 am by f0raster0 » Logged

keep calm & carry on

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 70
Posts: 2171
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
value1 == code

this won't work as you are comparing value1 with a pointer:
Code:
char code[15];

you need to compare with one of the elements. As 'bytesread' is your loop counter, maybe you mean:
Code:
//comparation value in eeprom with value read tag
if( value1 == code[ bytesread ] ){
  Serial.println("ok");
}

Edit: k isn't in scope, replaced with bytesread.
« Last Edit: March 16, 2013, 06:07:04 am by pYro_65 » Logged


NZ
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
value1 == code[ bytesread ]

yes I have did it..  smiley-cry but ok I will do it again..

Quote
Edit: k isn't in scope, replaced with bytesread.
I will try  smiley
« Last Edit: March 16, 2013, 06:14:05 am by f0raster0 » Logged

keep calm & carry on

NZ
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have did it and I can see EEPROM 2400CC572897 = tag read 2400CC572897, picture attached smiley-cool

Code:
                 //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]);


* Untitled.jpg (135.75 KB, 867x516 - viewed 21 times.)
« Last Edit: March 16, 2013, 06:58:40 am by f0raster0 » Logged

keep calm & carry on

NZ
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
if (value1[24] == code[13])  {
    Serial.println("ok");
}

my error is

Quote
In function 'void loop()':
error: invalid types 'byte[int]' for array subscript
« Last Edit: March 16, 2013, 07:24:54 am by f0raster0 » Logged

keep calm & carry on

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:

Code:
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.
Logged

NZ
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh thanks..
but I cant understand your code  smiley-cry  
so I cant do work it,
Code:
EEPROM.read(b+offs) != code[b]
Do you mean:
Code:
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..  smiley-lol
Code:
if(match)
{
   // found a match
}
« Last Edit: March 17, 2013, 12:13:46 am by f0raster0 » Logged

keep calm & carry on

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4261
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Code:
if (match)
returns true if match is true or false if it is false.  It is a quick way of writing
Code:
if (match == true)
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

NZ
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, thanks, I will try again  smiley-lol

Edit: great  smiley-cool I will check my code but it is fine, picture attached



* Untitled.jpg (46.81 KB, 498x346 - viewed 20 times.)
« Last Edit: March 17, 2013, 03:53:45 am by f0raster0 » Logged

keep calm & carry on

NZ
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone again  smiley

finally my code is working fine, but just for one card..  smiley-cry
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
Code:
//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("__________________________");                 
                  }     
        }       
}



* code_7-5-4.jpg (118.61 KB, 937x599 - viewed 20 times.)
« Last Edit: March 23, 2013, 07:23:06 am by f0raster0 » Logged

keep calm & carry on

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4261
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      //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 ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

NZ
Offline Offline
Newbie
*
Karma: 0
Posts: 40
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh.. sorry.. yes, the code is fine..
Code:
//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  smiley-cry
Logged

keep calm & carry on

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: 1 [2] 3   Go Up
Jump to: