# define ON HIGH
# define OFF LOW
# define SENSE LOW
const unsigned long interval = 1777; // life too short
const unsigned long debounceTime = 20; //... 5000: annoying and absurd - sensor issue?
unsigned long now;
const byte vmot = 8;
const byte atmo = 9;
const byte gran = 10;
const byte sens = A4;
void setup() {
pinMode(vmot, OUTPUT);
pinMode(atmo, OUTPUT);
pinMode(gran, OUTPUT);
pinMode(sens, INPUT_PULLUP);
{
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
pinMode(7, INPUT_PULLUP);
delay(1200);
}
}
void loop() {
now = millis();
int sensorActivity = sensor();
blower(sensorActivity);
{
digitalWrite(6, digitalRead(7));
}
}
/*
sensor activity
1 leading edge
0 no ativity
-1 trailing edge
*/
int sensor() {
static bool stableState;
static unsigned long lastTime;
bool edge = false;
if (now - lastTime < debounceTime)
return 0;
// !
// if (buttonState == HIGH) sensState = LOW; // false
// if (buttonState == LOW) sensState = HIGH; // true
// *reading* is true to sensState level.
// senesState LOW is 5 (gran + vmot)
// sensState HIGH is off.
bool reading = digitalRead(sens) == SENSE;
if (reading != stableState) {
stableState = reading;
edge = true;
}
if (!edge) return 0;
return stableState ? -1 : 1;
}
void blower(int sensorActivity) {
static bool sensor = false;
static unsigned long timer;
static bool atmoState;
if (sensorActivity) {
timer = now;
atmoState = true;
}
if (atmoState && now - timer > interval)
atmoState = false;
if (sensorActivity > 0) sensor = false;
if (sensorActivity < 0) sensor = true;
bool granState = !sensor && !atmoState;
bool vmotState = !sensor || atmoState;
digitalWrite(atmo, atmoState ? ON : OFF);
digitalWrite(gran, granState ? ON : OFF);
digitalWrite(vmot, vmotState ? ON : OFF);
}
/* redo for non-blocking timing
// use this in sensor() to throttle its loop rate
if (now - lastTime < debounceTime) {
*state = !buttonState;
return false;
}
lastTime = now;
*/
gran .... atmo .... vmot
0 ... 1
NC-COM-NO