how to stop a loop????

my overall goal is that when my "triggerpin" is high it will run a the blinking and when "triggerpin" is low it will stop.

The issue i have is that when the "triggerPin" goes low it will still run the blinking until it finishes.

I need it so that when "triggerpin" goes low it will stop blinking and reset.

Any suggestions will help

// These variables will change: int TriggerPin = 2; // the number of the pushbutton pin int RelayPin = 13; // the number of the Relay pin int TriggerState = 0; // variable for reading the Trigger status

void setup() { Serial.begin(9600); pinMode(RelayPin, OUTPUT); // initialize the Relay pin as an output: pinMode(TriggerPin, INPUT); // initialize the Trigger pin as an input: Serial.println("No Emergency"); }

void loop() { while (digitalRead(TriggerPin) == HIGH) { calibrate(); }

}

void calibrate() { Serial.println("EMER IN PROGRESS");

digitalWrite(13, HIGH); // turn the Relay on (HIGH is the voltage level) delay(10000); // wait for 10 sec digitalWrite(13, LOW); // turn the Relay off by making the voltage LOW delay(10000); // wait for 10 sec Serial.println("No Emergency"); }

If you want to reset the program, you might be able to call setup(), although that won't flush the variables.

I think that is not a good idea "when "triggerpin" goes low it will stop blinking and reset" Why you want to reset? If your real problem is as you say: "it will still run the blinking until it finishes", then you must something lie the example BlinkWithoutDelay, because your problem are the delays.

when the trigger pin is high i want the led to turn on for 10 sec and off for 10 seconds. i will adjust the times later for longer duration. once the “triggerpin” goes low i want to stop everything.
it is an emergency indicator. once the emergency is done i would like the indicator to stop the blinking.

Well you are calling delay with an argument of 10 seconds. That means you can’t do anything during that 10 seconds. That’s what delay does. You can’t react to a pin change, you can’t change the state of an LED, you can’t do anything during the delay.

You should study the “Blink Without Delay” example. It will show you how to blink your LED for whatever duration you’d like while still allowing the program to keep running through the loop() function and responding to whatever other things you might have going on.

If you want to be able to stop (or change) things in the middle you cannot use delay(). The demo several things at a time illustrates how to use millis() for non-blocking time control.

...R

Thanks, That helped, it has a delay when i pull to ground. getting closer!!

I think from your statement you want the LED to stop blinking, and not to stop a loop

If so, this code should get you started. It may not be quite correct, but you should get the idea.

void calibrate(EmergencyInProgress) {
  int blinkRate = 10000;

  if (EmergencyInProgress)  {
    if (counterSet == 0) {
      counterSet = 1;
      initCounterSet = millis();
      lastCounterSet = initCounterSet; // Not needed, except for personal sanity
    } else {
      lastCounterSet = millis();
    }

    if ((lastCounterSet - initCounterSet) < blinkRate) {
      setPinHigh = 1;
    } else {
      setPinHigh = 0;
      initCounterSet = millis();
      lastCounterSet = initCounterSet;
    }

    if (setPinHigh) {
      digitalWrite(13, HIGH);   // turn the Relay on (HIGH is the voltage level)
    } else {
      digitalWrite(13, LOW);    // turn the Relay off by making the voltage LOW
    }

  } else {
    digitalWrite(13, LOW);
    counterSet = 0;
    initCounterSet = 0;
    lastCounterSet = 0;
    setPinHigh = 0;
  }
}

These lines:

     initCounterSet = millis();
      lastCounterSet = initCounterSet; // Not needed, except for personal sanity

don’t should be:

     lastCounterSet = initCounterSet; // Not needed, except for personal sanity
      initCounterSet = millis();

?

Also check this might help you control the loop.

Switch

case 0: your code here break;

luisilva: These lines:

     initCounterSet = millis();
      lastCounterSet = initCounterSet; // Not needed, except for personal sanity

don't should be:

     lastCounterSet = initCounterSet; // Not needed, except for personal sanity
      initCounterSet = millis();

?

It depends what you are trying to do. You could have it either way, but the logic must follow appropriately. I don't know the final product or goal for the persons with the question. I imagine that person will fix the code to their needs once they try to make it work.

Jesse