it can be done with a state-machine. And I expect that it will be easier to maintain than without a state-machine.
The advantage of a state-machine is that the number of required if-conditions is reduced through the switch-case statement.
If you decide to modify your code for beeing based on a statemachine I highly recommend to use self-explaining names for everything.
try to understand this code-version
#include<millisDelay.h>
int light1=22;
int exhaust =23;
int pir1 =26;
int pir2=27;
int pirState1;
int pirState2;
millisDelay exhaustDelay;
unsigned long delayTime = 1*60*1000;
int flag =1;
int flag2=1;
void washRoomLight()
{
if(pirState1==1 || pirState2==1 && flag==1)
{
digitalWrite(light1,HIGH);
flag=0;
}
if(pirState1==0 && pirState2==0 && flag==0)
{
digitalWrite(light1,LOW);
exhaustDelay.start(delayTime);
flag=1;
}
}
void exhaustFan()
{
if(pirState2==1 && flag2==1)
{
digitalWrite(exhaust,HIGH);
flag2=0;
}
if(pirState2==0 && flag2==0 && exhaustDelay.justFinished())
{
digitalWrite(exhaust,LOW);
flag2=1;
}
}
void setup() {
// put your setup code here, to run once:
pinMode(pir1,INPUT);
pinMode(pir2,INPUT);
pinMode(light1,OUTPUT);
pinMode(exhaust,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
pirState1 = digitalRead(pir1);
pirState2 = digitalRead(pir2);
washRoomLight();
exhaustFan();
}
pretty hard to almost impossible as the names are not self-explaining
Now read this code-version which has the same functionality
#include<millisDelay.h>
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 HIGH
#define ON LOW
millisDelay exhaust_PinDelay;
unsigned long delayTime = 1 * 60 * 1000;
boolean lightSwitchedOn = false;
boolean FanIsRunning = false;
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);
}
}
}
void exhaustFan() {
if (!FanIsRunning) {
if (pirToiletState == personDetected ) {
digitalWrite(exhaust_Pin, ON);
FanIsRunning = true;
}
}
if (FanIsRunning) {
if (pirToiletState == notActivated && FanIsRunning && exhaust_PinDelay.justFinished()) {
digitalWrite(exhaust_Pin, OFF);
FanIsRunning = false;
}
}
}
void setup() {
// 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() {
// put your main code here, to run repeatedly:
pirWashRoomState = digitalRead(pirWashRoom_Pin);
pirToiletState = digitalRead(pirToilet_Pin);
washRoomLight();
exhaustFan();
}
pretty easy as all names are self-descriptive
best regards Stefan