How can i make a loop inside the 'void loop', for example:
I have 3 diferent cases, A Buzzer, a led and a servo motor. All controler by a ultrasom sensor. And i want choose, what i will use. But, if i a choose one of them, i will need to restart the aplicattion to choose another one...
I cant imagine a solution from this problems, cause if a use a WHILE, DO WHILE or a IF condittion, it will stop the aplicattion, and wait for a value from keyboard. How can i word whith this situation?
ramonglobinho:
I cant imagine a solution from this problems, cause if a use a WHILE, DO WHILE or a IF condittion, it will stop the aplicattion, and wait for a value from keyboard. How can i word whith this situation?
A while loop (for one example), will not "stop" the application. If you have no exit condition you check for, yes, it will continue looping, but it doesn't automatically keep loooping. Have a look at this code:
int i =5;
while (i > 0) {
Serial.println(i);
i--;
if (i == 1 then) {
Serial.println("All done!");
}
}
delay(1000);
This can be placed inside loop(), and the result will be that each time loop() cycles, the while loop will run, but it will run through only 5 times, because we have told it to execute the code inside the while loop only as long as the variable i contains a value larger than 0. Then, 1 second later, the while loop will execute again, because loop() is still running.
The if statement says "if i contains a value of 1, execute the code inside the if (between the {curly braces} )". So the ouput will look like this.
5
4
3
2
1
All done!
Have a good look at the various loops (for, while, do, etc.) and the if/else in the language reference on this site. It's also very helpful to learn by loading, compiling and running the various examples that come with the Arduino software. Don't be afraid to try changes to the examples to see what happens,
//---- LED turn ON 3 times ----
digitalWrite(13, HIGH); // LED on
delay(1000);
digitalWrite(13, LOW); // LED off
delay(1000);
digitalWrite(13, HIGH); // LED on
delay(1000);
digitalWrite(13, LOW); // LED off
delay(1000);
digitalWrite(13, HIGH); // LED on
delay(1000);
digitalWrite(13, LOW); // LED off
delay(1000);
}
}
All this ifs, run the aplicattion and stop, but my aplicattion CANT... =(. It need stay in looping, until i press the key 'z', and it cant stop to receive a answer...
ramonglobinho:
Yes, man. But on your examples, it has a stop time! And i cant have it. =(
What do you mean by "it has a stop time"?
If that code is inside loop(), it will run forever, printing the same output, over and over again to Serial.
Nothing stops. Nothing gets caught in the while loop().
All this ifs, run the aplicattion and stop, but my aplicattion CANT... smiley-cry. It need stay in looping, until i press the key 'z', and it cant stop to receive a answer...
It does not stop. it keeps right on running. If you type an "a" it prints a "b", if you type "b" it prints a "c", and if you type a "c"' it prints "abcde". It doesn't do anything when you type a "z", because you haven't told it to do anything when it receives a "z".
It does not flash the LED on pin 13 because you have not set pin 13 to be an OUTPUT.
So you have a bug in this code that prevents the LED from turning on, but the code most certainly does not "stop".
ramonglobinho:
Hello everybody,
How can i make a loop inside the 'void loop', for example:
I have 3 diferent cases, A Buzzer, a led and a servo motor. All controler by a ultrasom sensor. And i want choose, what i will use. But, if i a choose one of them, i will need to restart the aplicattion to choose another one...
I cant imagine a solution from this problems, cause if a use a WHILE, DO WHILE or a IF condittion, it will stop the aplicattion, and wait for a value from keyboard. How can i word whith this situation?
Thanks,
Using a "state machine".
See this link from Nick Gammon, a very good explanation: