Need help exit for(;;)

I hope you guys can help me. I am trying to exit a for(;: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 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 
    }
  }
}

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

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

You can mimic a while with the for ... too (better use a while)

bool keepRunning=true;
for (;keepRunning;)
{
  ...
  keepRunning = false;
  ...
}

break exits a loop. What is the problem exactly?

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.

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.

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);
  
  }
  
}

Just explain why you have the line:

  for(;;)

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

Meanwhile ...

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?

Try this and you will see what is going on

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.

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.

As Nick has said above, break; will exit a for loop as in this example

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.

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 ?

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

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.

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

TexasStingray:
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.

Pretty much what he said. If the value changes, I want the loop to reflect that change.

How are you expecting the value to change?

You're really not doing much to help yourself are you.

Without referencing any of the responses thus far in this thread, and in plain english (although this may be the problem), describe to us what you wish to do as EXACTLY AS YOU CAN without being the slightest bit vague.

In other words what's in your head is not known to us and thus you have to let us know what you want or you may as well stop posting.

TexasStingray - Not sure how I am going get the value to change as of right now, I will play around with what I have and see if I can figure out something. As per lloyddean request, I am going to wait to make any more post until I can "clarify" on what I am trying to accomplish.

Simple serial test code that continously loops awaiting the appropriate data to turn the arduino board LED on/off.

//zoomkat 3-5-12 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //if (readString.length() >0) {
        Serial.println(readString); //prints string to serial port out
        //do stuff with the captured readString
        if(readString.indexOf("on") >=0)
        {
          digitalWrite(ledPin, HIGH);
          Serial.println("LED ON");
        }
        if(readString.indexOf("off") >=0)
        {
          digitalWrite(ledPin, LOW);
          Serial.println("LED OFF");
        }       
        readString=""; //clears variable for new input
      //}
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}