Multibus_simple.ino 1.3 KB

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