USA
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« on: February 03, 2013, 04:11:46 pm » |
I hope you guys can help me. I am trying to exit a for(;  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 } } }
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 697
|
 |
« Reply #1 on: February 03, 2013, 04:17:26 pm » |
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 ...
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 86
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: February 03, 2013, 04:23:42 pm » |
You can mimic a while with the for ... too (better use a while) bool keepRunning=true; for (;keepRunning;) { ... keepRunning = false; ... }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #3 on: February 03, 2013, 04:29:53 pm » |
break exits a loop. What is the problem exactly?
|
|
|
|
|
Logged
|
|
|
|
|
USA
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #4 on: February 03, 2013, 11:36:10 pm » |
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
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #5 on: February 05, 2013, 02:22:35 am » |
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. 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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #6 on: February 05, 2013, 02:31:58 am » |
Just explain why you have the line: for(;;)
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 697
|
 |
« Reply #7 on: February 05, 2013, 02:50:46 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1381
May all of your blinks be without delay
|
 |
« Reply #8 on: February 05, 2013, 02:58:23 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
USA
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #9 on: February 05, 2013, 03:21:30 am » |
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
Edison Member
Karma: 47
Posts: 1381
May all of your blinks be without delay
|
 |
« Reply #10 on: February 05, 2013, 03:22:08 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 47
Posts: 1381
May all of your blinks be without delay
|
 |
« Reply #11 on: February 05, 2013, 03:25:51 am » |
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
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 697
|
 |
« Reply #12 on: February 05, 2013, 03:33:43 am » |
Perhaps a higher level description of what you wish to do with your arduino will allow us to give you an answer.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #13 on: February 05, 2013, 10:43:25 am » |
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
Edison Member
Karma: 47
Posts: 1381
May all of your blinks be without delay
|
 |
« Reply #14 on: February 05, 2013, 12:58:39 pm » |
We are all fumbling in the dark until we have an explanation of what is required.
|
|
|
|
|
Logged
|
|
|
|
|
|