Hi all,
I'm trying to read DS2520 and compare 4 first digits to what i'm setting
and if the numbers is equal get indication on led
I'm always getting that numbers is not equal, althought i know from DS reading that the numbers is coorect
can anyone help please?
#include <OneWire.h>
OneWire ds(6);
int led = 13;
void setup() {
Serial.begin (9600);
pinMode(led, OUTPUT);
}
void loop() {
byte i;
boolean present;
byte data[32];
byte leemem[3] = {
0xF0 , 0x00 , 0x00 };
byte ccrc;
byte ccrc_calc;
byte check[4] = {0,0,2,5};
byte dat[32];
present = ds.reset();
ds.skip();
if (present == TRUE){
Serial.println("DS250x device present");
ds.write(leemem[0],1);
ds.write(leemem[1],1);
ds.write(leemem[2],1);
ccrc = ds.read();
ccrc_calc = OneWire::crc8(leemem, 3);
if ( ccrc_calc != ccrc) {
Serial.println("Invalid command CRC!");
Serial.print("Calculated CRC:");
Serial.println(ccrc_calc,HEX);
Serial.print("DS250x readback CRC:");
Serial.println(ccrc,HEX);
return;
}
if(data[0] == check[0] && data[1] == check[1] && data[2] == check[2] && data[3] == check[3]){
Serial.println("Check is good");
digitalWrite(led, HIGH);
delay(10000);
digitalWrite(led, LOW);
}
else
Serial.println("Check is bad");
digitalWrite(led, LOW);
Serial.println("Data is: ");
for ( i = 0; i < 32; i++) {
data[i] = ds.read();
Serial.print(data[i]);
Serial.print(" ");
}
Serial.println();
delay(5000);
}
else {
Serial.println("Nothing connected");
delay(3000);
}
}
Yes, PaulS is right.
Why don't you show the monitor result from this piece of code:
for ( i = 0; i < 32; i++) {
data[i] = ds.read();
Serial.print(data[i]);
Serial.print(" ");
}
if(data[0] == check[0] && data[1] == check[1] && data[2] == check[2] && data[3] == check[3]){
Serial.println("check good");
digitalWrite(led, HIGH);
delay(10000);
digitalWrite(led, LOW);
}
And why don't you do the if in 4 different if's to see where is the error?
If you do something like:
if(data[0] == check[0]) {
Serial.println("check 0 good");
if (data[1] == check[1]) {
Serial.println("check 1 good");
if (data[2] == check[2]) {
Serial.println("check 2 good");
if (data[3] == check[3]) {
Serial.println("check good");
digitalWrite(led, HIGH);
delay(10000);
digitalWrite(led, LOW);
}
}
}
}
EDIT: Other thing. If you see what you want to see in the serial monitor, is because your chech[] array is not well defined. It must be defined like:
byte check[4] = {'0','0','2','5'};
Here is the Serial output
Nothing connected
DS250x device present
Check is bad
Data is:
0 0 2 5 4 7 8 6 0 2 0 8 1 0 1 5 2 255 255 255 255 255 255 255 255 255 255 255 255 255 255 248
Yeap! You must define the check like I said.
luisilva,
I have tried to define like you said
here is the updated code:
#include <OneWire.h>
OneWire ds(6);
int led = 13;
void setup() {
Serial.begin (9600);
pinMode(led, OUTPUT);
}
void loop() {
byte i;
boolean present;
byte data[32];
byte leemem[3] = {
0xF0 , 0x00 , 0x00 };
byte ccrc;
byte ccrc_calc;
byte check[4] = {'0','0','2','5'};
byte dat[32];
present = ds.reset();
ds.skip();
if (present == TRUE){
Serial.println("DS250x device present");
ds.write(leemem[0],1);
ds.write(leemem[1],1);
ds.write(leemem[2],1);
ccrc = ds.read();
ccrc_calc = OneWire::crc8(leemem, 3);
if ( ccrc_calc != ccrc) {
Serial.println("Invalid command CRC!");
Serial.print("Calculated CRC:");
Serial.println(ccrc_calc,HEX);
Serial.print("DS250x readback CRC:");
Serial.println(ccrc,HEX);
return;
}
if(data[0] == check[0] && data[1] == check[1] && data[2] == check[2] && data[3] == check[3]){
Serial.println("Check is good");
digitalWrite(led, HIGH);
delay(10000);
digitalWrite(led, LOW);
}
else
Serial.println("Check is bad");
digitalWrite(led, LOW);
Serial.println("Data is: ");
for ( i = 0; i < 32; i++) {
data[i] = ds.read();
Serial.print(data[i]);
Serial.print(" ");
}
Serial.println();
delay(5000);
}
else {
Serial.println("Nothing connected");
delay(3000);
}
}
and still this is what i'm getting in the terminal:
Nothing connected
DS250x device present
Check is bad
Data is:
0 0 2 5 4 7 8 6 0 2 0 8 1 0 1 5 2 255 255 255 255 255 255 255 255 255 255 255 255 255 255 248
Why are you comparing the data BEFORE collecting it?
Where is the data array being set with the data from the device?
The only place I can see that data[] is initialised is when you print the data in the debug message. Before that it is undefined.
for ( i = 0; i < 32; i++)
{
data[i] = ds.read();
Serial.print(data[i]);
Serial.print(" ");
}
system
10
ok, while waiting for answer found that i'm reading before the data was read 
can anyone help with the code?
Arrch
11
ribhoo:
can anyone help with the code?
Read the data BEFORE you compare the values.
OK. Try this:
#include <OneWire.h>
OneWire ds(6);
int led = 13;
void setup() {
Serial.begin (9600);
pinMode(led, OUTPUT);
}
void loop() {
byte i;
boolean present;
byte data[32];
byte leemem[3] = {
0xF0 , 0x00 , 0x00
};
byte ccrc;
byte ccrc_calc;
byte check[4] = {'0', '0', '2', '5'};
byte dat[32];
present = ds.reset();
ds.skip();
if (present == TRUE) {
Serial.println("DS250x device present");
ds.write(leemem[0], 1);
ds.write(leemem[1], 1);
ds.write(leemem[2], 1);
ccrc = ds.read();
ccrc_calc = OneWire::crc8(leemem, 3);
if ( ccrc_calc != ccrc) {
Serial.println("Invalid command CRC!");
Serial.print("Calculated CRC:");
Serial.println(ccrc_calc, HEX);
Serial.print("DS250x readback CRC:");
Serial.println(ccrc, HEX);
return;
}
Serial.println("Data is: ");
for ( i = 0; i < 32; i++) {
data[i] = ds.read();
Serial.print(data[i]);
Serial.print(" ");
}
if (data[0] == check[0] && data[1] == check[1] && data[2] == check[2] && data[3] == check[3]) {
Serial.println("Check is good");
digitalWrite(led, HIGH);
delay(10000);
}
else {
Serial.println("Check is bad");
}
digitalWrite(led, LOW);
Serial.println();
delay(5000);
}
else {
Serial.println("Nothing connected");
delay(3000);
}
}
And find the differences.
system
13
Ok, solved it
here is the code:
#include <OneWire.h>
OneWire ds(6);
int led = 13;
void setup() {
Serial.begin (9600);
pinMode(led, OUTPUT);
}
void loop() {
byte i;
boolean present;
byte data[32];
byte leemem[3] = {
0xF0 , 0x00 , 0x00 };
byte ccrc;
byte ccrc_calc;
int check[4] = {0,0,2,5};
present = ds.reset();
ds.skip();
if (present == TRUE){
Serial.println("DS250x device present");
ds.write(leemem[0],1);
ds.write(leemem[1],1);
ds.write(leemem[2],1);
ccrc = ds.read();
ccrc_calc = OneWire::crc8(leemem, 3);
if ( ccrc_calc != ccrc) {
Serial.println("Invalid command CRC!");
Serial.print("Calculated CRC:");
Serial.println(ccrc_calc,HEX);
Serial.print("DS250x readback CRC:");
Serial.println(ccrc,HEX);
return;
}
Serial.println(" ");
for ( i = 0; i < 32; i++) {
data[i] = ds.read();
Serial.print(data[i]);
Serial.print(" ");
}
Serial.println();
if(data[0] == check[0] && data[1] == check[1] && data[2] == check[2] && data[3] == check[3]) {
Serial.println("Check is good");
digitalWrite(led, HIGH);
delay(10000);
digitalWrite(led, LOW);
}
else
Serial.print("Check is bad");
digitalWrite(led, LOW);
Serial.println();
delay(5000);
}
else {
Serial.println("Nothing connected");
delay(3000);
}
}
This is the first code? Because it seams to me that it will not work again.
Arrch
15
luisilva:
This is the first code? Because it seams to me that it will not work again.
Looks like it will work to me; the data is being read at the proper point now (before it's compared).
Arrch:
luisilva:
This is the first code? Because it seams to me that it will not work again.
Looks like it will work to me; the data is being read at the proper point now (before it's compared).

int check[4] = {0,0,2,5};
if(data[0] == check[0] && data[1] == check[1] && data[2] == check[2] && data[3] == check[3]) {
This is the result from the "serial monitor":
(...)
Data is:
0 0 2 5 4 7 8 6 0 2 0 8 1 0 1 5 2 255 255 255 255 255 255 255 255 255 255 255 255 255 255 248
(...)
Is that some joke Arrch?
Arrch
17
luisilva:
Is that some joke Arrch?
Why would it be?
0 == 0
0 == 0
2 == 2
5 == 5
The math checks out.
EDIT: OK. After went back and check the code, maybe I'm wrong (or certainly). I don't know why I thought I read
Serial.write(data[i]);
instead of:
Serial.print(data[i]);