how to only loop once?

is there a way to only run the loop once in an Arduino sketch?

yes
do it like that:

void loop()
{
/* your code here */
....
while(1){}
}

Write code in setup section:

void setup(){
  //code you want to run once
}

void loop(){
  //no code
}

I can't understand what is your goal but this is the way.

what would happen if you'd return from loop?

what would happen if you'd return from loop?

It would just get called again.

#define EVER (;;)
...
...
 void loop () {
  // stuff you want just once

 for EVER;
}

;D

Or, as has already been pointed out, put it in "setup" and leave loop empty.

;D oooooooooooooh I kept reading about how you need void setup () and void loop ().....but it never crossed my mind that you could just leave the loop empty!!! ;D

I can't understand what is your goal but this is the way.

My goal is to stop a sketch from looping :wink:

I don't think its so difficult ...

cloture de piscine

Oh that's OK then, as long as alain marsol doesn't think its so difficult ... ::slight_smile:

Cummon n00b, haven't you memorized all those C books you bought yet? ;D

;D ;D ;D

Why would you only want it to do something once?

A generalization, for running the loop a certain (N) number of times:

int N = 5;
int runXTimes = 0;


void loop()
{
  if (runXTimes < N)
  {
     foo();
     runXTimes++;
  }
}

You may want to run the loop only once when you set a clock, for example.

A generalization, for running the loop a certain (N) number of times:

Amazing how "cheap" memory and instruction cycles have become for embedded applications. I remember sweating out every byte of memory or instruction. Just because I can:

int runXTimes = 5;

void loop()
{
  if (runXTimes)
  {
     foo();
     runXTimes--;
  }
}

Then again, I still see the memory available on the Arduino as a vast expanse while I often see threads here about not enough memory.

I remember sweating out every byte of memory or instruction. Just because I can:

Then you may want to reduce your flash/ram usage further still as in the following:

byte runXTimes = 5;

void loop()
{
  if (runXTimes)
  {
     foo();
     runXTimes--;
  }
}

I am sure there is an assembly version of this code, but then few people would understand that. That was the whole point of switching to C (from assembly), wasn't it?

Then you may want to reduce your flash/ram usage further still as in the following:

byte runXTimes = 5;

void loop()
{
 if (runXTimes)
 {
    foo();
    runXTimes--;
 }
}

Or what about...

void loop()
{
  static byte runXTimes = 5;
  if (runXTimes--)
     foo();
}

Not sure if this saves anything on the AVR, but it will save a couple of instructions on a 68k ;D

How would a clock work if the loop only runs once? It wouldn't be able to update the time or anything?!

Do you really want nothing to happen or do you want the loop to stop (not loop) until something else happens?

The loop by definition runs in a loop, so the setup process runs once and then the loop runs over and over. You can exit the loop and it will simply start again. Putting nothing in the loop is like turning on your computer with no programs running.

I have a do nothing loop that simply says delay(1000);

All it does is delay a second and then .. delay another second - my chip loves that one. I load that when I really do want no activity from my pins.

So if you really did want a "Do Nothing" program then that is as easy just having an empty loop() function.

If that was not it .. are you maybe looking for something that "waits" in the loop?

Example:
Lets say you wanted 'New' to show only one time .. then stop until you send 'n' across the serial port. At that time, it could send 'New' one time .. then stop again and only wait for a serial command.

In that case - you simply process the serial data in the loop and return doing nothing if no data is there, else process the command (which may be 'n' .. in which case you send 'New' over the serial port).

Another Example:
Lets say you wanted 'Go' to be Serial sent every second until you send the letter s across the serial port - then it stops until you send g back across.

In that case - you set a global variable to track mode and if mode is set to "go mode" then send 'Go' .. then check serial port to see if you got any new commands. If you got an s .. then set mode to "not go mode".

If none of that is what you need, provide more details and maybe a scenario.

Hope that helps.