I'm currently working on code that does reliability testing for switches. Currently I have it testing for contact bounce, and if there are 20 bounces in 100 valid presses I want to exit void loop() (see portion of code below).
if(pressCount%100 == 0) //check number of bounces every 100 real presses
{
if(bounceCount%100 > 20) //check if more than 20 bounces (subject to change)
{
digitalWrite(outPin, HIGH); //send signal to PLC to stop testing
Serial.print(totalBounceCount); //print total number of bounces
Serial.println(" total bounces");
break; //exit loop()-------------------ERROR HERE: "break statement not within loop or switch"---------------
}
else
bounceCount = 0; //reset bounce tracking
}
If you read above you can see I'm getting the error "break statement not within loop or switch", but through my online readings I thought this would work to exit the loop() function.
So does break; only work in actual loops within loop()? And if so what could I use to exit the loop() function?
It's been a while since I've programmed and I thought of trying to return 0, but since loop is void I knew that wouldn't work. Anyways, it compiles now with just return. Thanks for the help.
I believe that none of the solutions work in this case. In Arduino we only see one part of the code, to be more easy understandable for "artist, hobbyist", and other users not used to programming and electronics. The part of the code that we don't see is this:
include <Arduino.h>
int main(void)
{
init();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
So, inside the function loop() we don't have any for or while loops to break for. Instead of that the for is outside that function. If we return from the function loop() the function will be aclled another time, because this function is inside one for loop.
In my opinion, if what you want to do is stop the execution of the program, one thing that you can do is:
if (some_condition) {
while (1)
;
}
This will cause the execution of the program to stop, because if the condition is satisfied the execution will "fall" in one "repeat for ever" loop.
I don't know if something like this will work for you.
luisilva:
In my opinion, if what you want to do is stop the execution of the program, one thing that you can do is:
if (some_condition) {
while (1)
;
}
This will cause the execution of the program to stop, because if the condition is satisfied the execution will "fall" in one "repeat for ever" loop.
I don't know if something like this will work for you.
Another way you could try:
boolean failed = 0; //boolean false
void loop(){
if(failed== 0){
///do the rest of your code and
//set failed to 1 (boolean true) if condition met
}
}
loop() will still run continuously, but will skip over your code if there has been a failure.
Remember, though, that with both these methods you'll have to do a reset or power off to get it doing something useful again.
Just to be complete, break can be used in any loop-construct and in switch
statements. loop constructs are for, while and do-while. continue can be
used in any loop-construct.