-
Notifications
You must be signed in to change notification settings - Fork 0
/
Temperature.ino
94 lines (74 loc) · 2.09 KB
/
Temperature.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
void initTemperature() {
targetTemperature = 65;
currentTemperature = 65;
initSampling();
}
void initSampling() {
pollingIndex = 0;
temperaturePollingCache = targetTemperature;
}
void pollTemperature(boolean &hasData, float &temperature) {
// Read the temperature data
temperaturePollingCache += readTemperaturePin();
// If we have enough samples
// reset sampling data
// and manage the relay
if (pollingIndex >= SAMPLE_COUNT) {
float dF = convertAndAverageData();
initSampling();
hasData = true;
temperature = dF;
} else {
pollingIndex++;
hasData = false;
temperature = 0;
}
}
void recordRecords() {
if (currentTemperature > allTimeHigh) {
allTimeHigh = currentTemperature;
}
if (currentTemperature < allTimeLow) {
allTimeLow = currentTemperature;
}
}
float convertAndAverageData() {
float sampledAverage = temperaturePollingCache / (float) pollingIndex;
sampledAverage *= 9;
sampledAverage /= 5;
sampledAverage += 32;
return sampledAverage;
}
float readTemperaturePin() {
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !sensorController.search(addr)) {
//no more sensors on chain, reset search
sensorController.reset_search();
return -1000;
}
if (OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
sensorController.reset();
sensorController.select(addr);
sensorController.write(0x44,1); // start conversion, with parasite power on at the end
byte present = sensorController.reset();
sensorController.select(addr);
sensorController.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = sensorController.read();
}
sensorController.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}