Pages: [1] 2   Go Down
Author Topic: Is it possible to end a program forcefully?  (Read 811 times)
0 Members and 1 Guest are viewing this topic.
Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

-br
Logged

Copenhagen / Denmark
Offline Offline
Edison Member
*
Karma: 6
Posts: 2360
Do it !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
noInterrupts();
while(1) {}

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

Lefty
« Last Edit: January 14, 2013, 04:23:17 pm by retrolefty » Logged

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Logged

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're the man.

Thank you all so much!
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void stopSketch(void)
     {
      noInterrupts();
      while(1) {}
     }

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

stopSketch()


Lefty

Logged

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would starting that function starting with:

Code:
void stopSketch(void)


Differ from starting it with:
Code:
void stopSketch()
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
« Last Edit: January 14, 2013, 05:29:51 pm by LockDots » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would starting that function starting with:

Code:
void stopSketch(void)


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

Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
   
Is it possible to end a program forcefully?

Unplug this thing from power usually does it, smiley

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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17262
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
   
Is it possible to end a program forcefully?

Unplug this thing from power usually does it, smiley

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
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 721
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would starting that function starting with:

Code:
void stopSketch(void)


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

Pages: [1] 2   Go Up
Jump to: