exit(0); restart loop

Hi,

I put the order exit(0); at the end of my loop to stop Motor DCC when my vehicle arrive at destination. And I would like to know if it's possible to restart the loop with (in my example) a microphone who recognizes the sound defined before.

P.S: my project is a chair storage .

Don't exit loop(). There's no way for any of your code to run after that point, so you couldn't test a microphone or any other input.

After the motor is finished, don't digitalWrite() to it any more. But keep loop() looping so it can respond to new inputs.

Yeah but my programm is : when the distance measure is under 15cm the motor turn off but when this distance passes over 15 cm the motor turn on, and I want my motor turn on only when the microphone hears the sound.

Post your code so we can help but Morgan is right. If you exit the loop you are done.

int DistanceA() {
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
float Adistance = pulseIn(Echo, HIGH);
Adistance= Adistance / 58;
return (int)Adistance;
}
{
D1=DistanceA();
boolean Ba = digitalRead(B1);
boolean Bb = digitalRead(B2);
Serial.print(D1);
Serial.print(“cm”);
Serial.println();
delay(100);
if(Ba==HIGH){
tourner();
if(Bb==HIGH){
stop();
delay(1000);
avancer();
if(D1<15){
stop();
}
}
}
}

I can’t do a for endfor or a while endwhile because I’ve the Distance_A , the ultrasonic sensor calculated the distance everytime so If I do a for endfor , it will stop the calcul of the distance

Please edit your post to use code tags. The forum software eats some of your code if you don't.

It's also a good idea to use the autoformat function in Arduino before copying the code. It makes it easier to read.

Please also post the complete sketch

Maxcubex34: Hi,

I put the order exit(0); at the end of my loop to stop Motor DCC when my vehicle arrive at destination. And I would like to know if it's possible to restart the loop with (in my example) a microphone who recognizes the sound defined before.

In C/C++ functions are called, and the return back to the caller when they have done what they are designed to do.

exit() is a function just like any other function like loop() setup() digitalWrite() etc... However, exit() is a bit special in that it is built in function provided by the C compiler library that does not return. When exit() is called in Arduino, the processor goes into a hard loop that never returns. In a system with a real operating system, exit() would exit the program and return control back to the parent process. But since there is no OS and no parent process in an embedded system like Arduino, the exit() function just spins and loops forever, effectively halting the processor.

In Arduino, the C main() function calls loop() over and over again.

if you have a condition and want to return from your loop() function somewhere in the middle of your loop() code rather than at the bottom, simply use:

return;

That will return from your loop() function back to main() and the Arduino provided main() function will call loop() again, which effectively "re-starts" loop.

--- bill