NZ
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #15 on: March 16, 2013, 03:42:54 am » |
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  ) ) //comparation value in eeprom with value read tag if (value1 == code) { Serial.println("ok"); }
I dont know how do it #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)
Online
Edison Member
Karma: 49
Posts: 1436
May all of your blinks be without delay
|
 |
« Reply #16 on: March 16, 2013, 05:06:41 am » |
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
|
|
|
|
|
NZ
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #17 on: March 16, 2013, 06:00:04 am » |
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 
|
|
|
|
« Last Edit: March 17, 2013, 05:58:37 am by f0raster0 »
|
Logged
|
keep calm & carry on
|
|
|
|
North Queensland, Australia
Online
Edison Member
Karma: 31
Posts: 1199
|
 |
« Reply #18 on: March 16, 2013, 06:04:57 am » |
value1 == code this won't work as you are comparing value1 with a pointer: char code[15]; you need to compare with one of the elements. As 'bytesread' is your loop counter, maybe you mean: //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
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #19 on: March 16, 2013, 06:12:34 am » |
value1 == code[ bytesread ] yes I have did it..  but ok I will do it again.. Edit: k isn't in scope, replaced with bytesread. I will try 
|
|
|
|
« Last Edit: March 16, 2013, 06:14:05 am by f0raster0 »
|
Logged
|
keep calm & carry on
|
|
|
|
NZ
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #20 on: March 16, 2013, 06:54:53 am » |
I have did it and I can see EEPROM 2400CC572897 = tag read 2400CC572897, picture attached  //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]);
|
|
|
|
« Last Edit: March 16, 2013, 06:58:40 am by f0raster0 »
|
Logged
|
keep calm & carry on
|
|
|
|
NZ
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #21 on: March 16, 2013, 07:23:17 am » |
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
|
|
|
|
« Last Edit: March 16, 2013, 07:24:54 am by f0raster0 »
|
Logged
|
keep calm & carry on
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35573
Seattle, WA USA
|
 |
« Reply #22 on: March 16, 2013, 08:58:31 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
NZ
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #23 on: March 17, 2013, 12:10:40 am » |
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..  if(match) { // found a match }
|
|
|
|
« Last Edit: March 17, 2013, 12:13:46 am by f0raster0 »
|
Logged
|
keep calm & carry on
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1436
May all of your blinks be without delay
|
 |
« Reply #24 on: March 17, 2013, 03:06:31 am » |
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)
|
|
|
|
|
Logged
|
|
|
|
|
NZ
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #25 on: March 17, 2013, 03:11:12 am » |
ok, thanks, I will try again  Edit: great  I will check my code but it is fine, picture attached
|
|
|
|
« Last Edit: March 17, 2013, 03:53:45 am by f0raster0 »
|
Logged
|
keep calm & carry on
|
|
|
|
NZ
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #26 on: March 23, 2013, 12:21:37 am » |
Hello everyone again  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("__________________________"); } } }
|
|
|
|
« Last Edit: March 23, 2013, 07:23:06 am by f0raster0 »
|
Logged
|
keep calm & carry on
|
|
|
|
East Anglia (UK)
Online
Edison Member
Karma: 49
Posts: 1436
May all of your blinks be without delay
|
 |
« Reply #27 on: March 23, 2013, 06:16:13 am » |
//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
|
|
|
|
|
NZ
Offline
Newbie
Karma: 0
Posts: 39
|
 |
« Reply #28 on: March 23, 2013, 06:58:31 am » |
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 
|
|
|
|
|
Logged
|
keep calm & carry on
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35573
Seattle, WA USA
|
 |
« Reply #29 on: March 23, 2013, 07:02:42 am » |
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
|
|
|
|
|
|