# include "fcs.h"
# include "jbutton.h"
unsigned long now;
// polling period. TOF will def have a fresh report
# define RATE 1000
jButton event4; // fake no cat TOF report
jButton event0; // fake cat TOF report
// pins for fake event trigger buttons
# define fake0 6
# define fake4 5
// ----- symbolic indices -----
enum {
HEARTBEAT, // 0
NONE_1, // 1
NONE_2, // 2
SEE0, // 3
ACK, // 4
SEE4, // 5
NONE_6, // 6
ARRIVE, // 7
LEAVE, // 8
NONE_9, // 9
HERE, // 10
GONE, // 11
LED_COUNT
};
void setup() {
Serial.begin(115200);
Serial.println("hi Mom!");
setupFlash();
event0.begin(fake0);
event4.begin(fake4);
}
// fake TOF readings
bool event;
unsigned char eventType;
// note when the cat last disappeared
bool needDepartureReport;
unsigned long lastLeft;
// and if longer than this, conclude it is gone, milliseconds
# define LONGGONE 3777
bool catPresent;
int catScore;
void loop()
{
static unsigned long lastTime;
now = millis();
// loop rate
serviceFlash();
event0.update();
event4.update();
if (event0.uolPress()) {
event = true;
eventType = 0;
flash(SEE0);
}
if (event4.uolPress()) {
event = true;
eventType = 4;
flash(SEE4);
}
if (now - lastTime < RATE) return;
lastTime = now;
// once per
flash(HEARTBEAT, 7);
# define K1 3
// we are now looking for K1 consecutive identical reports
if (event) {
event = false;
if (catPresent) {
if (eventType == 4) if (++catScore == K1) catScore = K1; // bump
if (eventType == 0) catScore = 0; // reset
}
if (!catPresent) {
if (eventType == 0) if (++catScore == K1) catScore = K1; // bump
if (eventType == 4) catScore = 0; // reset
}
}
if (catScore == K1 && !catPresent) {
catScore = 0;
catPresent = true;
needDepartureReport = true;
flash(ARRIVE);
Serial.print(now); Serial.println(" cat in sight");
latchOn(HERE);
}
if (catScore == K1 && catPresent) {
catScore = 0;
catPresent = false;
flash(LEAVE);
Serial.print(now); Serial.println(" cat no in sight");
resetLatched();
lastLeft = now;
}
// if it has been awhile with no activity, conclude the cat has left the building
if (needDepartureReport && !catPresent)
if (now - lastLeft > LONGGONE) {
needDepartureReport = false;
Serial.print(now);
Serial.print(" cat left at "); Serial.print(lastLeft);
Serial.println("");
flash(GONE, 50); // long flash when Lucy jets
}
}SEE4
HEARTBEAT
0
1
2
3
ARRIVE
LEAVE
GONE
SEE0
PRESENT
ABSENT
4
5
6
7
8
9
10
11
HERE