Go Down

Topic: How to remain inside a function? (Read 1 time) previous topic - next topic

Lsnyman

I need some assistance to understand structuring my own functions. I can create a function and call the function based on a decision in the void loop() but why does the program return to the loop afterward?
How do I structure it to just remain within the function once it has branched there. I know it sounds silly with this example but basically my application makes a decison between 2 different sets of data to execute based on the initial value of the analog input. Once that decison is made it should remain within the function. So in my example code I branch to option 1 which is executed and I would never expect the Serial.println ("Why?")  statement to be executed but it is.
Can someone explain why and tell me the correct way to structure this?

the output is -
Why?
0
1
2
3
Why?
0
1
2
3
Why?
0
1
2
3
Why?


Code: [Select]

int i = 1;
//==========================================================================================================//
void setup()
{
  Serial.begin(9600);
   }
//================================================================================================//
// Decision between option1 & option2
void loop()

   if (i = 1){
   option1();
          } 

   else if (i = 2){
     option2();
    }
  Serial.println("Why?");
}
//===============================================================================================================================================//
void option1()
{
      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);
        }

Arrch

That's just how structured programming was designed. If you want to stay inside the loop forever, then encapsalate the code within the function in a while (1) { } although I can't imagine their are many circumstances where that's actually what is wanted.

Code: [Select]
if (i = 1){
Not how you write an if statement.
= is assignment
== is equivalence

Dyslexicbloke

#2
Oct 11, 2012, 05:50 am Last Edit: Oct 11, 2012, 05:57 am by Dyslexicbloke Reason: 1
Since you are obviously new to all this, we've all been there, you might like to draw a flow chart .... It will help you visualise what you want to do.

I don't understand why you would want to pick a loop and stay there either but if that is what you need then while is the simplest construct
Have a look at the reference section ...
http://arduino.cc/en/Reference/While
Code: [Select]

void loop()
while(i = 1){
  // statement(s)
}
while(i = 2){
  // statement(s)
}
Serial.println("Why?");
}

If i is not 1 or 2 you will continually print why.
if i is 1 or 2 you will continually loop through the code in one of the while loops.

In the  Arduino IDE ...
void Setup() always runs first, but only once.
then
void loop() is called and any code within it is executed sequentially, when you reach the bottom it simply starts again, no matter how long or short it is
(within constraints of time and available memory)

If you call a function from anywhere, including the main loop, the code in it will run and when its done the control is passed back to the line immediately below the function call in the original code, a bit like a detour off the path to visit something on one side.

You are going to need to read up on some basics to get a feel for things.
I hope I have given you somewhere to start
If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

Nick Gammon

Code: [Select]

while(i = 1){
  // statement(s)
}


Should be:

Code: [Select]

while(i == 1){
  // statement(s)
}


Ditto to the OP.

Lsnyman

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.
Code: [Select]
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);
        }
}

Go Up