Pausing code until input goes LOW

Hello all,

Simple question today. How do I pause my loop until an input goes from high to low? I have an input that is programmed to ring a bell and turn on lights and shut them all off shortly after each other. My input has to be manually reset by the user after the trip, so I want it to shut everything off and then wait until the input is reset to low before going back into it's ready state waiting for the input to go high again before it trips the outputs. See the below code. Is this something while could take care of?

// definitions and setup:

// Pin Definitions
const int audiblePin = 7; // bell circuit
const int visualPin = 6; // red/blue LED circuit
const int bunklightPin = 5; // bunk room light activation
const int pagerPin = 3; // minitor pager activation

// Variables
int pagerState = 0;

void setup() {

pinMode(audiblePin, OUTPUT);
pinMode(visualPin, OUTPUT);
pinMode(bunklightPin, OUTPUT);
pinMode(pagerPin, INPUT);
}

void loop() {

pagerState = digitalRead(pagerPin);

if (pagerState == HIGH) {
digitalWrite(audiblePin, HIGH);
digitalWrite(visualPin, HIGH);
digitalWrite(bunklightPin, HIGH);

delay(4000); // wait 4000 milliseconds (4 seconds)
digitalWrite(audiblePin, LOW);
delay(10000); // wait 10000 milliseconds (10 seconds)
digitalWrite(visualPin, LOW);
delay(10000); // wait 10000 milliseconds (10 seconds)
digitalWrite(bunklightPin, LOW);

}
else {
digitalWrite(audiblePin, LOW);
digitalWrite(visualPin, LOW);
digitalWrite(bunklightPin, LOW);
}

}

Don't even think about doing this!. Take a look at this thread Demonstration code for several things at the same time - Project Guidance - Arduino Forum and then adept the concept.

Oh, and get rid of those delay()'s

Mark

Specification How do I pause my loop until an input goes from high to low?

pseudo code

while( read input ) {timeout } // pause when input high with optional timeout
....process after input goes low
debounce optional

That's more what I'm looking for - just something to stop until a High input is sensed again and then restart the loop

Yes, but then you will want to extend things and wont be able to without a total rewrite. Vacla's idea is short sited! Learn to thing the right way from the start.

loop(){
  if (digitalRead(pin)==LOW){
     dostuff for pin low

  }
  //other code to do stuff with other things

}

You must think ahead!

Mark

mpaine:
That's more what I'm looking for - just something to stop until a High input is sensed again and then restart the loop

I don't think this is same as the original "spec". That is why I posted the specification.
So can you put in English how you want it to work

This is the way I interpret your requirement so far
loop start
input detected - for example Button = HIGH
processing....

wait until button goes LOW

wait until button goes HIGH again

loop end

Keep in mind the process runs inside the loop so there is no need to "restart" the loop.
And as long as your processing can stop , as you indicated, there is no need to confuse the issue with "future requirements", code for what you need now first.

Vaclav:
I don't think this is same as the original "spec". That is why I posted the specification.
So can you put in English how you want it to work

This is the way I interpret your requirement so far
loop start
input detected - for example Button = HIGH
processing....

wait until button goes LOW

wait until button goes HIGH again

loop end

Keep in mind the process runs inside the loop so there is no need to "restart" the loop.
And as long as your processing can stop , as you indicated, there is no need to confuse the issue with "future requirements", code for what you need now first.

My requirements are as follows:

(I am using a relay shield by the way)

Input detected on Pin 3 (pagerPin)

Activate the following: Relay 1, Relay 2, Relay 3.
-- Relay 1 should operate for a total of 4 seconds
-- Relay 2 should operate for a total of 7 minutes
-- Relay 3 should operate for a total of 5 minutes

Once all relays have shut off, the input on Pin 3 will still be HIGH until the external input relay is reset by the user. So, the arduino should "stand by" until it senses that the input on Pin 3 has gone LOW again and the system, upon detecting the LOW, should then go back to monitoring the input on Pin 3 for a HIGH to repeat the process.

You could use a boolean flag 'System_Ready' initiated to TRUE. Now if the the system is ready (System_Ready == TRUE) AND input is detected then do the relay stuff and set the System_Ready flag to FALSE. Now the relay stuff won't happen again until the System_Ready flag is set to TRUE which can be done when the external input relay is reset by the user.

mpaine:
Once all relays have shut off, the input on Pin 3 will still be HIGH until the external input relay is reset by the user. So, the arduino should "stand by" until it senses that the input on Pin 3 has gone LOW again and the system, upon detecting the LOW, should then go back to monitoring the input on Pin 3 for a HIGH to repeat the process.

A "busy waiting loop" that blocks all program execution until the pin is LOW looks like:

  while(digitalRead(3)==HIGH) ;  // which means: while pin-3 is HIGH ==> do nothing (empty statement)

mpaine:
My requirements are as follows:

(I am using a relay shield by the way)

Input detected on Pin 3 (pagerPin)

Activate the following: Relay 1, Relay 2, Relay 3.
-- Relay 1 should operate for a total of 4 seconds
-- Relay 2 should operate for a total of 7 minutes
-- Relay 3 should operate for a total of 5 minutes

Once all relays have shut off, the input on Pin 3 will still be HIGH until the external input relay is reset by the user. So, the arduino should "stand by" until it senses that the input on Pin 3 has gone LOW again and the system, upon detecting the LOW, should then go back to monitoring the input on Pin 3 for a HIGH to repeat the process.

Ok , here is a skeleton of the code so you get the idea
IT IS NOT CUT AND PASTE, I hope you can edit it yourself.

The code is just demo of logic flow.

You need to decide what ACTIVATES the pagerPin , if you use pull-up ( HIGHLY recommended) it would be active LOW.

Before someone gets after me - this is PSEUDO code, blocking - waiting for responses and millis() is a not real time , just interval timer since the program started execution and will roll over in x milliseconds.

Good luck.

void setup() {
  // put your setup code here, to run once:

}

void loop() {

  Input detected on Pin 3 (pagerPin)
  while (pagerPin) {}         // while pagerPin is HIGH - wait for pagerPin to go active LOW 

  //pager pin active - LOW
 
  startTime = millis();     //start timer
  digitalWrite(Relay1, LOW ); // set relay
  digitalWrite(Relay2, LOW ); // set relay
  digitalWrite(Relay3, LOW ); // set relay

  while ( millis() - startTime < Relay1 time timeout 4 sec  ) {} // wait for relay 1 timeout
  digitalWrite(Relay1, HIGH ); // reset  relay

  while ( millis() - startTime < Relay3 time  timeout 5 minutes ) {} // wait for relay 3 timeout
  digitalWrite(Relay3, HIGH ); // reset  relay
  
  while ( millis() - startTime < Relay2 time timeout 7 minutes ) {} // wait for relay 2 timeout
  digitalWrite(Relay2, HIGH ); // reset  relay


  while(!pagerPin){} // wait for pagerPin release - idle HIGH ( pull-up) 



Activate the following: Relay 1, Relay 2, Relay 3.
  -- Relay 1 should operate for a total of 4 seconds
  -- Relay 2 should operate for a total of 7 minutes
    -- Relay 3 should operate for a total of 5 minutes


    }

Pausing code until input goes LOW

I use the below in some servo code.

while (digitalRead(7)) {} //delay loop until pin 7 is low

zoomkat:
I use the below in some servo code.

while (digitalRead(7)) {} //delay loop until pin 7 is low

Good show.

I am glad someone else indirectly confirms my suggestion.
Not to be picky - a snippet of code should be generic - reference to "delay loop" could be just "wait until...".
Even when it is apparent from context, as in this case.
It has been noted on this forum that posts get misinterpreted for minute discrepancy.

"Once all relays have shut off, the input on Pin 3 will still be HIGH until the external input relay is reset by the use"

I am still little puzzled by this requirement - keeping the pin 3 active.

The process starts by activated pin 3 , relays are operated and released after their timeout expires.

The process is blocking and must finish by releasing the last relay.

So why keeping the pin / input active during the process?

Is there any other requirement I am missing?

Vaclav:
It has been noted on this forum that posts get misinterpreted for minute discrepancy.

If the forum doesn't spot minor discrepancies, the compiler certainly will.

It's a little difficult to know where to start, but Vaclav is right that you shouldn't copy and paste his code.

const int pagerPin = 3; // minitor pager activation
...
  while(!pagerPin){} // wait for pagerPin release - idle HIGH ( pull-up)

I'm not sure when the constant 3 will ever change, but maybe Vaclav can explain that.