This should be a quick question.
I'm trying to figure out how I can restart void loop()
if a certain condition is met. In this instance, if (s>10), I want the loop to restart. Any help will be appreciated.
void loop() {
if (getDistance(0) == true) //if the car passed the first sensor
{
while (getDistance(0) == true && getDistance(1) == false);
unsigned long currentMillis = millis(); //time when the passed the first sensor
while (getDistance(1) == true && getDistance(0) == true); //wating car to pass the second sensor
unsigned long endMillis = millis(); //time at which the car passed the second sensor
unsigned long timeMillis = endMillis - currentMillis; //get duration in ms
float timeSeconds = timeMillis / 1000.0; //because 1000ms = 1s
float s = sensorDistance / timeSeconds; //speed = distance / time
if(s>=7 && s<=10) // for red to turn on
{
Serial.print(s); // printing the speed magnitude
Serial.print(" m/s");// printing the units of speed
Serial.println(); // new line in printing
char msg[2] = {'h','#'}; // sending some random message to indicate that speed is greater than 10
digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
vw_send((uint8_t *)msg, 2); // sending a message
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(led_pin, LOW); // again bringing the flash light to low
}
else if (s<7)
{
Serial.print(s); // printing the speed magnitude
Serial.print(" m/s");// printing the units of speed
Serial.println(); // new line in printing
char msg[4] = {'h','e','l','#'}; // sending some random message to indicate that speed is greater than 10
digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
vw_send((uint8_t *)msg, 4); // sending a message
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(led_pin, LOW); // again bringing the flash light to low
}
delay(2000);
}
}
3 Answers 3
When programming with arduino IDE, you declare 2 functions, setup() and loop().
Before compilation, arduino IDE basically expands to :
int main(void)
{
/* run the Arduino setup */
setup();
/* and the event loop */
while (1) {
loop();
}
/* never reached */
return 0;
}
So, from here you can see that setup() is called once at startup and then, loop() is called in an infinite loop. Il you return from within your loop function, it will be called again.
To expand on what jfpoilpret said setup() and loop() are called by the code you can't see in every program. setup()
is called once and loop()
is called in a while(true)
loop until the program crashes or the universe ends.
To call you code while a condition is met you could either wrap it in a loop or an if, within the loop()
function, so something like this:
void loop ()
{
if (s > 10)
{
// Your stuff....
}
}
This will only call your code IF s > 10
. Something you need to watch for is ensuring that there is some code that can cause s
to be greater than 10
.
You can also move "// Your stuff..."
into a function of its own
void MyCode ()
{
// Your stuff ...
}
and then call this function from inside the if statement. This makes you code easier to read and easier to fix.
If this answer wasn't what you meant then feel free to comment and I'll see if I can come up with a better answer.
I hate to bring this up, but I've now been programming for 58 years. One curious and often overlooked feature of the C & C++ languages is the GOTO keyword. It can be very useful. My second language was FORTRAN I. I know, now I'm relegated to the 9th circle of he|| where the miscreants are frozen into glaciers head down with their legs protruding?
-
Regarding the last bit, perhaps not at the hands of the arduino.cc people, since bothered to mention
goto
in the official documentation.timemage– timemage2021年10月06日 10:33:12 +00:00Commented Oct 6, 2021 at 10:33 -
goto
has legitimate uses, but there is no point in using it if you instead can simplyreturn
: "_ If there is no cleanup needed then just return directly_".Edgar Bonet– Edgar Bonet2021年10月06日 12:26:42 +00:00Commented Oct 6, 2021 at 12:26 -
frozen into glaciers head down
... that's one way to keep a cool headjsotola– jsotola2022年12月06日 23:27:38 +00:00Commented Dec 6, 2022 at 23:27
loop()
is just called in a... loop. Hence, if you justreturn
from it, it will be called again.return;
statement, which will cause the current function call to exit immediately and return control to the calling function. In the case of loop() this will cause it to be restarted.