Q: OneWire library usage

Hi,

I have a question about OneWire (http://www.pjrc.com/teensy/td_libs_OneWire.html) library. Documentations and examples says that I have to define pin nr to create OneWire object in sketch header. Is it possible to define OneWire pin dynamically?

#include <OneWire.h>

/* DS18S20 Temperature chip i/o */

OneWire  ds(10);  // on pin 10

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

For conclusion I need to define and read OneWire pin nr-s from EEPROM. I’ll write OneWire pin nr over serial into EEPROM and every time Arduino should take pin nr from EEPROM and use it. It does not matter if EEPROM reading takes in loop or in setup functions. Can someone give some hints or explain how to release this idea of using dynamic pin selector.

Many thanks.

#include <OneWire.h>

OneWire  *ds;

void setup(void) {
  Serial.begin(9600);
  int dsPin = EEPROM.read(addressOfdsPin);
  ds = OneWire(dsPin);
}

In your code, use ds->read() in place of ds.read(). If you need to pass the object to a function, use *ds in place of ds.

That doesn’t compile:

sketch_dec26b.cpp: In function 'void setup()':
sketch_dec26b:10: error: cannot convert 'OneWire' to 'OneWire*' in assignment

However if you use “new” it does:

#include <OneWire.h>
#include <EEPROM.h>

OneWire  *ds;

const byte addressOfdsPin = 0;  // or wherever, in EEPROM

void setup(void) {
  Serial.begin(9600);
  byte dsPin = EEPROM.read(addressOfdsPin);
  ds = new OneWire(dsPin);
}

void loop () {}