Is it possible to end a program forcefully?

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.

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

-br

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.

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

noInterrupts();
while(1) {}

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

Lefty

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

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!

LockDots:

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

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

You're the man.

Thank you all so much!

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.

void stopSketch(void)
     {
      noInterrupts();
      while(1) {}
     }

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

stopSketch()

Lefty

How would starting that function starting with:

void stopSketch(void)

Differ from starting it with:

void stopSketch()

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?

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?

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 :slight_smile:

LockDots:
How would starting that function starting with:

void stopSketch(void)

Differ from starting it with:
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

Is it possible to end a program forcefully?

Unplug this thing from power usually does it, :slight_smile:

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.

dhenry:

Is it possible to end a program forcefully?

Unplug this thing from power usually does it, :slight_smile:

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

retrolefty:

LockDots:
How would starting that function starting with:

void stopSketch(void)

Differ from starting it with:
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?

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?

Unfortunately I'm far from being a C/C++ expert, I like hardware much better. I think the use of void function_name(void) is the formal method but void function_name() seems to be an acceptable default. I would bet that varies with the C/C++ standard the compiler is working with and most likely has changed over time and newer compiler standards. But wait for a real expert to know for sure. In the mean time use what works is my motto. :smiley:

Lefty

I think the abort function is exactly what OP is looking for. It's a library function that does essentially what the stopSketch() function posted does.

But is there any larger reason to put the second void in there in the definition or declaration?

The void in the parentheses after the function name says that you have thought about the list of arguments, and have decided that there are none, so you are explicitly telling the compiler that.

The empty set of parentheses implicitly says that there are no arguments. The resulting object module is exactly the same.