Go Down

Topic: Changing the 1-Wire pin in the library OneWire (Read 468 times) previous topic - next topic

ogogon

May 07, 2011, 06:42 pm Last Edit: May 08, 2011, 01:22 am by ogogon Reason: 1
In all the examples use the library OneWire, pin control 1-Ware bus is given explicitly when compiling sketch.

Can I change pin from a program?

Ogogon.

Nick Gammon

It's just a variable:

Code: [Select]
#include <OneWire.h>

/* DS18S20 Temperature chip i/o */

OneWire  ds(10);  // on pin 10

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


Instead of 10 use x, where x can change.

Ah, I see the problem, it is in the constructor. Well you could fiddle around and change the library a bit.

Do something like make the constructor take no arguments. Then make a "begin" function that does what the constructor used to do. And call ds.begin (x) where x is the pin you want to use.
Please post technical questions on the forum, not by personal message. Thanks!

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

ogogon


Well you could fiddle around and change the library a bit.

I had to slightly modify the library.
Remove from the constructor initialization pin to the function.

Code: [Select]

OneWire::OneWire( uint8_t pinArg)
{
     pinset (pinArg);
}

void OneWire::pinset (uint8_t pinArg)
{
     pin = pinArg;
     port = digitalPinToPort (pin);
     bitmask =  digitalPinToBitMask (pin);
     outputReg = portOutputRegister (port);
     inputReg = portInputRegister (port);
     modeReg = portModeRegister (port);
#if ONEWIRE_SEARCH
     reset_search ();
#endif
}



In fact, the designer should be without this argument, and it should already be set in the setup() section.
But this will lead to incompatibilities with previous versions.

Ogogon.

Go Up