Pages: [1]   Go Down
Author Topic: Add a function noLoop() to the standard functions  (Read 1082 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"I want my code to run only once! How to?!"

This question comes up every once in a while, including today. Solution:

#define noLoop() while(1)

Explain it in standard functions and have an example like other functions.

Processing has a noLoop() you can call too.
Logged


Guildford, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 217
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or just put the code in setup()?

Iain
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, a different method is to put it in setup and the loop() is then only called once. Either way works. It's a simple fix that I proposed.
Logged


Guildford, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 217
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure you understood my comment. Instead of putting your code in loop() put it into setup(). In this case loop() is empty and you don't care how often it's called.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 168
Posts: 12430
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

people asking for such a function/define need to learn a bit more about programming, and spent more time on the tutorial and reference section. I think the question will still be asked even if it was part of the language. Its in the nature of tinkerers smiley

by the way I would call it STOP
#define STOP() while(true)

There are many more of this kind of #defines like

#define REPEAT do {
#define UNTIL(x)  }while(!(x));

I once saw a set of defines to create a string switch

SWITCH(string)
{
  CASE("monkey") : ...
  BREAK;
  CASE("fish") smiley-small..
  BREAK;
  DEFAULT ....
  BREAK;
}
implementation left as an exercise for the reader .... smiley

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Two things...

First, "noLoop()" versus "exit(0);".  The former requires either defining the macro or an addition to the Arduino software.  The latter is ready to use.  The former leaves the interrupt flag alone which potentially allows the Sketch to limp along.  The latter disables interrupts effectively shutting down all execution.  They are the same number of characters to type.  I think I'll go with the latter.

Second, if you're going to stop execution, you really should put the processor to sleep.  (exit does not)
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure you understood my comment. Instead of putting your code in loop() put it into setup(). In this case loop() is empty and you don't care how often it's called.

Now I get it. Right you can do that.
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Two things...

First, "noLoop()" versus "exit(0);".  The former requires either defining the macro or an addition to the Arduino software.  The latter is ready to use.  The former leaves the interrupt flag alone which potentially allows the Sketch to limp along.  The latter disables interrupts effectively shutting down all execution.  They are the same number of characters to type.  I think I'll go with the latter.

Second, if you're going to stop execution, you really should put the processor to sleep.  (exit does not)


Quite technical, but I like it (good luck to the newbies). So what does exit(0) actually do and what command puts the processor to sleep?
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So what does exit(0) actually do

This is from the documentation...

Quote
In a C++ context, global destructors will be called before halting execution.

I have not tested "in a C++ context" so I don't know if destructors are in fact called.  In my test Sketch, this is essentially what was produced...

Code:
void exit( int ignored )
{
  cli();
  while ( true );
}

Quote
and what command puts the processor to sleep?

This should do the trick (untested)...

Code:
#include <avr/sleep.h>

void die( void )
{
  set_sleep_mode( SLEEP_MODE_PWR_DOWN );

  while ( true )
  {
    sleep_enable();
    cli();
    sleep_cpu();
  }
}

The "while ( true )" should not be necessary but I've dealt with enough "should nots" in my life to know that including it is worth the two (or four) bytes of Flash.
Logged

Pages: [1]   Go Up
Jump to: