-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsensors.cpp
162 lines (136 loc) · 3.74 KB
/
sensors.cpp
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
#include <OneWire.h>
#include <Time.h>
#include <Wire.h>
#include "vivarium.h"
#include "lcd.h"
#include "filestore.h"
#include "pitches.h"
const int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
// Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
struct sensor sensors[4];
int sensorViewBase=0;
int displaySwitch=0;
void sensorIdToBuffer(const int idx,char *buf) {
for(int i=0;i<8;i++) {
fmt2XDigits(buf,sensors[idx].addr[i]);
buf+=2;
}
buf[0]=0;
}
void showSensor(int sensor) {
if(sensor>=sensorCount) return;
char buf[21];
fmt2Digits(buf,sensor+1);
buf[2]=':';
buf[3]=' ';
sensorIdToBuffer(sensor,buf+4);
buf[20]=0;
write(buf);
}
// Scroll the sensors up
void scrollSensors() {
if(sensorCount<0) {
writeAt_P(1,2,PSTR("NO SENSORS YET"));
}
else {
at(1,2);
showSensor(sensorViewBase);
if(sensorCount>sensorViewBase) {
at(1,3);
showSensor(sensorViewBase+1);
}
sensorViewBase++;
if(sensorViewBase==sensorCount-1) {
sensorViewBase=0;
}
}
}
float getCTemp(byte *addr) {
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
float temperatureSum=0.0;
ds.reset();
ds.select(addr);
ds.write(0x44,0); // start conversion, without 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
temperatureSum = tempRead / 16;
return temperatureSum;
}
int scanbus() {
int sensorCount=0;
ds.reset_search();
while ( sensorCount<4 && ds.search(sensors[sensorCount].addr)) {
if ( OneWire::crc8( sensors[sensorCount].addr, 7) != sensors[sensorCount].addr[7]) {
Serial.println("CRC!");
continue;
}
if ( sensors[sensorCount].addr[0] != 0x10 && sensors[sensorCount].addr[0] != 0x28) {
Serial.print("Device! ");
continue;
}
sensorCount++;
}
return sensorCount;
}
#define ALARM_NOTES 1
int alarmBase=0;
int alarm[] = {
NOTE_G3, NOTE_A3 };
void readSensors() {
// Find sensors. Poll each time until we successfully get some
// TODO: Periodically scan for sensors and report any removed/new sensors
if(sensorCount==-1) {
int sc=scanbus();
if(sc>0) sensorCount=sc;
else return;
}
for(int i=0;i<sensorCount;i++) {
float temperature = getCTemp(sensors[i].addr);
sensors[i].value=temperature;
if(temperature<0 || temperature>100) {
sensors[i].valid=0;
continue;
}
// if(sensors[i].lastValue>0 && abs(sensors[i].lastValue-sensors[i].value)>5.0) {
// sensors[i].valid=0;
// sensors[i].lastValue=sensors[i].value;
// continue;
// }
sensors[i].lastValue=sensors[i].value;
sensors[i].valid=1;
int v=vivForSensor(i);
float target=0;
if(v!=-1 && now()-boot>3*60) { // 3*60 is to give the sensors time to settle
target=getTarget(v);
if(herp.vivs[v].relay_pin!=0) {
if(sensors[i].value<(herp.vivs[v].temp.target-TEMP_DELTA)) {
digitalWrite(herp.vivs[v].relay_pin,1);
sensors[i].relay_on=1;
}
else if(sensors[i].value>(herp.vivs[v].temp.target+TEMP_DELTA)) {
digitalWrite(herp.vivs[v].relay_pin,0);
sensors[i].relay_on=0;
}
}
float hi=getHi(v);
float lo=getLo(v);
if(!silence && (sensors[i].value<lo || sensors[i].value>hi)) {
tone(SOUNDER_PIN, alarm[alarmBase++],1000/4);
if(alarmBase>ALARM_NOTES) alarmBase=0;
} else {
noTone(SOUNDER_PIN);
}
}
logData(i,v,target);
}
}