Pages: [1]   Go Down
Author Topic: Q: OneWire library usage  (Read 282 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8855
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That doesn't compile:

Code:
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:
#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 () {}
Logged


Pages: [1]   Go Up
Jump to: