-
Notifications
You must be signed in to change notification settings - Fork 1
/
OneWireTemperature.ino
220 lines (177 loc) · 4.04 KB
/
OneWireTemperature.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "MicroView.h"
#include "myMicroView.h"
#include <OneWire.h>
#include <math.h>
//DS18S20 Signal pin on digital 3 (uView 12)
#define DS18S20_Pin 3
OneWire ds(DS18S20_Pin);
// Temp variables
// number of samples to use for running average and smoothing
#define avgreadings 12
#define smoothreadings 5
float tmin = 85.; // min temp
float tmax = -10.; // max temp
float tavg = 0.; // avg temp
float tavgprev = 0.; // previous avg temp
float avgsamples[avgreadings]; // samples for average
float smoothsamples[smoothreadings]; // samples for smoothing
float totavg = 0.; // running total for average
float totsmooth = 0.; // running total for smoothing
int si = 0; // smoothing index
int ai = 0; // average index
int displayType = 0; // type of temperature to display
void setup() {
#ifdef SERIALDEBUG
Serial.begin(9600);
#endif
uView.begin();
for (int i = 0; i < avgreadings; i++) {
avgsamples[i] = 0.;
}
for (int i = 0; i < smoothreadings; i++) {
smoothsamples[i] = 0.;
}
// throw away the first readings to initialize all smoothing values
for (int i = 0; i < smoothreadings; i++) {
float tbad = smoothTemp();
displayTemp('.');
}
// initialize the average temperature and the sample set
for (int i = 0; i < avgreadings; i++) {
avgsamples[i] = smoothTemp();
totavg += avgsamples[i];
displayTemp('*');
}
// set average, min and max temp
tavg = totavg / avgreadings;
tavgprev = tavg;
if (tavg > tmax) {
tmax = tavg;
}
if (tavg < tmin) {
tmin = tavg;
}
}
void loop() {
totavg -= avgsamples[ai];
avgsamples[ai] = smoothTemp();
totavg += avgsamples[ai];
if (++ai >= avgreadings) {
ai = 0;
}
// set average, min and max temp
tavgprev = tavg;
tavg = totavg / avgreadings;
if (tavg > tmax) {
tmax = tavg;
}
if (tavg < tmin) {
tmin = tavg;
}
switch (displayType){
case 0:
displayTemp(tavg, displayType);
break;
case 1:
if (tavg > tavgprev) {
displayTemp(UP_ARROW);
}
if (tavg < tavgprev) {
displayTemp(DOWN_ARROW);
}
break;
case 2:
displayTemp(tmin, displayType);
break;
case 3:
displayTemp(tmax, displayType);
}
if (++displayType > 3) {
displayType = 0;
}
#ifdef SERIALDEBUG
Serial.println(tavg);
#endif
}
// Compute Temperature Smoothed Value
float smoothTemp() {
totsmooth -= smoothsamples[si];
smoothsamples[si] = round(getTemp() * 100) / 100; // 1 decimal precision
totsmooth += smoothsamples[si];
if (++si >= smoothreadings) {
si = 0;
}
return (totsmooth / smoothreadings);
}
// Display Temperature
void displayTemp(float temp, int type) {
uView.clear(PAGE);
uView.setCursor(0, 0);
uView.setFontType(FONT_font8x16);
switch (type) {
// current temp
case 0:
uView.println("T. (C):");
break;
// tmin
case 2:
uView.println("T.MIN.:");
break;
// tmax
case 3:
uView.println("T.MAX.:");
}
uView.println(temp);
uView.display();
delay(1000);
}
void displayTemp(char temp) {
uView.clear(PAGE);
uView.setCursor(0, 0);
uView.setFontType(FONT_font8x16);
uView.println("T. (C):");
uView.println(temp);
uView.display();
delay(1000);
}
void displayTemp(char *temp) {
uView.clear(PAGE);
uView.setCursor(27, 10);
uView.setFontType(FONT_Arrows);
uView.println(*temp);
uView.display();
delay(1000);
}
//returns the temperature from one DS18S20 in DEG Celsius
float getTemp() {
byte data[12];
byte addr[8];
if (!ds.search(addr)) {
//no more sensors on chain, reset search
ds.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;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.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;
}