Go Down

Topic: Is it possible to end a program forcefully? (Read 983 times) previous topic - next topic

LockDots

I'm still trying to wrap my head around how the arduino is programmed compared to a Basic Stamp (pBasic).

In pBasic every program must have an END command to stop the program. I've noticed that in C++ (or at least arduino programming) the main program is basically a LOOP command that will repeat endlessly until power is removed from the arduino or the reset button is pressed.

My question is, can the arduino be programmed to end or will I have to encompass the entire program within a WHILE loop? If not, what would be a better way to program it to do so?

I have a flowchart made for a program where a decision is made. Each decision leads to a sequence of events that differ from each other but ultimately end. When I say it ends, I mean that the program cannot be run again until the arduino is powered down and back up again.

billroy

If you put all your code in setup() and have an empty loop() it will do pretty much what you want.

-br

MikMo

The Loop() will alway run.

So you must structure your program in such a way that there will be a state where the loop is "idling".

That does not mean that everything must be in the loop, you can have functions outside the loop that you call from within the loop.


retrolefty

#3
Jan 14, 2013, 10:18 pm Last Edit: Jan 14, 2013, 10:23 pm by retrolefty Reason: 1
Any time you want the sketch to just do nothing else forever just add a:

Code: [Select]
noInterrupts();
while(1) {}


Only a reset or power off/on will restart the sketch.

Lefty

LockDots


Any time you want the sketch to just do nothing else forever just add a:

Code: [Select]
noInterrupts();
while(1) {}


Only a reset or power off/on will restart the sketch.

Lefty



hmm, I think I like this one best so far. Care to explain what it's doing or waiting for? Thanks in advance!

retrolefty



Any time you want the sketch to just do nothing else forever just add a:

Code: [Select]
noInterrupts();
while(1) {}


Only a reset or power off/on will restart the sketch.

Lefty



hmm, I think I like this one best so far. Care to explain what it's doing or waiting for? Thanks in advance!


Well the noInterrupts() statement turns off all interrupt sources, so the timer interrupt that works millis() does not function.

The while(1) {}  says as long a 1 equals 1 (it does always), do the statements inside the {} continuously, and as it's empty, it will just stay in a tight loop there doing nothing.

Lefty

LockDots


retrolefty

well in true C fashion you really should just turn it into a user defined function that you can call anywhere in your sketch you want, as so add the following code either before the setup() function of after the loop() function.

Code: [Select]
void stopSketch(void)
    {
     noInterrupts();
     while(1) {}
    }


So anywhere you want to stop the sketch just add the statement

stopSketch()


Lefty


LockDots

How would starting that function starting with:

Code: [Select]
void stopSketch(void)

Differ from starting it with:
Code: [Select]
void stopSketch()

wildbill

The majority of arduino projects you'll see on the forum run continuously, sensing something and acting on it. Some of course do have a definite end - beer brewing would be an example. Purely out of curiosity, what are you making?

LockDots

#10
Jan 14, 2013, 11:26 pm Last Edit: Jan 14, 2013, 11:29 pm by LockDots Reason: 1

The majority of arduino projects you'll see on the forum run continuously, sensing something and acting on it. Some of course do have a definite end - beer brewing would be an example. Purely out of curiosity, what are you making?


My wife's car has Push-To-Start ignition and as of right now there are no Immobilizer bypass or remote start solutions for it, so I'm creating one.

I have a remote start already, and an extra key-fob. I want to use the arduino to sense the remote start having been activated. Once 'activated' it will power on a burried key-fob so that the key information is radiated via RF into the car allowing engine startup. Next it will power up the brake circuit to simulate pressing in the brake pedal. The Arduino will then pulse the PTS button to fire up the engine, shutting off the key-fob afterwards preventing someone from getting into the vehicle and driving off with it. (the next part is the reason for wanting to end the program)

At this point the arduino will monitor the remote start's Status wire (shows ground when running, and floats high when off). If the Status wire drops ground it will pulse the PTS button twice to shut off the engine, and then pulse the driver's door pin wire to turn off the dome lights preventing battery drain. This is essentially to shut off the vehicle should the remote start time-out.

If the Arduino instead sees the brake pedal being pressed while the remote start is still running, it will not pulse the PTS button but will power off the remote start via the Brake Pedal Input sensing wire on the remote start. This will essentially allow key takeover.

Hope all that made sense :)

retrolefty


How would starting that function starting with:

Code: [Select]
void stopSketch(void)

Differ from starting it with:
Code: [Select]
void stopSketch()


The void stopSketch(void) is telling the compiler when it first compiles this function that it returns no value back to the calling program and requires no argument to be passed to it when it is called by the program.

During use in your sketch you just write stopSketch()  and be done with it.

Lefty


dhenry

Quote

Is it possible to end a program forcefully?


Unplug this thing from power usually does it, :)

In a real mcu environment, you actually have the ability to led the code end and typically you restart from the beginning (making it effective a loop with setup()). In the arduino environment, you don't have that option right away, but you can manually edit the generated cpp file and perform make on your own.

retrolefty


Quote

Is it possible to end a program forcefully?


Unplug this thing from power usually does it, :)

In a real mcu environment, you actually have the ability to led the code end and typically you restart from the beginning (making it effective a loop with setup()). In the arduino environment, you don't have that option right away, but you can manually edit the generated cpp file and perform make on your own.



Well you certainly can within a sketch enable the watch dog timer (WDT) to time out and interrupt in just 25 millisec if you wish, which will effect a reset to the chip just as if you hit the reset switch or cycled the power off and on.

Lefty

Delta_G



How would starting that function starting with:

Code: [Select]
void stopSketch(void)

Differ from starting it with:
Code: [Select]
void stopSketch()


The void stopSketch(void) is telling the compiler when it first compiles this function that it returns no value back to the calling program and requires no argument to be passed to it when it is called by the program.

During use in your sketch you just write stopSketch()  and be done with it.

Lefty




When I give my function declaration or definition is there any difference whether or not I put void in the parameter list or if I just use empty parenthesis?  I know both will compile.  But is there any larger reason to put the second void in there in the definition or declaration?

Go Up