Re: Assistance with millis

Hi All

I require the input of the knowledgable :slight_smile:

I have set a delay on UNO to delay a lock from opening, all good, it works, however i cannot seem to figure out how to get it back to its original state after the lock has been released.

So in this instance, UNO delays trigger for 15 min, then releases door lock after 15 min has lapsed, then has to, after 1 min, go back into locked state.

My code below..

int pbuttonPin=2;
int relayPin=3;

int val=0;
int magOff=0;
int pushed=0;

int led = 13; // Pin 13 has an LED connected on most Arduino boards.

unsigned long DELAY_TIME = 30000; // 10 sec
unsigned long delayStart = 0; // the time the delay started
bool delayRunning = false; // true if still waiting for delay to finish

void setup() {
// put your setup code here, to run once:
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);

digitalWrite(relayPin, LOW); // turn MAGLOCK OFF
// start delay
delayStart = millis();
delayRunning = true;
}

void loop() {
// put your main code here, to run repeatedly:

val = digitalRead(pbuttonPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)

} else

if (delayRunning && ((millis() - delayStart) >= DELAY_TIME)) {
delayRunning = false; // finished delay -- single shot, once only
digitalWrite(relayPin, HIGH); // turn MAGLOCK ON

}

}

I whish You had the wiring diagram attached. Using "INPUT_PULLUP" the pbutton must be connected between GND and input. That makes a pressed button show up as LOW.

If You would attache an oscilloscope connected to the input You would se a burst of switchings between HIGH and LOW. That is called "bouncing". You have no debouncing at all. Using a switching from HIGH to LOW to trigger the 15 minute event could still work.

Railroader:
I whish You had the wiring diagram attached. Using "INPUT_PULLUP" the pbutton must be connected between GND and input. That makes a pressed button show up as LOW.

That would be correct, and it works perfectly. My issue lies in the code, unable to deactivate relay after relay has activated. i seem to be missing a line of code that resets back to its normal state after the function hass been fulfilled

Did You read my next post? The one about bouncing?

Railroader:
If You would attache an oscilloscope connected to the input You would se a burst of switchings between HIGH and LOW. That is called "bouncing". You have no debouncing at all. Using a switching from HIGH to LOW to trigger the 15 minute event could still work.

How do i achieve this? I do not possess an oscilloscope

Then I tell You what an oscilloscope woudl show. When You press the button a fast burst of switchings, from HIGH to LOW to HIGH... will take place....
Your code for button being pressed is if ... == LOW

Railroader:
Then I tell You what an oscilloscope woudl show. When You press the button a fast burst of switchings, from HIGH to LOW to HIGH... will take place....
Your code for button being pressed is if ... == LOW

But what code do i use to turn the relay to off state after the door lock has been opened so that it can lock again?

Using the long delay construction You can't turn it off. The controller goes sleeping during delay.
Look for topics containing "using millis()", "doing several things at the same timeโ€ฆ".
Only a hard wire contact braker can do it. And then You can't turn it on until the delay time has expired.

@Riyaad786

Do NOT CROSS POST.
Other poste DELETED.

Please take some time to READ THIS.

Bob.