0

From a bunch of 1-wire devices I want to write the devices rom addresses to an array. I have tried many options, but obviously I don't have a clue how to do this right.

In the code below, in the device search loop I get the adress printed on the serial line as I expect it. But the printed output of the main loop indicates that I am way of in my method to store this address in an array....

#include <OneWire.h>
// http://www.pjrc.com/teensy/td_libs_OneWire.html
OneWire  ds(2);

void setup(void) {
   Serial.begin(9600);
   while (!Serial) {   
  }
}

unsigned char UIDs[12];
int indx=0;

void getDeviceAddresses(void)
{
 int i=0;
 byte present = 0;
 byte done = 0;
 byte data[12];
 byte addr[8];

 while ( !done )
 {
   if ( ds.search(addr) != 1)
   {
     Serial.print("No more addresses.\n");
     ds.reset_search();
     done = 1;
     delay(1000);
     indx=0;
     return;
   }
   else
   {
     Serial.print("Sensors");
     Serial.print(indx);
     Serial.print(" address is:\t");
     indx++;
    //read each byte in the address array
    for( i = 0; i < 8; i++) {
      //Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');   
      }
      // print each byte in the address array in hex format
      UIDs[indx]=(UIDs[indx]+(addr[i], HEX));                    // I guess this is not how to do it....
      Serial.print(addr[i], HEX);
    }
  }
  Serial.println();
 }
}

void loop (){
  getDeviceAddresses();
  int i=0;
  while (true) {
    for ( indx = 0; indx < 13; indx++) {
      Serial.println(UIDs[indx]);    
    }
   delay(4000);
  }
}
Sensors0 address is:    106C402502080064
Sensors1 address is:    101E3C25020800DE
Sensors2 address is:    10614C250208000F
Sensors3 address is:    10513325020800E0
Sensors4 address is:    10094B250208003C
Sensors5 address is:    104D342502080097
Sensors6 address is:    10FD4025020800E2
Sensors7 address is:    10534025020800AD
Sensors8 address is:    1047672502080083
No more addresses.
0
128
128
128
128
128
128
128
128
128
0
0
12

oszkar
  • 865
  • 5
  • 21
kopke
  • 67
  • 5
  • Your main issue is that you have an array of unsigned char and you're trying to stick strings into it. Each slot of that array can hold ONE character and that's it. If you want to store an array of strings then you're going to need a 2D array or an array of pointers. – Delta_G Apr 05 '20 at 20:03

2 Answers2

2

It looks like addr is an array of 8 bytes holding your address.

If you define as:

unsigned char UIDs[12][8];

Then on each pass through your function you have:

{
     Serial.print("Sensors");
     Serial.print(indx);
     Serial.print(" address is:\t");
     indx++;
    //read each byte in the address array
    for( i = 0; i < 8; i++) {
      //Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');   
      }
      // print each byte in the address array in hex format
      UIDs[indx][i] = addr[i];         
      Serial.print(addr[i], HEX);
    }
  }

And finally in your loop to print them:

for ( indx = 0; indx < 13; indx++) {
      for (int i=0; i<8; i++){
         if(UIDs[indx][i] < 16){
            Serial.print('0');
         }
      Serial.print(UIDs[indx][i], HEX);    
    }
Delta_G
  • 2,927
  • 2
  • 9
  • 15
0

@Delta_G - Thanks!

I tried the 2 dimensional array first but could not get it right. Think I messed up the second printing routine. For future reference in case somebody needs this to, this is a complete working test code.

unsigned char UIDs[12][8];
byte indx=0;
byte nr_of_devices=0;
#include <OneWire.h>
// http://www.pjrc.com/teensy/td_libs_OneWire.html
OneWire  ds(2);

void setup(void) {
   Serial.begin(9600);
   while (!Serial) {   
  }
}

void getDeviceAddresses(void)
{
 int i=0;
 byte present = 0;
 byte done = 0;
 byte data[12];
 byte addr[8];

 while ( !done )
 {
   if ( ds.search(addr) != 1)
   {
     Serial.print("No more addresses.\n");
     ds.reset_search();
     done = 1;
     delay(1000);
     nr_of_devices=indx;
     indx=0;
     return;
   }
   else
   {
     Serial.print("Sensors");
     Serial.print(indx);
     Serial.print(" address is:\t");
     indx++;
    //read each byte in the address array
    for( i = 0; i < 8; i++) {
      //Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');    
      }
      // print each byte in the address array in hex format
      UIDs[indx][i] = addr[i];                    
      Serial.print(addr[i], HEX);
      if (!i >= 1) {
        Serial.print("-");
      }
    }
  }
  Serial.println();
 }
}

void loop (){
  getDeviceAddresses();
  int i=0;
  while (true) {
  Serial.println("Sensors found:");
  for ( indx = 1; indx < nr_of_devices; indx++) {
    Serial.print(indx);Serial.print(": ");
      for (int i=0; i<8; i++){
         if(UIDs[indx][i] < 16){
            Serial.print('0');
         }
      Serial.print(UIDs[indx][i], HEX);    

    }
   Serial.println("");
  }
  delay(4000);
  }
}
kopke
  • 67
  • 5