start code from the begining .How?

I have a program that makes some tasks and it checks for infrared signals.

all i want is every time it checks for IR signals i want the scetch to start from begining. I tried with goto but it only works inside their void

byte incoming_IR_data;
void setup() {                

  pinMode(13, OUTPUT);     
}

void loop() {
  start:
  task_0(); 
  task_1();
  task_2();  
}

void task_0(){
  
  //some code here
   check_IR();
}
void task_1(){
  
  //some code here
   check_IR();
}
void task_2(){
  
  //some code here
   check_IR();
}
void check_IR(){
 //Some code that checks IR and stores data from Infrared to incoming_IR_data variable
 
     if (incoming_IR_data==12524){
       goto start;   
     }
  
  
}

You do realise that the very purpose of the loop() function i to run the same code again and again don't you ?

void loop() 
{
  task_0(); 
  check_IR();
}

will call the task_0() function then the check_IR() function then task_0() then check_IR() and so on with no special instructions. Your 'code' seems to indicate that the loop should only start again if a specific code in read by the IR function.

Can you explain more clearly what you want to do ?

you are right. my original scetch is really big so i decided to give you an example on what i want to do.

so check again my code.
it runs many task and in all these tasks it checks for IR.
all i need is to start from begining when a IR signal is coming

I tried with goto but it only works inside their void

A void is nothing - a null, an emptiness, a big hole.
Did you mean "function"?

yes. function

acmiilangr:
I have a program that makes some tasks and it checks for infrared signals.

all i want is every time it checks for IR signals i want the scetch to start from begining. I tried with goto but it only works inside their void

Since you want the 3 tasks to do something different, (and presumably if you notice IR data after task_1 you want to skip task_2), you might want something like this: (using flags)

byte incoming_IR_data;
boolean IR_received;  // Flag for knowing if you have to do something different when IR information has been received. Should initialize to 0 (which ==FALSE)

void setup() {                

  pinMode(13, OUTPUT);     
}

void loop() {
  if (IR_received)  // 
  {
    // code to handle IR stuff if needed. Otherwise unwrap the following line out of the if statement.
    IR_received = FALSE;  // (Re)set the received flag
  }
  task_0(); 
  task_1();
  task_2();  
}

void task_0()
{
  // don't need to check IR_received here because we haven't checked IR communication since we last reset the flag
  //some code here
   check_IR();
}
void task_1()
{

  if (!IR_received)  // if IR_received == FALSE
  {
    return; // return from the function w/o doing anything
  }
  
  //some code here
   check_IR();
}
void task_2()
{

  if (!IR_received)  // if IR_received == FALSE
  {
    return; // return from the function w/o doing anything
  }
  
  //some code here
   check_IR();
}
void check_IR()
{
 //Some code that checks IR and stores data from Infrared to incoming_IR_data variable
 
     if (incoming_IR_data==12524)
     {
       IR_received == TRUE;  // Set the flag that basically neuters the other functions.
     }
}

Now, I'm not sure if this is what you really want, but I think it should do what you asked. :wink:

It's still not that clear what you want to do...

loop() will automatically loop/repeat, unless you are stuck in another loop.

You can break out of a for-loop, while-loop, or do-while loop, with [u]break[/u], but you shouldn't generally break-out of your main loop because your program will end and nothing will happen 'till you hardware-reset.

Or as Sembazuru suggested, you may need an [yurl=http://arduino.cc/en/Reference/If]if[/url]-statement.

I tried with goto but it only works inside their void.

  • loop() is a function.

  • void at beginning of a function prototype or function definition means the the function returns nothing when the function ends/returns. Otherwise, you'll see the varaible-type that's returned by the function (such as int).

  • The empty parenthesis indicate that no values are passed into the function.

And FYI - It's generally considered "bad practice" to use goto in C/C++.

well this is what i have made using arduino code:

the code is really complicated and big. user can program it via IR remote control.

each loop takes about 1-3 minutes. I want the add a new feauture now. "night mode"
when user presses "off" button on remote control, the cross display need to show only temperature. (this is night mode).
i succed to do that with a flag, but the functions are spending a lot of time and it needs to finish one of them and then go to night mode.
for example when it scrolling the time, when user presses "off" button it waits to finish this fuction (maybe about 5-10 seconds) and then it goes to night mode.. and this is not what i want from that. i want to turning on night mode in real time

edit.

i found this
void(* resetFunc) (void) = 0; //declare reset function @ address 0
...
resetFunc(); //call reset

is it safe to use it?

I don't understand what going in to 'night mode' has to do with starting your code from the beginning. Can you please explain ?

there is a function that checks if there is "OFF" signal from IR remote control (lets name this function IR_CHECK). if user presses off button then it stores "1" to a temp variables. if user press "ON" button it stores "0" to that temp variable

on my project there are many other functions (displaying graphic efe,showing scrolling text and other)
all these functions are calling IR_CHECK function to check if there is "OFF" or "ON" signal. and they works like this:

if (temp==0){
task_0();
task_1();
task_2();
//...
//...
}
if (temp==1){
task_temperature();//show only temperature
}

so when user presses OFF button the only fanction that runs is task_temperature. the only problem is that it needs to wait to finish the current task (some of them are spending some seconds)

A better way to achieve what you're asking for is to redesign your sketch to be non-blocking so that loop() runs to completion instead of waiting for your display to complete, and put your test for IR input at the start of loop().

I think you're trying to complicate something that is really simple!

const uint8_t   pinLED  = 13;

byte incoming_IR_data;

bool check_IR()
{
    // Some code that checks IR and stores data from Infrared to
    // incoming_IR_data variable

    if ( incoming_IR_data == 12524 )
    {
        return false;
    }
}

bool task_2()
{
    bool    result;
    
    result = check_IR();
    if ( ! result )
    {
        return result;
    }

    ...

    return true;
}

bool task_1()
{
    bool    result;
    
    result = check_IR();
    if ( ! result )
    {
        return result;
    }

    ...

    return true;
}

bool task_0()
{
    bool    result;
    
    result = check_IR();
    if ( ! result )
    {
        return result;
    }

    ...

    return true;
}

void loop()
{
start:
    if ( false == task_0() ) return;
    if ( false == task_1() ) return;
    if ( false == task_2() ) return;
}

void setup()
{
    pinMode(pinLED, OUTPUT);     
}

If you really want to do this, check out set_jmp() and jmp().

But it might suck up a lot of stack.