Go Down

Topic: Q: OneWire library usage (Read 312 times) previous topic - next topic

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?
Code: [Select]
#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.

johnwasser

Code: [Select]

#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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Nick Gammon

That doesn't compile:

Code: [Select]

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:

Code: [Select]

#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 () {}
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up