Files

67 lines
1.8 KiB
Arduino
Raw Permalink Normal View History

2023-03-17 11:59:21 +00:00
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
2026-03-20 17:18:15 +00:00
// Pass our oneWire reference to Dallas Temperature.
2023-03-17 11:59:21 +00:00
DallasTemperature sensors(&oneWire);
void setup(void)
{
// start serial port
Serial.begin(115200);
Serial.println("Dallas Temperature Control Library - Async Demo");
Serial.println("\nDemo shows the difference in length of the call\n\n");
// Start up the library
sensors.begin();
}
void loop(void)
2026-03-20 17:18:15 +00:00
{
2023-03-17 11:59:21 +00:00
// Request temperature conversion (traditional)
Serial.println("Before blocking requestForConversion");
2026-03-20 17:18:15 +00:00
unsigned long start = millis();
2023-03-17 11:59:21 +00:00
sensors.requestTemperatures();
unsigned long stop = millis();
Serial.println("After blocking requestForConversion");
Serial.print("Time used: ");
Serial.println(stop - start);
2026-03-20 17:18:15 +00:00
2023-03-17 11:59:21 +00:00
// get temperature
Serial.print("Temperature: ");
2026-03-20 17:18:15 +00:00
Serial.println(sensors.getTempCByIndex(0));
2023-03-17 11:59:21 +00:00
Serial.println("\n");
2026-03-20 17:18:15 +00:00
2023-03-17 11:59:21 +00:00
// Request temperature conversion - non-blocking / async
Serial.println("Before NON-blocking/async requestForConversion");
2026-03-20 17:18:15 +00:00
start = millis();
2023-03-17 11:59:21 +00:00
sensors.setWaitForConversion(false); // makes it async
sensors.requestTemperatures();
sensors.setWaitForConversion(true);
stop = millis();
Serial.println("After NON-blocking/async requestForConversion");
Serial.print("Time used: ");
2026-03-20 17:18:15 +00:00
Serial.println(stop - start);
// 9 bit resolution by default
2023-03-17 11:59:21 +00:00
// Note the programmer is responsible for the right delay
// we could do something usefull here instead of the delay
int resolution = 9;
2026-03-20 17:18:15 +00:00
delay(750 / (1 << (12 - resolution)));
2023-03-17 11:59:21 +00:00
// get temperature
Serial.print("Temperature: ");
2026-03-20 17:18:15 +00:00
Serial.println(sensors.getTempCByIndex(0));
Serial.println("\n\n\n\n");
delay(1500);
2023-03-17 11:59:21 +00:00
}