Continue an IF statement after input is stopped

Car builder here trying to get a flush mount door handle to open slightly to allow for the door to be opened and then return to the flush position. My issue is I need to take the “unlock signal” and run the if statements, the issue however is after that signal is removed my if statement quits and does not perform the entire program. I’m sure I’m missing something rather simple, but the wife is getting annoyed at how much time I have spent in the garage trying to find the answer on the interwebs. Thanks and my current code is below.

Using a ArduinoUno R3 powered by a 12v to 5v converter, the input is powered by two relays running off the lock module ground signal providing 5v to the analog inputs, and the arduino outputs to a L298N motor driver running the door actuators.

int OpenIn = A0; // Door Open Input Signal
int LockIn = A3; // Door Lock Input Signal
int RelayOne = 4;  // Relay 1 Door Open Output Signal
int RelayTwo = 7;  // Relay 2 Door Close Output Signal
int RelayThree = 8;  // Relay 3 Lock Open Output Signal
int RelayFour = 12;  // Relay 4 Lock Close Output Signal
void setup() 
{pinMode(OpenIn, INPUT);
pinMode(LockIn, INPUT);
pinMode(RelayOne, OUTPUT);
pinMode(RelayTwo, OUTPUT);
pinMode(RelayThree, OUTPUT);
pinMode(RelayFour, OUTPUT);}
void loop() {
if (analogRead(OpenIn) > 1000) { // Door Handle Unlock, Open, Close Sequence
// Unlock Handle
digitalWrite(RelayThree, HIGH);
digitalWrite(RelayFour, LOW);
delay(1000);
// Handle Open
digitalWrite(RelayThree, LOW);
digitalWrite(RelayOne, HIGH);delay(1000);
// Pause Handle Open
digitalWrite(RelayOne, LOW);delay(20000);
// Handle Close
digitalWrite(RelayOne, LOW);
digitalWrite(RelayTwo, HIGH);
// Return Relays To Normally Closed
delay(1000);
digitalWrite(RelayTwo, LOW);
}
if (analogRead(LockIn) > 1000) {  // Door Lock Sequence
// Lock Close
digitalWrite(RelayFour, HIGH);
delay(1000);
// Return Relays To Normally Closed
digitalWrite(RelayFour, LOW);
}
}
//}

Just make a Boolean to remember that there was an unlock signal. Reset it to false when the logic is all done.

Hey Paul,

Thanks for the reply, would you mind explaining that to me like I’m an idiot? Because in regards to programming I am. How would that look in the program? Thanks again!

I assume you know how to set up a global Boolean with some name. Like “unlocksignal”. When you discover the unlock signal, set the Boolean to true. From then on in your code always refer to “unlocksignal” instead of what you are using now. When you are sure you are all through with what you need to do, set unlocksignal to false.

If you are sending 5 volts through the relays to the arduino inputs, out of curiosity, why are you analog reading them as >1000? A relay will either send the 5 volts or it won’t. Why not digitalRead them as HIGH or LOW?

I am uncertain by your description at which point it stops working.

It looks like you need to learn to use millis() based timing. You have 23 seconds of delay in the first if statement. Every one of those delays will stop the code from executing (checking) while it is delayed. It could be that by the time a delay is over and it starts checking code again to read the inputs, the inputs have been turned off and so it assumes that it does not need to do anything. The if statement is NOW not true so it jumps out of the if statement, at which point there is nothing for it to do until openIn or lockIn is > 1000 again.

Any time I use delays and have trouble with what should be a simple task, the culprit is almost always the delays.

Alright that sounds easy enough, I bullheaded my way this far into it, sure I can figure it out with a couple of goes. Thank you again!

Well that’s what the first thing I came across said to do and it worked lol.

The program stops working the second I pull the 5V from the input. The door lock module (Digital GaurdDawg) only sends a quick ground signal out, which triggers my relays. This provids the arduino with its inputs but only for a moment.

If I hold a constant 5V to the inputs the program runs without issue. Thanks for your help!

Your welcome. There are quite a few millis() timing tutorials. It is pretty simple once you get the hang of it.

Can you post your new code if it works as intended?

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction