aborting a loop

For a school project were programming an arduino board. In default the script does nothing untill one of our sensors is triggered. When a sensor is triggered it starts a loop. (multiple leds turn on with timers between them) While te loop is running the sensors are disabled.

But we want to add a button that can interupt the loop, so that the board returns to its default status. How can i make a code that interupts the loop?

This is how the code looks now:

// IN
int afstand0 = 0;    
int afstand1 = 1;    
int ingang7 = 7;     
// OUT
int uitgang8 = 8;  // LEDS
int uitgang9 = 9;  
int uitgang10 = 10; 
int uitgang11 = 11;  
int uitgang12 = 12; // PUMP
int uitgang13 = 13; // LASER 
int val0 = 0;    
int val1 = 0;    
int val2 = 0;   
int afstand = 400; 
int timer_aan1 = 1000;  
int timer_aan2 = 1000; 
int timer_aan3 = 1000; 
int timer_aan4 = 1000; 
int timer_uit1 = 10000; 
int timer_uit2= 1000; 
int timer_uit3 = 1000; 
int timer_uit4 = 1000; 

void setup() { 
  pinMode(ingang7, INPUT);  
  pinMode(uitgang8, OUTPUT);  
  pinMode(uitgang9, OUTPUT);  
  pinMode(uitgang10, OUTPUT);  
  pinMode(uitgang11, OUTPUT);   
  pinMode(uitgang12, OUTPUT);  
  pinMode(uitgang13, OUTPUT);  
void loop() { 
  val0 = analogRead(afstand0);    
  val1 = analogRead(afstand1); 
  val2 = digitalRead(ingang7);  
    if ( val0 > afstand){ 
      digitalWrite(uitgang8, HIGH);  // led on
      digitalWrite(uitgang9, HIGH);  // led on
      digitalWrite(uitgang10, HIGH);  // led on
      digitalWrite(uitgang11, HIGH);//led on
      digitalWrite(uitgang12, HIGH); // pump on
      digitalWrite(uitgang8, LOW); // led off
      digitalWrite(uitgang9, LOW);  // led off
      digitalWrite(uitgang10, LOW);  // led off
      digitalWrite(uitgang13, HIGH); // laser sends signal
      digitalWrite(uitgang11, LOW);  // led off
      digitalWrite(uitgang12, LOW);  // pump off
       digitalWrite(uitgang13, HIGH); // laser stops sending

Thanks in advance

In my opinion you must not use the delay() function for the timing of your LED animation, try using millis() to compare the time how long the leds are already on or of . Then the main loop will continue to run and you can always read the sensors and set the “val0” varible by your button to zero again.
I copied this from the tutorial section its the “Blinking an LED without using the delay() function” post.
it has to be something like this.

int ledPin = 13;                // LED connected to digital pin 13
int value = LOW;                // previous value of the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

void setup()
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output

void loop()
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, is the difference
  // between the current time and last time we blinked the LED bigger than
  // the interval at which we want to blink the LED.
  if (millis() - previousMillis > interval) {
    previousMillis = millis();   // remember the last time we blinked the LED

    // if the LED is off turn it on and vice-versa.
    if (value == LOW){
      value = HIGH;}
      value = LOW;}

    digitalWrite(ledPin, value);

in general, if you need to abort a running loop you have to write your code into an extra function, which can be called from the main loop check this: http://www.arduino.cc/en/Reference/FunctionDeclaration

hope this helps


I would follow woo_oow’s suggested approach to your issue; however, if you are ever in a situation where you need to exit a loop there is a c/c++ statement that performs this function ‘break’

Basically it will execute the innermost (in the case of nested loops) loop structure that contains the break statement. The reference page for the arduino has a good description.

thanks for the help, ill try it out on monday. ill get back at ya!

Thanks for the ideas but our group isnt really capable of figuring it out. My teacher (not an expert) said we could maybe try a interrupt that whatever loop is playing, if the STOP button is pushed it interrupts anything and return to the default status.


here's a hint :slight_smile:

define your own delay function, call it mydelay, that returns an int

break down any delay into multiple 10 msec delays using a for loop
every time you wait for 10msec you check an input, if the input has changed
you quit the for loop and return TRUE otherwise you finish off the loop and return FALSE

when you use mydelay you check the return value, if it's false you go on to the next step otherwise you break out of the animation loop
it's a bit crude but it doesnt require an interrupt and it works at a 10msec resolution

(please note that the for loop and checking the input uses a fee microseconds therefore on long delays you might need to compensate the extra time)