I am trying to figure out how to break out of a loop if it is being called from a function. I have a similar post, here, that I've decided to do something with the Arduino onboard LED, to isolate it from the NeoPixels Library and it is giving me the same issue. The answers I have received there have been helpful but something is still puzzling me.
Using serial from the Arduino to my PC, the below "Example 1" code, can break out of a local loop successfully (and immediately) when the Arduino received keyboard input with key "2".
//Example 1
int ledPIN = 13; // sets up onboard LED on Arduino located near PIN13
char rx_byte = 0; // variable to hold serial input from console;
int count = 0; // placeholder for incrementing during debugging of loops
void setup() {
Serial.begin(115200);
pinMode (ledPIN, OUTPUT);
}
void loop() {
if (Serial.available() > 0) { // is a character available?
rx_byte = Serial.read(); // get the character
// check if a number was received
if ((rx_byte >= '0') && (rx_byte <= '9')) {
switch(rx_byte) {
case '1':
Serial.println("You've entered: 1");
for (int i; i <=5000; i++) {
rx_byte = Serial.read(); // must read character again
if (rx_byte == '2') {
Serial.println("Quitting...");
digitalWrite(ledPIN, LOW); // turn LED pin 13 off
break;
}
else {
Serial.println(i);
digitalWrite(ledPIN, HIGH); // turn LED pin 13 on
}
}
digitalWrite(ledPIN, LOW); // turn LED pin 13 off
break;
}
} // end: if (Serial.available() > 0)
}
}
From what I've been told, you shouldn't necessarily perform a "serial.read()" inside of a loop. However, if I do not include that in the for loop, it does not allow for any further input.
Since "Example 1" works, I am now trying to accomplish the same principle, but, this time, when calling a function which contains its own independent loop of course. I've spent countless hours trying to resolve this.
I was under the impression that I could use a while loop within "void loop()" and call in a function which also contains a loop, then be able to break it from the while loop. When attempting this various ways, the for loop within the function seems to blink the LED 10+ times and totally ignores my keystroke "2" to break the loop and is now infinite. I know I could just put the for loop up in "void loop()" and it would work to break out similar to Example 1, but I'm sure it can be broken from a function as well. I will eventually have similar functions that will contain parameters.
How would I go about doing this the correct way?
Below is "Example 2" that I am trying to break from the function. I'm assuming it's an issue with doing it from the while loop?
Again, any assistance is greatly appreciated.
//Example 2
int ledPIN = 13; // sets up onboard LED on Arduino located near PIN13
char rx_byte = 0; // variable to hold serial input from console;
int count = 0; // placeholder for incrementing during debugging of loops
void setup() {
Serial.begin(115200);
pinMode (ledPIN, OUTPUT);
}
void loop() {
if (Serial.available() > 0) { // is a character available?
rx_byte = Serial.read(); // get the character
boolean forceTerminate = false;
// check if a number was received
if ((rx_byte >= '0') && (rx_byte <= '9')) {
switch(rx_byte) {
case '1':
Serial.println("You've entered: 1");
while(forceTerminate == false) {
rx_byte = Serial.read(); // must read character again
if (rx_byte == '2') {
Serial.println("Quitting...");
digitalWrite(ledPIN, LOW); // turn LED pin 13 off
forceTerminate = true;
break;
}
else {
//count = count+1;
//Serial.println(count);
blinkPIN13(); // FUNCTION: blinkPIN13
}
}
digitalWrite(ledPIN, LOW); // turn LED pin 13 off
break;
} // end: switch(rx_byte)
}
} // end: if (Serial.available() > 0)
}
/************************************************************************************
* FUNCTION for blinking LED located on PIN 13 to the state of ON(1)/OFF(0)
************************************************************************************/
void blinkPIN13() {
for (int i; i <=10; i++) {
digitalWrite(ledPIN, HIGH);
delay(1000);
digitalWrite(ledPIN,LOW);
delay(1000);
}
}