Thank you for the replies. Using the While statement works well. The actual issue i am having is with Arduino Leonardo. My original code runs without problems on the UNO, because the Uno resets when you connect to USB and that allows my sketch to do the analogRead and make the decision between the 2 loops. However the Leonardo does not reset. So I have been trying to troubleshoot it and wanted to make sure of the structure to use function calls.
I have followed the getting started guide for Leonardo and it says I can use the while (!Serial) ;
statement to do nothing until a serial connection is made. This works when I structure it like the below but somehow when I apply it to my original code, the program just continues to execute even if the Serial Monitor is closed. This means i miss the calibration step in the beginning analogRead.
Sorry, I know it is confusing.
int i = 1;
//==========================================================================================================//
void setup()
{
Serial.begin(9600);
while (!Serial) ;
}
//================================================================================================//
// Decision between option1 & option2
void loop()
{
if (i == 1){
Serial.println("Option 1");
option1();
}
else if (i = 2){
Serial.println("Option 2");
option2();
}
Serial.println("Why?");
}
//===============================================================================================================================================//
void option1()
{
while (i==1){
for (int i=0; i <= 3; i++){
Serial.println(i);
delay(200);
}}
}
//============================================================================================//
void option2()
{
for (int i=10; i <= 13; i++){
Serial.println(i);
delay(200);
}
}