Argument in function name?

Hi all,

just bought an Arduino board and set up Onewire temperature measurement with the program available on Playground. It worked fine.
I modified the code with my very basic skills in C programming so that I could connect two 1-wire buses. It worked too and
now I want to clean up my program. It repeats the same piece of code twice so I want to make it a function.
The problem is with the function calls of the type

ds1.reset();
where 1/2 is the instance (bus) number

The bus number should be passed as an argument to the function name. Don't know if it is possible. I've been searching here and there but no clue. Maybe this is a silly question but I risk it.

from my code:

OneWire ds1(10); // 1-wire bus on pin 10
OneWire ds2(11); // second 1-wire bus on pin 11
.
.
.
for ( n = 0; n < k1; n++) {
ds1.reset();
ds1.select(addr1[n]);
ds1.write(0x44,1); // start conversion
delay(750); // maybe 750ms is enough, maybe not
present = ds1.reset();
ds1.select(addr1[n]);
ds1.write(0xBE); // Command read scratchpad
.
.
.
for ( n = 0; n < k2; n++) {
ds2.reset();
ds2.select(addr2[n]);
ds2.write(0x44,1); // start conversion
delay(750); // maybe 750ms is enough, maybe not
present = ds2.reset();
ds2.select(addr2[n]);
ds2.write(0xBE); // Command read scratchpad
.
.
.
So effectively lines of the type

ds1.reset(); should functionally be changed to something like:

ds*.reset(); where i is from 1 to 2 given in the calling line*

This is a very simple solution, I am new to arduino programming too and have only taken a basic C programming class, but this is how you would call a function to reset whichever sensor you wanted to..

void ResetSelector(byte i) //i is an arbitrary name it can be whatever you like, same with ResetSelector, byte just means your passing a value the size of a byte this can be int or whatever your using, it is called a void function because it doesn't return a value
{
 if(i==1)
{
ds1.reset()
}
if(i==2)
}
ds2.reset()
}
if(i==3)
{
ds1.reset()
ds2.reset()
}
}

Then you would call it like this

ResetSelector(1) //to reset the 1st 
ResetSelector(2) //to reset the 2nd
ResetSelector(3) //to reset both.
//you would also do this
byte variablename = 2;
ResetSelector(variablename)
// just make sure you pass it a byte if you set it up as a byte or an int if you set it up as an int

Hope this helps or gives you an idea of how to use a function

If you create an array of OneWire objects, you can store the instances in that array.

OneWire ds1(10);
OneWire ds2(11);

OneWire dss[] = { ds1, ds2 };

Then, you can reference the first device using ds1 or dss[0].

Consider writing the above as

OneWire dss[] = { OneWire(10), OneWire(11) };

That will save you some RAM as you only allocate two objects, not four. :slight_smile:

Thanks PaulS and others, that worked. Tested with 8 x DS18B20 on bus 1 and 2 x DS18B20 on bus 2. Maybe I'll add one more bus.

OneWire dss[] = { ds1, ds2 };
void RdTemp(int sensors, int bus, OneWire dss);
.
.
RdTemp(2, 1, dss[0]);
RdTemp(8, 2, dss[1]);
.
.
void RdTemp(int sensors, int bus, OneWire dss) {
.
.