-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS18B20_Get_Temp_without_Delay - stripped.ino
More file actions
48 lines (40 loc) · 1.7 KB
/
DS18B20_Get_Temp_without_Delay - stripped.ino
File metadata and controls
48 lines (40 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <OneWire.h> // OneWire library to make the OneWire bus usable
#include <DallasTemperature.h> // DS18B20 library for the DS18B20 temperature sensor
#define TEMPERATURE_SENSOR_PIN 11
#define MEASUREMENT_DELAY_MS 1000
#define TEMP_SENSOR_RESOLUTION 12
OneWire oneWireObject(TEMPERATURE_SENSOR_PIN); // Create OneWire object
DallasTemperature tempSensor(&oneWireObject); // Declare temperature sensor
DeviceAddress addressTempSensor; // Sensor address is ascertained in findAndSetupTempSensor()
unsigned long lastRequestTime = 0;
// Ueberpruefe ob Sensoren vorhanden sind
void findAndSetupTempSensor()
{
// Initialize temperature sensor
tempSensor.begin();
tempSensor.getAddress(addressTempSensor, 0);
tempSensor.setResolution(addressTempSensor, TEMP_SENSOR_RESOLUTION);
tempSensor.setWaitForConversion(false); // Enables temperature polling without blocking
tempSensor.requestTemperatures();
delay(10); // A short delay is recommended after requesting the current temperature
}
float getTemperature()
{
float temperature = tempSensor.getTempCByIndex(0); // The index must be changed if there are more than one sensor on the same wire/input pin.
tempSensor.requestTemperatures(); // Tell the sensor to make a temperature conversion. This may happen a bit later.
return temperature;
}
void setup() {
Serial.begin(9600);
findAndSetupTempSensor();
lastRequestTime = millis();
}
void loop() {
// Check if enough time has passed since last temperature request
if (millis() >= (lastRequestTime + MEASUREMENT_DELAY_MS))
{
lastRequestTime = millis();
float currentTemp = getTemperature();
Serial.println(currentTemp);
}
}