Pages: [1] 2   Go Down
Author Topic: Need help exit for(;;)  (Read 892 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I hope you guys can help me. I am trying to exit a for(;smiley-wink loop or would like to know of another alternative if it is impossible to exit this kind of loop.

It would be nice to run an infinite loop until some input will stop it.

Code:

//Code is more of example...
void setup()
{
}
void loop()
{
  process(12,300);//random values just as example
  delay(5000);//delay 5 seconds
  process(12,500);
  delay(5000);
}
void process(int x, int y)
{
  for(;;)//Create infinite loop
  {

      // Loop until command to stop
    if(!condition)//Need help with this
    {
      break; // Stop loop
    }
  }
}
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Provided by the programming environment and surrounding all your code is -

Code:
int main()
{
    init();
    setup();
    for (;;)
    {
        loop();
    }
}

so  moving your code to within 'setup' may be what you want but either way 'loop' will be called ...
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12425
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can mimic a while with the for ... too (better use a while)
Code:
bool keepRunning=true;
for (;keepRunning;)
{
  ...
  keepRunning = false;
  ...
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

break exits a loop. What is the problem exactly?
Logged

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks everyone for the reply
I will have to try to clean up my code. For some reason I am having issues with my if statement and causing the break.
Logged

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

break exits a loop. What is the problem exactly?

I thought about this... and this is what I am trying to do, in a little more detail. I have a loop that is in function more. I want the serial output that is inside of the loop to change(if that makes any sense). So if you look of void loop() you will see more is called twice with different values. If you got to the serial monitor, it just shows the first value.

Code:
void setup()
{Serial.begin(9600);
Serial.println("Starting...");

}
void loop()
{
more(1,3);
delay(3000);
more(3,5);
delay(3000);
}

void more(int x,int y)
{
  int z = x + y;
  for(;;)
  {
  Serial.println(z);
  delay(250);
 
  }
 
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just explain why you have the line:

Code:
  for(;;)
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're going to have to do a better job of communicating both what you are trying accomplish and how it's failing.

Meanwhile ...

Code:
void more(int x, int y)
{
    int z = x + y;

    for ( ; ; )
    {
        Serial.println(z);
        delay(250);

        if ( true )
        {
            break;
        }
    }
}


Is this returning as you expect?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3453
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this and you will see what is going on
Code:
void more(int x,int y)
{
  int z = x + y;
  Serial.println("before loop");
  for(;;)
  {
    Serial.println(z);
    delay(250);
  }
  Serial.println("after loop");
}
The (what) for loop is causing the problem because it never finishes so prints the same value over and over again

Why do you think you need to use such a for loop ?  If you want something to happen for ever there are better ways such as the loop() function itself or if you want to keep doing something until a condition is true, such as pressing a button, then a 'while' would be more appropriate.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am sorry for not being so clear. I am still new to arduino and embed programming.
It doesn't have to be a for( ; ; ) loop. I just need a loop that will continue... if I pass new information to that loop it will continue with the new information.

I know that the loop() will continue but it continues with one function at a time...At least that's how it seems with some other code I have used.

I really do appreciate all the feedback and help I am getting and hopefully this will help me with future post.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3453
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As Nick has said above, break; will exit a for loop as in this example
Code:
void setup()
{
  Serial.begin(9600);
  Serial.println("Starting...");

}
void loop()
{
  more(1,3);
  delay(3000);
  more(3,5);
  delay(3000);
}

void more(int x,int y)
{
  int z = x + y;
  long startTime=millis();
  Serial.println("before loop");
  for(;;)
  {
    Serial.println(z);
    delay(250);
    if (millis() - startTime > 5000) break;
  }
  Serial.println("after loop");
}
To me the infinite for loop seems clumsy.  It is more difficult to see what condition would cause it to terminate than using a while loop.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3453
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is true that the Arduino can only do one thing at a time but in most cases, such as reading sensors or turning inputs on/off repeatedly (think flashing LEDs), taking those actions does not need to block other actions.  The most infamous blocking function is delay().  Has that caused you problems in the past ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps a higher level description of what you wish to do with your arduino will allow us to give you an answer.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think what you are trying to do would require an interrupt. you appear to want to run a loop and display a value and then if that value changes it would be displaying the new value. I could be wrong but I too am still learning the Arduino. you will have to have the variable you want to display define as a global as volitile so that the interrup function could change the value. My understanding is that when am interrupt is called all control does to the interrupt function and all current processing stops until the interrupt function is completed, at which time the process continues. Hopes this helps.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3453
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We are all fumbling in the dark until we have an explanation of what is required.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1] 2   Go Up
Jump to: