Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <OneWire.h>
- #include <DallasTemperature.h>
- // Data wire is plugged into port 2 on the Arduino
- #define ONE_WIRE_BUS 10
- #define TEMPERATURE_PRECISION 8
- #define BAUD_RATE 9600
- // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature.
- DallasTemperature sensors(&oneWire);
- // arrays to hold device addresses
- DeviceAddress devices[10];
- int devicesFound = 0;
- void setup(void)
- {
- // start serial port
- Serial.begin(BAUD_RATE);
- sensors.begin();
- devicesFound = sensors.getDeviceCount();
- // locate devices on the bus
- Serial.print("Locating devices...");
- Serial.print("Found ");
- Serial.print(devicesFound, DEC);
- Serial.println(" devices.");
- // report parasite power requirements
- Serial.print("Parasite power is: ");
- if (sensors.isParasitePowerMode()) Serial.println("ON");
- else Serial.println("OFF");
- for (int i = 0; i < devicesFound; i++)
- if (!sensors.getAddress(devices[i], i))
- Serial.println("Unable to find address for Device" + i);
- // show the addresses we found on the bus
- for (int i = 0; i < devicesFound; i++)
- {
- Serial.print("Device " + (String)i + " Address: ");
- printAddress(devices[i]);
- Serial.println();
- }
- for (int i = 0; i < devicesFound; i++)
- sensors.setResolution(devices[i], TEMPERATURE_PRECISION);
- }
- // function to print a device address
- void printAddress(DeviceAddress deviceAddress)
- {
- for (uint8_t i = 0; i < 8; i++)
- {
- // zero pad the address if necessary
- if (deviceAddress[i] < 16) Serial.print("0");
- Serial.print(deviceAddress[i], HEX);
- }
- }
- // function to print the temperature for a device
- void printTemperature(DeviceAddress deviceAddress)
- {
- float tempC = sensors.getTempC(deviceAddress);
- if (tempC < 10)
- Serial.print("0");
- Serial.print(tempC);
- }
- // function to print a device's resolution
- void printResolution(DeviceAddress deviceAddress)
- {
- Serial.print("Resolution: ");
- Serial.print(sensors.getResolution(deviceAddress));
- Serial.println();
- }
- // main function to print information about a device
- void printData(DeviceAddress deviceAddress)
- {
- Serial.print("Device Address: ");
- printAddress(deviceAddress);
- Serial.print(" ");
- printTemperature(deviceAddress);
- Serial.println();
- }
- void loop(void)
- {
- if (devicesFound == 0)
- {
- Serial.println("No devices found.");
- return;
- }
- int start=millis();
- sensors.requestTemperatures();
- // print the device information
- for (int i = 0; i < devicesFound; i++)
- {
- printTemperature(devices[i]);
- if (i != devicesFound - 1)
- Serial.print(" ");
- }
- Serial.println();
- Serial.print(millis() - start);
- Serial.println(" ms passed.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement