11#include < OneWire.h>
22#include < DallasTemperature.h>
33
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-
4+ OneWire ds18x20[] = { 3 , 7 };
5+ const int oneWireCount = sizeof (ds18x20)/sizeof (OneWire);
6+ DallasTemperature sensor[oneWireCount];
107
118void setup (void ) {
129 // start serial port
1310 Serial.begin (9600 );
1411 Serial.println (" Dallas Temperature Multiple Bus Control Library Simple Demo" );
1512 Serial.print (" ============Ready with " );
16- Serial.print (oneWirePinsCount );
13+ Serial.print (oneWireCount );
1714 Serial.println (" Sensors================" );
1815
1916 // Start up the library on all defined bus-wires
2017 DeviceAddress deviceAddress;
21- for (int i=0 ; i<oneWirePinsCount; i++) {;
22- ds18x20[i].setPin (oneWirePins[i]);
18+ for (int i = 0 ; i < oneWireCount; i++) {;
2319 sensor[i].setOneWire (&ds18x20[i]);
2420 sensor[i].begin ();
2521 if (sensor[i].getAddress (deviceAddress, 0 )) sensor[i].setResolution (deviceAddress, 12 );
2622 }
27-
2823}
2924
3025void loop (void ) {
3126 // call sensors.requestTemperatures() to issue a global temperature
3227 // request to all devices on the bus
3328 Serial.print (" Requesting temperatures..." );
34- for (int i= 0 ; i<oneWirePinsCount ; i++) {
29+ for (int i = 0 ; i < oneWireCount ; i++) {
3530 sensor[i].requestTemperatures ();
3631 }
3732 Serial.println (" DONE" );
3833
3934 delay (1000 );
40- for (int i= 0 ; i<oneWirePinsCount ; i++) {
41- float temperature= sensor[i].getTempCByIndex (0 );
35+ for (int i = 0 ; i < oneWireCount ; i++) {
36+ float temperature = sensor[i].getTempCByIndex (0 );
4237 Serial.print (" Temperature for the sensor " );
4338 Serial.print (i);
4439 Serial.print (" is " );
4540 Serial.println (temperature);
4641 }
4742 Serial.println ();
48- }
43+ }
0 commit comments