if an interrupt occurs during an if statement, after the interrupt does the rest of the if statement run or does it go back to the main code? I have a linear actuator controllong a door for my cats to go in and out. There is a switch on the door to determine if it's opened or closed. I have a touch sensor inside the house and one outside the house that the cats step on to initiate the code. I also have 2 limit switches to control the linear actuator's travel limit. I'd like the code to run as follows.
if a cat steps on the inside sensor the light will come on, the linear actuator will start moving and open the door until it hits the upper limit switch and stops. The door should stay open for 5 seconds. Then the linear actuator will start reversing to close the door until it hits the lower limit switch and stops. Light turns off.
The same code should run if a cat steps on the outside sensor , or if there is a cat standing on the inside sensor and a cat standing on the outside sensor at the same time.
const int upperLimitswitch = 2; // upper limit switch
const int lowerLimitswitch = 3; // lower limit switch
const int inSensor = 4;
const int outSensor = 5;
const int doorSensor = 6;
const int motorRelay1 = 7; // relays wired as a DPDT switch to control linear actuator
const int motorRelay2 = 8;
const int lightRelay = 9;
int inSensorVal;
int outSensorVal;
int doorSensorVal;
void setup() {
pinMode(upperLimitswitch, INPUT); // wired normally closed
pinMode(lowerLimitswitch, INPUT); // wired normally closed
pinMode(inSensor, INPUT); // wired normally open
pinMode(outSensor, INPUT); // wired normally open
pinMode(doorSensor, INPUT); // wired normally open
pinMode(motorRelay1, OUTPUT);
pinMode(motorRelay2, OUTPUT);
pinMode(lightRelay, OUTPUT);
attachInterrupt(digitalPinToInterrupt(limit1), uplimit, LOW);
attachInterrupt(digitalPinToInterrupt(limit2), lowlimit, LOW);
}
void uplimit() { //stops linear actuator
digitalWrite(motorRelay1, LOW);
digitalWrite(motorRelay2, LOW);
}
void lowlimit() { //stops linear actuator
digitalWrite(motorRelay1, LOW);
digitalWrite(motorRelay2, LOW);
}
void loop() {
inSensorVal = digitalRead(inSensor);
outSensorVal = digitalRead(outSensor);
doorSensorVal = digitalRead(doorSensor);
// Condition 1
if (doorSensorVal == HIGH && inSensorVal== HIGH && outSensorVal == LOW) {
digitalWrite(lightRelay, HIGH); //Light comes on
digitalWrite(motorRelay1, HIGH); // linear actuator moves out to open door
digitalWrite(motorRelay2, LOW); // linear actuator moves out to open door to trigger
// upper limit switch
}
Condition 2:
if (doorSensorVal== HIGH && inSensorVal == LOW && outSensorVal == HIGH) {
digitalWrite(lightRelay, HIGH); //Light comes on
digitalWrite(motorRelay1, HIGH); // linear actuator moves out to open door
digitalWrite(motorRelay2, LOW); // linear actuator moves out to open door to trigger
// upper limit switch
}
// Condition 3:
if (doorSensorVal == HIGH && inSensorVal == HIGH && outSensorVal == HIGH) {
digitalWrite(lightRelay, HIGH); //Light comes on
digitalWrite(motorRelay1, HIGH); // linear actuator moves out to open door
digitalWrite(motorRelay2, LOW); // linear actuator moves out to open door to trigger
//upper limit switch
}
}