Skip to content

Commit d1ebc3c

Browse files
committed
Merge pull request #28 from prashantdev/master
Making it simpler to use multiple OneWire buses with an arduino array
2 parents 146cfdd + 5626fb7 commit d1ebc3c

4 files changed

Lines changed: 63 additions & 1 deletion

File tree

DallasTemperature.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@ extern "C" {
1717
}
1818
#endif
1919

20+
DallasTemperature::DallasTemperature() {}
2021
DallasTemperature::DallasTemperature(OneWire* _oneWire)
2122
#if REQUIRESALARMS
2223
: _AlarmHandler(&defaultAlarmHandler)
2324
#endif
2425
{
26+
setOneWire(_oneWire);
27+
}
28+
void DallasTemperature::setOneWire(OneWire* _oneWire){
2529
_wire = _oneWire;
2630
devices = 0;
2731
parasite = false;
2832
bitResolution = 9;
2933
waitForConversion = true;
3034
checkForConversion = true;
3135
}
32-
3336
// initialise the bus
3437
void DallasTemperature::begin(void)
3538
{

DallasTemperature.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ class DallasTemperature
6464
{
6565
public:
6666

67+
DallasTemperature();
6768
DallasTemperature(OneWire*);
6869

70+
void setOneWire(OneWire*);
71+
6972
// initialise bus
7073
void begin(void);
7174

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Arduino Library for Dallas Temperature ICs
22
==========================================
33

4+
=========MODIFICATIONS BY PRASHANT========
5+
An extra no arguments constructor to simplify multiple OneWire buse use,
6+
allowing for easier construction of arrays of OneWire buses,
7+
in the limited environment provided by Arduino.
8+
See the Multibus_simple example illustrating a use-case of the extension.
9+
Use in conjunction with my modification of the OneWire library.
10+
=========END MODIFICATIONS BY PRASHANT========
11+
412
Usage
513
-----
614

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <OneWire.h>
2+
#include <DallasTemperature.h>
3+
4+
int oneWirePins[]={3,7};//OneWire DS18x20 temperature sensors on these wires
5+
const int oneWirePinsCount=sizeof(oneWirePins)/sizeof(int);
6+
7+
OneWire ds18x20[oneWirePinsCount];
8+
DallasTemperature sensor[oneWirePinsCount];
9+
10+
11+
void setup(void) {
12+
// start serial port
13+
Serial.begin(9600);
14+
Serial.println("Dallas Temperature Multiple Bus Control Library Simple Demo");
15+
Serial.print("============Ready with ");
16+
Serial.print(oneWirePinsCount);
17+
Serial.println(" Sensors================");
18+
19+
// Start up the library on all defined bus-wires
20+
DeviceAddress deviceAddress;
21+
for (int i=0; i<oneWirePinsCount; i++) {;
22+
ds18x20[i].setPin(oneWirePins[i]);
23+
sensor[i].setOneWire(&ds18x20[i]);
24+
sensor[i].begin();
25+
if (sensor[i].getAddress(deviceAddress, 0)) sensor[i].setResolution(deviceAddress, 12);
26+
}
27+
28+
}
29+
30+
void loop(void) {
31+
// call sensors.requestTemperatures() to issue a global temperature
32+
// request to all devices on the bus
33+
Serial.print("Requesting temperatures...");
34+
for (int i=0; i<oneWirePinsCount; i++) {
35+
sensor[i].requestTemperatures();
36+
}
37+
Serial.println("DONE");
38+
39+
delay(1000);
40+
for (int i=0; i<oneWirePinsCount; i++) {
41+
float temperature=sensor[i].getTempCByIndex(0);
42+
Serial.print("Temperature for the sensor ");
43+
Serial.print(i);
44+
Serial.print(" is ");
45+
Serial.println(temperature);
46+
}
47+
Serial.println();
48+
}

0 commit comments

Comments
 (0)