0

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);
 }
}
asked Nov 27, 2016 at 22:11
2
  • 7
    loop() is just called in a... loop. Hence, if you just return from it, it will be called again. Commented Nov 27, 2016 at 22:23
  • 2
    Of course you could mean you want to restart loop() halfway through the code in loop, in which case you should use the 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. Commented Nov 28, 2016 at 8:10

3 Answers 3

4

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.

answered Nov 28, 2016 at 14:19
1

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.

answered Nov 28, 2016 at 8:08
0

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?

answered Oct 6, 2021 at 10:28
3
  • Regarding the last bit, perhaps not at the hands of the arduino.cc people, since bothered to mention goto in the official documentation. Commented Oct 6, 2021 at 10:33
  • goto has legitimate uses, but there is no point in using it if you instead can simply return: "_ If there is no cleanup needed then just return directly_". Commented Oct 6, 2021 at 12:26
  • frozen into glaciers head down ... that's one way to keep a cool head Commented Dec 6, 2022 at 23:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.