Help! "reset"the void loop

Hi, i'm creating car remote starter using arduino nano, receiver and some relays. one of the demands from this project was to be able to stop the code (by trigger), reset some variables and reset the void loop(make the code start from the beginning again).

i was thinking about interrupts witch can help me to reset some variables when trigger occurs but the problem is after the interrupt the code execution goes back to the last line it was and that line can be delay function of 5 minutes is there is any command i can "reset"the void loop? i,e make it run from the beginning?

need help guys... thanks

This is abit of a hack but it will cause the Arduino to seg fault and restart.

However, I will also add that loop is repeated every time it completes, unless you are not allowing loop to end. You should be able to manage this by managing state.

void(* resetFunc) (void) = 0;  // Create a function pointer that points nothing



resetFunc();  //Call function when you want to force the Arduino to eset.

Perhaps if you explain a bit more what you want, we can do it without this hack.

These sorts of tasks are usually done with state machines.

You'll probably get more predictable operation by resetting the state variable than trying to "reset" loop().

Do you have code you can post?

Romonaga: This is abit of a hack but it will cause the Arduino to seg fault and restart.

However, I will also add that loop is repeated every time it completes, unless you are not allowing loop to end. You should be able to manage this by managing state.

void(* resetFunc) (void) = 0;  // Create a function pointer that points nothing


resetFunc();  //Call function when you want to force the Arduino to eset.










Perhaps if you explain a bit more what you want, we can do it without this hack.

the idea behind the code is:

A,shutdown are my inputs (triggers) ignition1,ignition2,starter are my output

endless loop that's waiting for trigger ( lets call him "A") when A occurs => set ignition1=HIGH set ignition2=HIGH => wait 3 seconds =>set starter=HIGH=> while A present do nothing=> if no more A, set starter=LOW and wait 5 minutes=>set ignition1=LOW set ignition2=LOW=>go to start(endless loop that's waiting for trigger ).

that code have to work with this condition: if whenever in the code trigger( lets call him "shutdown") occurs=>set ignition1=LOW set ignition2=LOW set starter=LOW=>go to start(endless loop that's waiting for trigger ).

+

Blackfin:
These sorts of tasks are usually done with state machines.

Right, forget the hack. Learn to do it the correct way.

What you are wanting to do can be done without the hack I presented. Read up on state management.

Romonaga: it will cause the Arduino to seg fault

No.

It will cause a jump via the reset vector.

This is from the ATmega datasheet:

11.2 Reset Sources
The ATmega48A/PA/88A/PA/168A/PA/328/P has four sources of reset:
• Power-on Reset. The MCU is reset when the supply voltage is below the Power-on Reset threshold (VPOT).
• External Reset. The MCU is reset when a low level is present on the RESET pin for longer than the minimum
pulse length.
• Watchdog System Reset. The MCU is reset when the Watchdog Timer period expires and the Watchdog System
Reset mode is enabled.
• Brown-out Reset. The MCU is reset when the supply voltage VCC is below the Brown-out Reset threshold (VBOT)
and the Brown-out Detector is enabled.

So the watchdog timer looks like the only way to “legitimately” do a software reset. There is an example in [u]this thread[/u].

reset some variables and reset the void loop(make the code start from the beginning again).

Normally, you ARE re-starting the main loop over-and over, and you call a function (or otherwise use an if-statement) to re-set any variables that were initialized in setup().

And/or your “main code” doesn’t have to run in the main loop, and you’re not required to run setup(). You can put your “real loop” inside the main loop. Or, you can call a function from the main loop never returns (and probably contains one or more loops of it’s own).

Most C/C++ programs (running on a computer) have a main() function instead of a main loop and() I’d expect that to work on the Arduino.

P.S.
A long-long time ago I made a car alarm with a different microcontroller. It finally died, but it ran for almost 20 years and it ran 24/7 even when the alarm was “disabled”. The only time it was reset was when the car battery died every few-years, or if the alarm was temporarily disconnected for some reason.

OP, here’s some proto-code that might help give you some ideas. Not complete. Not tested or compiled. Treat as pseudo-code:

void setup()
{
    //...

}//setup

void loop( void )
{
    StateMachine();
    
}//loop

bool CheckShutdown( void )
{
    //if shutdown signal is active, return true; will force reset of state machine
    return( (digitalRead( pinShutdown ) == LOW) ? false:true );
    
}//CheckShutdown

#define INIT_DELAY              5000ul          //5-sec from initialization/SM reset to allow starting sequence
#define POST_IGN_DELAY          3000ul          //3-sec after ignitions turned on before starter engaged
#define MAX_RUN_TIME            300000ul        //5-min run time after start before auto-shutdown

#define ST_INITIALIZE           0
#define ST_INIT_DELAY           1
#define ST_IDLE                 2
#define ST_IGN_DELAY            3
#define ST_STARTING             4
#define ST_RUNNING              5

void StateMachine( void )
{
    static unsigned long
        timeRemote;
    static byte
        stateRemoteStarter = ST_INITIALIZE;
    unsigned long
        timeNow;

    //if at any time the shutdown signal is seen we reset the state machine
    if( !CheckShutdown() )
        stateRemoteStarter = ST_INITIALIZE;

    timeNow = millis();
    switch( stateRemoteStarter )
    {
        case    ST_INITIALIZE:
            //initialize the ignition and starter relays "off"
            digitalWrite( pinIGN_1, LOW );
            digitalWrite( pinIGN_2, LOW );
            digitalWrite( pinSTARTER, LOW );
            //after initialization, allow a delay period of INIT_DELAY
            timeRemote = timeNow;
            
            stateRemoteStarter = ST_INIT_DELAY;
            
        break;

        case    ST_INIT_DELAY:
            //after SM reset/initialization, wait INIT_DELAY before allowing remote start
            if( timeNow - timeRemote <= INIT_DELAY )
                return;
                
            stateRemoteStarter = ST_IDLE;
            
        break;

        case    ST_IDLE:            
            //here we wait for the trigger to begin start sequence
            if( digitalRead( pinTrigA ) == HIGH )
            {
                //when received, set the ignitions "on"...
                digitalWrite( pinIGN_1, HIGH );
                digitalWrite( pinIGN_2, HIGH );
                //...and grab the current time so we can count our 3-sec delay
                timeRemote = timeNow;
                
                stateRemoteStarter = ST_IGN_DELAY;
                
            }//if
            
        break;

        case    ST_IGN_DELAY:                
            //after ignition on, wait POST_IGN_DELAY before engaging starter
            if( timeNow - timeRemote < POST_IGN_DELAY )
                return;

            //engage starter
            digitalWrite( pinSTARTER, HIGH );
            stateRemoteStarter = ST_STARTING;
                
        break;

        case    ST_STARTING:
            //continue to crank until trigger shows low
            if( digitalRead( pinTrigA ) == LOW )
            {
                //when low, turn off starter
                digitalWrite( pinSTARTER, LOW );
                //grab time now so we can time the 5-min idle time
                timeRemote = timeNow;
                
                stateRemoteStarter = ST_RUNNING;
                    
            }
            
        break;
        
        case    ST_RUNNING:
            //may also add a check of, say, the ignition key to allow driver to enter car and insert key without shutting down
            //may also add a check of oil pressure; after some delay (to allow pressure to build); if pressure drops below
            //safe value for more than 1 few seconds, force shutdown...
            //
            //for now you just asked for time
            if( timeNow - timeRemote <= MAX_RUN_TIME )
                return;

            //if 5-min elapses we shut things down by going back to initialize state
            stateRemoteStarter = ST_INITIALIZE;
            
        break;
        
        
    }//switch
                
}//StateMachine

Blackfin:
OP, here’s some proto-code that might help give you some ideas. Not complete. Not tested or compiled. Treat as pseudo-code:

void setup()

{
    //…

}//setup

void loop( void )
{
    StateMachine();
   
}//loop

bool CheckShutdown( void )
{
    //if shutdown signal is active, return true; will force reset of state machine
    return( (digitalRead( pinShutdown ) == LOW) ? false:true );
   
}//CheckShutdown

#define INIT_DELAY              5000ul          //5-sec from initialization/SM reset to allow starting sequence
#define POST_IGN_DELAY          3000ul          //3-sec after ignitions turned on before starter engaged
#define MAX_RUN_TIME            300000ul        //5-min run time after start before auto-shutdown

#define ST_INITIALIZE          0
#define ST_INIT_DELAY          1
#define ST_IDLE                2
#define ST_IGN_DELAY            3
#define ST_STARTING            4
#define ST_RUNNING              5

void StateMachine( void )
{
    static unsigned long
        timeRemote;
    static byte
        stateRemoteStarter = ST_INITIALIZE;
    unsigned long
        timeNow;

//if at any time the shutdown signal is seen we reset the state machine
    if( !CheckShutdown() )
        stateRemoteStarter = ST_INITIALIZE;

timeNow = millis();
    switch( stateRemoteStarter )
    {
        case    ST_INITIALIZE:
            //initialize the ignition and starter relays “off”
            digitalWrite( pinIGN_1, LOW );
            digitalWrite( pinIGN_2, LOW );
            digitalWrite( pinSTARTER, LOW );
            //after initialization, allow a delay period of INIT_DELAY
            timeRemote = timeNow;
           
            stateRemoteStarter = ST_INIT_DELAY;
           
        break;

case    ST_INIT_DELAY:
            //after SM reset/initialization, wait INIT_DELAY before allowing remote start
            if( timeNow - timeRemote <= INIT_DELAY )
                return;
               
            stateRemoteStarter = ST_IDLE;
           
        break;

case    ST_IDLE:           
            //here we wait for the trigger to begin start sequence
            if( digitalRead( pinTrigA ) == HIGH )
            {
                //when received, set the ignitions “on”…
                digitalWrite( pinIGN_1, HIGH );
                digitalWrite( pinIGN_2, HIGH );
                //…and grab the current time so we can count our 3-sec delay
                timeRemote = timeNow;
               
                stateRemoteStarter = ST_IGN_DELAY;
               
            }//if
           
        break;

case    ST_IGN_DELAY:               
            //after ignition on, wait POST_IGN_DELAY before engaging starter
            if( timeNow - timeRemote < POST_IGN_DELAY )
                return;

//engage starter
            digitalWrite( pinSTARTER, HIGH );
            stateRemoteStarter = ST_STARTING;
               
        break;

case    ST_STARTING:
            //continue to crank until trigger shows low
            if( digitalRead( pinTrigA ) == LOW )
            {
                //when low, turn off starter
                digitalWrite( pinSTARTER, LOW );
                //grab time now so we can time the 5-min idle time
                timeRemote = timeNow;
               
                stateRemoteStarter = ST_RUNNING;
                   
            }
           
        break;
       
        case    ST_RUNNING:
            //may also add a check of, say, the ignition key to allow driver to enter car and insert key without shutting down
            //may also add a check of oil pressure; after some delay (to allow pressure to build); if pressure drops below
            //safe value for more than 1 few seconds, force shutdown…
            //
            //for now you just asked for time
            if( timeNow - timeRemote <= MAX_RUN_TIME )
                return;

//if 5-min elapses we shut things down by going back to initialize state
            stateRemoteStarter = ST_INITIALIZE;
           
        break;
       
       
    }//switch
               
}//StateMachine

thank you every one,
especially Blackfin ! thank you! you really helped me! you understood me very very well!by the comments you wrote in the sketch you posted i can assume you done something familiar?