Go Down

Topic: call function (Read 735 times) previous topic - next topic

MostafaHamdy

Code: [Select]
const int Sensor1 = 2 ;
int i;
void setup ()
{
  pinMode(Sensor1,INPUT);
}
void loop ()
{Sensor_1:
// statement...............
}

void delay_function ()
{
for(i=0;i<10000;i++)
  {
  if (digitalRead(Sensor1) == 0)
     {
       goto Sensor_1 ;
     }
  }
}


can i use goto function to Transfers program flow between different functions such as goto from void delay_function()  to void loop()

gardner

no.  I am not a goto-hater, but goto will not branch across functions.

If you think you need that, you better reconsider your program logic.

MikMo

Using Goto is considered very poor programming practice. It makes it very difficult to read a program and to understand the structure of the program


You just call your function like this:

Delay_function();

When it is finished doing what it should, program flow will be returnd to where you called it from.

holmes4

Never use goto's in a C/C++ program forget they exist. And No you can't.

Mark

MostafaHamdy



You just call your function like this:

Delay_function();

When it is finished doing what it should, program flow will be returnd to where you called it from.

i need when make specific action such as press switch exit from Delay_function() and back to the void loop (), what order can i used 

MikMo

Your loop() is alway running.

So if you call your function from within loop, program flow will always be returned to loop when you exit your function.

So you need to implement the logic to read the switch in your function and then exit the function when the switch is pressed.

MichaelMeissner

#6
Jan 07, 2013, 11:11 pm Last Edit: Jan 07, 2013, 11:14 pm by MichaelMeissner Reason: 1
The C method of doing what you want is using the setjmp and longjmp functions.

Code: [Select]

#include <setjmp.h>

jmp_buf unwind_stack;

void
inner_function (void)
{
   if (some_condition ()) {
       lonjmp (unwind_stack, 1);
   }
}

void
loop (void)
{
   if (setjmp (unwind_stack) == 0) {
      /* normal code, do whatever you need here.  */
      inner_function ();

   } else {
      /* code after longjmp is called.  */
      Serial.println ("whoops");
   }
}


The C++ method uses try/catch exceptions (http://www.cplusplus.com/doc/tutorial/exceptions/).  I am unsure if exceptions are fully supported in the Arduino environment.

However, as others have said, you probably should restructure your code not to need exceptions.  I tend to think you want to use state machines, where you have a bunch of variables that say what to do, and check those each time inside of loop.  Perhaps reading and really understanding blink without a delay (the time to restart the function is a simple minded state machine) would be a start.

gardner


i need when make specific action such as press switch exit from Delay_function() and back to the void loop (), what order can i used 


Perhaps you should read about what "break" and "return" do.  You probably want to use "return".

These, together with "continue" are softer forms of goto that the goto-haters tolerate.

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview