forked from hydronics2/Bee-counter-3.1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter
288 lines (227 loc) · 7.93 KB
/
counter
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
counter_
Benjamin Foote and Thomas Hudson
May 8th 2015
BeeCounter project
6/10/2015 - changed IP address, updated code for IN/OUT counting
9/3/2016 - posted on github
*/
#include <TimerOne.h>
#define LED 13
#define NUMBER_OF_SHIFT_CHIPS 3
#define DATA_WIDTH NUMBER_OF_SHIFT_CHIPS * 8
#define PULSE_WIDTH_USEC 5
#define POLL_DELAY_MSEC 1
#define BYTES_VAL_T unsigned long //change to long if number_of_shift_chips is higher than 2
//-----------------------------------------BEE COUNTING variables
int ploadPin = 5; // ORANGE - Connects to /PL aka Parallel load aka latch pin the 165
int dataPin = 6; // BLUE - Connects to the Q7 aka MISO pin the 165
int clockPin = 7; // GREEN - Connects to the Clock pin the 165
BYTES_VAL_T pinValues;
BYTES_VAL_T lastPinValues;
const int numberOfGates = 12; // 12 gates, 24 sensors
const int startGate = 0; //useful for testing
const int endGate = 12; //useful for testing
const int debeebounce = 120;
const int outputDelay = 15000; //prints bee counts every 15 seconds
unsigned long lastOutput = 0;
unsigned long currentTime = 0;
int inPin[numberOfGates];
int outPin[numberOfGates];
int inCount[numberOfGates];
int outCount[numberOfGates];
int inReading[numberOfGates];
int outReading[numberOfGates];
int lastInReading[numberOfGates];
int lastOutReading[numberOfGates];
unsigned long inReadingTime[numberOfGates];
unsigned long outReadingTime[numberOfGates];
unsigned long lastInReadingTime[numberOfGates];
unsigned long lastOutReadingTime[numberOfGates];
int inReadingTimeHigh[numberOfGates];
int outReadingTimeHigh[numberOfGates];
int lastInTime[numberOfGates];
int lastOutTime[numberOfGates];
int inReadingTimeComparator[numberOfGates];
int outReadingTimeComparator[numberOfGates];
int lastInReadingTimeHigh[numberOfGates];
int lastOutReadingTimeHigh[numberOfGates];
int totalTimeTravelGoingOut[numberOfGates];
int totalTimeTravelGoingIn[numberOfGates];
int firstTestInVariable[numberOfGates];
int firstTestOutVariable[numberOfGates];
int inTotal;
int outTotal;
int j = 0;
int n = 0;
/* This function is essentially a "shift-in" routine reading the
* serial Data from the shift register chips and representing
* the state of those pins in an unsigned integer (or long).
*/
BYTES_VAL_T read_shift_regs()
{
long bitVal;
BYTES_VAL_T bytesVal = 0;
/* Trigger a parallel Load to latch the state of the data lines,
*/
digitalWrite(ploadPin, LOW);
delayMicroseconds(PULSE_WIDTH_USEC);
digitalWrite(ploadPin, HIGH);
/* Loop to read each bit value from the serial out line
* of the SN74HC165N.
*/
for(int i = 0; i < DATA_WIDTH; i++)
{
bitVal = digitalRead(dataPin);
/* Set the corresponding bit in bytesVal.
*/
bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));
/* Pulse the Clock (rising edge shifts the next bit).
*/
digitalWrite(clockPin, HIGH);
delayMicroseconds(PULSE_WIDTH_USEC);
digitalWrite(clockPin, LOW);
}
return(bytesVal);
}
void pinValuesToGates()
{
j = 0;
for(int i = 0; i < DATA_WIDTH; i++)
{
if((pinValues >> i) & 1)
outReading[j] = 1;
else
outReading[j] = LOW;
i++;
if((pinValues >> i) & 1)
inReading[j] = 1;
else
inReading[j] = LOW;
j++;
}
}
void printGates() {
for(int i = 0; i < numberOfGates; i++){
Serial.print("outReading ");
Serial.print(i);
Serial.print(": ");
Serial.println(outReading[i]);
Serial.print(" inReading ");
Serial.print(i);
Serial.print(": ");
Serial.println(inReading[i]);
}
Serial.println("");
}
void setup() {
Serial.begin(115200);
pinMode(ploadPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
digitalWrite(clockPin, LOW);
digitalWrite(ploadPin, HIGH);
/* Read in and display the pin states at startup.
*/
Serial.println("testing at startup");
delay(1000);
pinValues = read_shift_regs();
pinValuesToGates();
printGates();
lastPinValues = pinValues;
}
void loop() {
currentTime = millis();
/* Read the state of all sensors.*/
pinValues = read_shift_regs();
/* If there was a chage in state, display which ones changed.
*/
if(pinValues != lastPinValues) {
//printGates();
//printGates();
pinValuesToGates();
lastPinValues = pinValues;
for (int i = startGate; i < endGate; i++) {
// has anything changed going IN?
if(inReading[i] != lastInReading[i]) { //change of state
if(currentTime - inReadingTime[i] > debeebounce){ //deebounce sensor 120ms
//Serial.print(i);
//Serial.print(", high_or_low: ");
//Serial.print(inReading[i]);
if(inReading[i] == 0){ //a bee just left the sensor; that is, it was HIGH, now it is LOW (empty)
lastInReadingTime[i] = currentTime;
//Serial.print("_ IN sensor HIGH for: ");
//Serial.print(currentTime - inReadingTime[i]);
//Serial.print(", current time: ");
//Serial.println(currentTime);
inReadingTimeHigh[i] = currentTime - inReadingTime[i]; //this variable is how long the bee was present for
if(currentTime - lastOutReadingTime[i] < 150){ //this variable indicates that the leading edge of the outside sensor was just triggered... indicating the bees movement is in the out direction
if(inReadingTimeHigh[i] < 350 || outReadingTimeHigh[i] < 350){ ///if all these things are true... a bee has left, count it. any longer times indicates not sure which way the bee is really headed
outCount[i]++;
outTotal++;
//Serial.println("");
//Serial.print("OUT: ");
//Serial.println(outTotal);
}
}
}
if(inReading[i] == 1){
//Serial.print("_ IN sensor LOW for: ");
//Serial.print(currentTime - inReadingTime[i]);
//Serial.print(", current time: ");
//Serial.println(currentTime);
}
}
inReadingTime[i] = currentTime;
lastInReading[i] = inReading[i];
}
// has anything changed going OUT
if(outReading[i] != lastOutReading[i]) {
if(currentTime - outReadingTime[i] > debeebounce){ //deebounce sensor
//Serial.print(i);
//Serial.print(", high_or_low: ");
//Serial.print(outReading[i]);
if(outReading[i] == 0){
lastOutReadingTime[i] = currentTime;
//Serial.print("_OUT sensor HIGH for: ");
//Serial.print(currentTime - outReadingTime[i]);
//Serial.print(", current time: ");
//Serial.println(currentTime);
outReadingTimeHigh[i] = currentTime - outReadingTime[i];
if(currentTime - lastInReadingTime[i] < 150){
if(inReadingTimeHigh[i] < 350 || outReadingTimeHigh[i] < 350){ ///if all these things are true... a bee has entered, count it
inCount[i]++;
inTotal++;
//Serial.println("");
//Serial.print("in: ");
//Serial.println(inTotal);
}
}
}
if(outReading[i] == 1){
//Serial.print("_OUT sensor LOW for: ");
//Serial.print(currentTime - outReadingTime[i]);
//Serial.print(", current time: ");
//Serial.println(currentTime);
}
}
outReadingTime[i] = currentTime;
lastOutReading[i] = outReading[i];
}
}
}
if (currentTime - lastOutput > outputDelay) {
Serial.println("sending data");
sendData();
lastOutput = currentTime;
inTotal = 0;
outTotal = 0;
}
}
void sendData() {
Serial.print("OUT: ");
Serial.println(outTotal);
Serial.print("in: ");
Serial.println(inTotal);
// over wifi or ethernet or serial
}