Stop void loop()?

can you stop void loop() from within it?

i am using the for or while statement from the examples here http://www.arduino.cc/en/Reference/For & http://arduino.cc/en/Reference/While and serial out shows that the variable increments. i have the variable set to 10 but once it gets to 10, it just starts over. how do i stop it or exit the loop? just for ease, i was using the blink example to have the led blink 10 times, but the statements do not stop it.

FOR:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);  
  Serial.begin(9600);  
}

void loop() {
  for (int i=0; i < 10; i++)
  {
  digitalWrite(13, HIGH);   // set the LED on
  delay(250);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(250);  // wait for a second
  Serial.println(i);
  }
}

WHILE:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);  
  Serial.begin(9600); 
}

void loop() {
  int i = 0;
  while(i < 10)
  {
  digitalWrite(13, HIGH);   // set the LED on
  delay(250);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(250);  // wait for a second
  i++;
  Serial.println(i);
  }
}

please assist... :blush:

If you really want to stop it deat, just put infor (;;);, if you want start again from the top of "loop ()", put inreturn;

The idea of a microcontroller is that it is always running something. It doesn't "start and stop" code / programs like you would with applications on a PC. So generally, you don't just have it do one task and stop.

However, Groove's method works for stopping your code from running. As does adding a "while(1);" statement wherever you want your code to "stop."

These two ways just put the microcontroller into an infinite loop so it is still running code, just nothing useful.

Another way is to just put the loop code into setup.


Rob

how would i call it the 10x?

how would i call it the 10x?

That is such a basic question, I can't believe you need to ask it.
http://arduino.cc/en/Reference/For

Exectly the same way you are now, just move that code into setup.


Rob

take the blink example - the code is in the main loop, maybe i am thinking of something else, but if i moved the blink code into the setup area, how would you call it from the loop? i have tried many things and get error about scope of variables even if they are declared in the setup area which i thought made them global, but that is not the case

also, the blink code wouldn't stop w/ an if or while statement, at least the way i have posted above.

sometimes i get hyperfocused on 1 thing, so maybe that is the issue...?? :blush:

but if i moved the blink code into the setup area, how would you call it from the loop?

You don't, setup() is run automatically, it flashes the LED 10 time then returns. loop() is then called and does nothing.

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);  
  Serial.begin(9600); 
  
   int i = 0;
  while(i < 10)
  {
  digitalWrite(13, HIGH);   // set the LED on
  delay(250);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(250);  // wait for a second
  i++;
  Serial.println(i);
  }
}

void loop() {

}

Rob

:% i see...move everything into the setup only. it is ok if loop has nothing in it? wow, this is probably what i need to do, thanks so much

Most programs don't just flash a LED then stop, they continue to monitor and control things forever. So when you write something more useful I'd move the code back into loop() and use one of the techniques mentioned above.

Having said that this loop/setup arrangement is a quirk of Arduino to make is easier (or at least clearer) for new programmers, but there's no reason a large program can't sit entirely in setup();


Rob

it is more a proof of concept and blink is small and easy to work w/ :slight_smile:

is there any explanation why the while and if statements don't 'stop' the code in their { } like arduino explains they should?

while() and for() execute until a contidion is true, however you have them inside loop() which is itself just another function. If you look in the main core file you will see this

int main(void)
{
	init();

	setup();
    
	for (;;)
		loop();
        
	return 0;
}

This is the program you are actually running (it's just hidden in the Arduino environment). Note that loop() is itself inside a never-ending for() loop, that's why your code runs over and over.


Rob

after banging my head for some time,i thought why would arduino claim the if setup would work the way they claimed if it didn't, so i looked more though the code and found i had some brackets in the wrong spots...... :blush: and after they were moved it works as planned now :stuck_out_tongue_closed_eyes:. i will leave it on overnight to verify it does what i want it to though.

just out of curiosity, if it works as planned now, do these ever get messed up if they are on for some time? is it kind of it works or not? w/ proper use of correct data types of course.

thanks for the assistance :slight_smile:

do these ever get messed up if they are on for some time?

By "these" I assume you're referring to a program running on a microprocessor.

Normally you would expect that if it runs now it will run in 10 years time. However there are things that can take hours, days or even years to manifest and the more complex the program the more likely this is.

The most classic example is the Y2K "bug", I wrote a program with 2 year digits in the early 80s not even thinking about the 99/00 roll over. Bugger me if they weren't still using the program 20 years later. There was a bug that took nearly 20 years to pop up.

So it's quite possible for a condition to occur that you didn't think of and that didn't occurs during the 2 hours you tested.

One of the common problems we see here is the use of a lot of constant strings for error or other messages etc. These take up RAM and it's possible that the stack will encroach into the string area. As long as the error message is never printed it probably doesn't matter, but 1 year later a user enters the wrong number and you print "ERROR- dickhead that was the wrong number" only the string has been clobbered by the stack and the program goes AWOL.


Rob

thanks for the info. fwiw, i do plan to test for a couple weeks w/ varying amounts of time between blinking the led for varying amounts of time along w/ keeping it on for different lengths of time. guess they wouldn't be so popular if the hardware was not very reliable though. will clean up my code too.

thanks again for all the assistance,

help me, how does the mass stop the loop, I've tried it but it always fails.

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

const int buttonOne = 2; // assign button pin to variable

void setup()
{
pinMode(buttonOne,INPUT_PULLUP); // setup button as input
Serial.begin(31250); // setup MIDI output
}

void loop()
{

if(digitalRead(buttonOne) == LOW) { // check button state again
MIDI.sendNoteOn(36, 127, 10); // Send a Note (pitch 42, velo 127 on channel 1)
MIDI.sendNoteOn(57, 127, 10); // Send a Note (pitch 42, velo 127 on channel 1)
MIDI.sendNoteOff(36, 0, 10); // Stop the note
delay(100);

}
}

midi_switch.ino (588 Bytes)

midi_switch.ino (588 Bytes)

help me, how does the mass stop the loop, I've tried it but it always fails.

The mass of what? Jupiter?

The code does NOT fail. You failed to describe what it actually does. You failed to describe what you expect it to do.

how does the mass stop the loop,

Sorry, but I don't understand the question.

What is "the mass" ?

I want, when the button is pressed, switch on once and does not repeat. repeat if pressed again