Reputation: 169
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!
Upvotes: 0
Views: 81
Reputation: 1368
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];
}
Upvotes: 1
Reputation: 193
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();
}
Upvotes: 0