// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// Take it for granted at the moment scroll down to void setup
// start of macros dbg and dbgi
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *
const byte lightWashRoom_Pin = 22;
const byte exhaust_Pin = 23;
const byte pirWashRoom_Pin = 26;
const byte pirToilet_Pin = 27;
const byte personDetected = 1;
const byte notActivated = 0;
byte pirWashRoomState;
byte pirToiletState;
#define OFF LOW //HIGH
#define ON HIGH //LOW
unsigned long myWaitTimer;
const unsigned long delayTime = 3 * 1000; // for testing delay just 3 seconds
boolean lightSwitchedOn = false;
boolean FanIsRunning = false;
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
void washRoomLight() {
if (!lightSwitchedOn) {
if (pirWashRoomState == personDetected || pirToiletState == personDetected ) {
digitalWrite(lightWashRoom_Pin, ON);
lightSwitchedOn = true;
}
}
if (lightSwitchedOn) {
if (pirWashRoomState == notActivated && pirToiletState == notActivated ) {
digitalWrite(lightWashRoom_Pin, OFF);
lightSwitchedOn = false;
//exhaust_PinDelay.start(delayTime);
myWaitTimer = millis(); // store snapshot of time when switching light OFF
}
}
}
void exhaustFan() {
if (!FanIsRunning) {
if (pirToiletState == personDetected ) {
digitalWrite(exhaust_Pin, ON);
FanIsRunning = true;
}
}
if (FanIsRunning) {
if (pirToiletState == notActivated && FanIsRunning && TimePeriodIsOver (myWaitTimer,delayTime) ) {
digitalWrite(exhaust_Pin, OFF);
FanIsRunning = false;
}
}
}
void setup() {
Serial.begin(115200);
Serial.println( F("Setup-Start"));
// put your setup code here, to run once:
pinMode(pirWashRoom_Pin, INPUT);
pinMode(pirToilet_Pin, INPUT);
pinMode(lightWashRoom_Pin, OUTPUT);
pinMode(exhaust_Pin, OUTPUT);
}
void loop() {
pirWashRoomState = digitalRead(pirWashRoom_Pin);
pirToiletState = digitalRead(pirToilet_Pin);
dbgi("1:",pirWashRoomState,1000);
dbgi("2:",pirToiletState,1000);
washRoomLight();
exhaustFan();
}