|
| 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