Pages: [1] 2   Go Down
Author Topic: Stop void loop()?  (Read 1400 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
/*
  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:
Code:
/*
  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... smiley-red


Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you really want to stop it deat, just put in
Code:
for (;;);
, if you want start again from the top of "loop ()", put in
Code:
return;
Logged

Per Arduino ad Astra

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6108
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8471
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 1
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

how would i call it the 10x?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8471
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 1
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...??  smiley-red
« Last Edit: February 20, 2011, 07:43:46 pm by bob4432 » Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8471
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Code:
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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 1
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley-draw 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
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8471
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 1
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8471
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Code:
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
« Last Edit: February 20, 2011, 10:56:08 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

0
Offline Offline
Newbie
*
Karma: 1
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...... smiley-red and after they were moved it works as planned now  smiley-yell.  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 smiley
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8471
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1] 2   Go Up
Jump to: