forked from daveake/FlexTrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathds18b20.ino
66 lines (56 loc) · 2.03 KB
/
ds18b20.ino
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* ========================================================================== */
/* ds18b20.ino */
/* */
/* Code for reading OneWire Temperature devices/averaging ADC channels */
/* */
/* */
/* */
/* ========================================================================== */
// Variables
#ifdef WIREBUS
#include <OneWire.h>
#include <DallasTemperature.h>
#define MAX_SENSORS 4
// Variables
int SensorCount=0; // Number of temperature devices found
unsigned long CheckDS18B20s=0;
int GettingTemperature=0;
OneWire oneWire(WIREBUS); // OneWire port
DallasTemperature sensors(&oneWire); // Pass oneWire reference to Dallas Temperature object
int DS18B20_Temperatures[MAX_SENSORS];
void Setupds18b20(void)
{
sensors.begin();
// Grab a count of devices on the wire
SensorCount = sensors.getDeviceCount();
Serial.print(SensorCount);
Serial.println(" DS18B20's on bus");
SensorCount = min(SensorCount, MAX_SENSORS);
if (SensorCount > 0)
{
sensors.setResolution(9);
}
}
void Checkds18b20(void)
{
if (millis() >= CheckDS18B20s)
{
if (GettingTemperature)
{
int i;
for (i=0; i<SensorCount; i++)
{
DS18B20_Temperatures[i] = sensors.getTempCByIndex(i);
Serial.print("Temperature "); Serial.print(i); Serial.print(" = "); Serial.print(DS18B20_Temperatures[i]); Serial.println("degC");
}
CheckDS18B20s = millis() + 10000L;
}
else
{
sensors.requestTemperatures(); // Send the command to get temperature
CheckDS18B20s = millis() + 1000L; // Leave 1 second (takes 782ms) for readings to happen
}
GettingTemperature = !GettingTemperature;
}
}
#endif