# include "fcs.h"
# include "jbutton.h"
unsigned long now;
unsigned int counter;
# define button 2
# define LED 3
# define PRESSED LOW
// polling period. TOF will def have a fresh report
# define RATE 1333
jButton event4; // fake no cat
jButton event0; // fake cat
// 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!");
pinMode(button, INPUT_PULLUP);
pinMode(LED, OUTPUT);
setupFlash();
event0.begin(fake0);
event4.begin(fake4);
}
// fake TOF readings
bool event;
unsigned char eventType;
// note when the cat last disappeared
bool catHasBeen;
unsigned long lastLeft;
// and if longer than this, conclude it is gone, milliseconds
# define LONGGONE 3777
bool catPresent;
int catScore;
bool catInSight; // active signal - true between arrival and departure, many transitions per real visit
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);
// we are now adding N consecutive identical reports
# define K1 3
if (event) {
if (eventType == 0)
if (catScore < K1) catScore++;
if (eventType == 4)
if (catScore > 0) catScore--;
event = false;
}
if (catScore == K1 && !catPresent) {
catPresent = true;
catHasBeen = true;
flash(ARRIVE);
Serial.print(now); Serial.println(" cat in sight");
latchOn(HERE);
}
if (catScore == 0 && catPresent) {
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 (catHasBeen && !catPresent)
if (now - lastLeft > LONGGONE) {
catHasBeen = false;
Serial.print(now);
Serial.print(" cat left at "); Serial.print(lastLeft);
Serial.println("");
flash(GONE, 50); // long flash when Lucy jets
}
}
/*
void loop() {
static unsigned long lastTime;
now = millis();
// loop rate
serviceFlash();
static bool flag;
// this could be a debounced button and just do the thing asynchronously
if (digitalRead(button) == PRESSED) {
flag = true;
flash(ACK);
}
// once per
if (now - lastTime < RATE) return;
lastTime = now;
Serial.println(counter); counter++;
if (flag) {
flag = false;
digitalWrite(LED, digitalRead(LED) == LOW ? HIGH : LOW);
flash(ACTION);
if (digitalRead(LED) == HIGH) flash(STOP);
else flash(START);
}
}
*/SEE4
HEARTBEAT
0
1
2
3
ARRIVE
LEAVE
GONE
SEE0
PRESENT
ABSENT
4
5
6
7
8
9
10
11
HERE