0

I have five identical DS2401 unique ID chips attached to an Arduino, and I wish to read the serial number from each consecutively into RAM so that I can send the data over serial.

I have each one initialised separately, so that I can call the code to retrieve the serial number with ds24_0.getSerialNumber(), ds24_1.getSerialNumber() etc.

If I were to build these into a for loop, how do I get the code to call ds24_i, where i is the loop iteration number? Am I going about this the wrong way?

OneWire oneWire_0(10);
DS2401 ds24_0(&oneWire_0);
OneWire oneWire_1(A0);
DS2401 ds24_1(&oneWire_1);
OneWire oneWire_2(A1);
DS2401 ds24_2(&oneWire_2);
OneWire oneWire_3(A2);
DS2401 ds24_3(&oneWire_3);
OneWire oneWire_4(A3);
DS2401 ds24_4(&oneWire_4);

//snip

void idRequest()
{
  uint8_t serialNumber[5][6];
  uint8_t result[5];

  result[0] = ds24_0.init();  //how do I loop this
  result[1] = ds24_1.init();
  result[2] = ds24_2.init();
  result[3] = ds24_3.init();
  result[4] = ds24_4.init();

  if(result[0] == DS2401_SUCCESS) 
  {
    ds24_0.getSerialNumber(serialNumber[0]);  //how do I loop this also?
  }

  else if(result[0] == DS2401_CRC_FAIL || DS2401_NOT_DS2401 || DS2401_NO_WIRE) 
    {
      for (uint8_t i = 0; i < 6; i++) 
      {
        serialNumber[0][i] = 0;
      }
    }
//assemble packet here
}

Thanks!

8
  • What are ds24_0 and so on? Where are they defined? Commented Mar 3, 2020 at 11:25
  • @PaulHankin I have added the definitions to the code Commented Mar 3, 2020 at 11:27
  • 1
    You define them yourself? Why not use an array rather than 5 variables? Commented Mar 3, 2020 at 11:29
  • @PaulHankin because I don't know how to do that, that's why I wrote this post. Commented Mar 3, 2020 at 11:29
  • @JoeofLoath what is A1, A2, A3 ... ? Commented Mar 3, 2020 at 11:33

2 Answers 2

1

In define:

 #define NB_PIN 5  

In code:

 uint8_t tabUnit[NB_PIN]={10, 0xA0, 0xA1, 0xA2, 0xA3};
 OneWire *tabOneWire[NB_PIN];
 DS2401  *tabDS2401[NB_PIN];
 uint8_t  result[NB_PIN];

 for ( int i=0; i<NB_PIN; i++)
 {
   tabOneWire[i] = new OneWire (tabUnit[i]);
   tabDS2401 [i] = new DS2401 (tabOneWire[i]);
   result [i]    = tabDS2401[i]->init();
 }

 // .........

 for ( int i=0; i<NB_PIN; i++)
 {
    delete tabDS2401 [i];
    delete tabOneWire[i];
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! If I'm following this correctly we create instances of onewire and ds2401 locally, use them iteratively, then delete them again?
@JoeofLoath with operator new your objects can be used from any scope and not just locally (as I didn't know your constraints in terms of scope visibility). At the end of their use, you must destroy them to free resources as well as not to have memory leaks.
Thank you, this is working now! I had one issue - tabDS2401 [i] = new DS2401 (*tabOneWire[i]); fails to compile as tabOneWire[i] is already a pointer - remove the asterisk and it compiles and runs properly.
@JoeofLoath Ok, Thanks for the remark. I update the post code.
0

Put the ds24's in an array.

auto ds24s = {ds24_0, ds24_1, ds24_2, ds24_3, ds24_4};
for (int i = 0; i < 5; i++)
{
    result[i] = ds24s[i].init();
}

2 Comments

Thanks, I'm unfamiliar with the auto keyword. However, this fails - looks like an Arduino bug: github.com/arduino/Arduino/issues/6609
Are you sure that copying DS2401 objects is sensible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.