for loop within a void loop??

No idea why I can't get this to work. I have placed and replaced the for loop a various places without success. I am just trying to get the led on/off action to execute 5 times before stopping. What am I doing wrong? Thanks for any help. Is a for loop not allowed within a void loop? I started with the BlinkWithoutDelay sketch.

void loop() {
  for (int i = 0; i <=5; i++) {
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis >= interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;
      // if the LED is off turn it on and vice-versa:


      if (ledStateAlarm == LOW) {
        ledStateAlarm = HIGH;
      }
      else {
        ledStateAlarm = LOW;
        ledStateLight = HIGH;
      }

      // set the LED with the ledState of the variable:
      digitalWrite(AlarmPin, ledStateAlarm);
      digitalWrite(LightPin, ledStateLight);
    }
  }
}

You should probably create a variable for counting the on/off conditions of the LED.

That for loop executes as fast as can be (VERY fast).
While it may be transitioning, it will be so fast you will not be able to discern the transitions.

Is a for loop not allowed within a void loop?

A for loop is perfectly valid within the loop() function.

Work through what the for loop is doing, by hand. You'll quickly see that a for loop is unnecessary.

What you want is a counter, incremented every time the LED is toggled on. When the counter is less than 5, it matters what time it is. When the counter is 5, it doesn't.

byte blinkCount = 0;
byte ledState = LOW;

void loop()
{
   if(blinkCount < 5)
   {
      // see if it is time to toggle the led
      // if it is
      {
         ledState = !ledState;
         digitalWrite(ledPin, ledState);
         if(ledState == HIGH)
            blinkCount++;
      }
   }

   // Do whatever else is needed
}

Kinda hard to determine the problem, since the definitions of some of the variables is missing and we have no idea how long interval is. Post the entire program.

As I mentioned in the post, it is essentially the BlinkWithouDelay given in the Arduino Examples. All I did was changed some pin names and added a couple of others.

Still, wouldn't it have been easier on us if you had listed the entire program, rather than requiring us to load the IDE and load the program? Are you confident enough in your "All I did was change" code to think it has no errors? If that were the case, you probably wouldn't be posting here. Also, some of us read these on a Smartphone, which means you're cutting off some possible responses by posting a snippet.

Here is the entire sketch:

#include <SoftwareSerial.h>

/* Blink without Delay
  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
// constants won't change. Used here to set a pin number :
const int AlarmPin =  3;      // the number of the LED pin
const int LightPin =  6;

// Variables will change :
int ledStateAlarm = LOW;             // ledState used to set the LED
int ledStateLight = LOW;

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(AlarmPin, OUTPUT);
  pinMode(LightPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i <=5; i++) {
    // here is where you'd put code that needs to be running all the time.

    // check to see if it's time to blink the LED; that is, if the
    // difference between the current time and last time you blinked
    // the LED is bigger than the interval at which you want to
    // blink the LED.
    unsigned long currentMillis = millis();

    if (currentMillis - previousMillis >= interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;
      // if the LED is off turn it on and vice-versa:


      if (ledStateAlarm == LOW) {
        ledStateAlarm = HIGH;
      }
      else {
        ledStateAlarm = LOW;
        ledStateLight = HIGH;
      }

      // set the LED with the ledState of the variable:
      digitalWrite(AlarmPin, ledStateAlarm);
      digitalWrite(LightPin, ledStateLight);
    }
  }
}

This piggy backs on what PaulS said, but you don't seem to get that he function loop() IS a loop.

What you do is enter loop() under initial conditions, then you run your "for loop" 5 times, then you go back to the beginning of "loop()" and run your "for loop" 5 times, then you go back to the beginning of "loop()" and run your "for loop" 5 times, then. . . .

it's not like "main()" in C. It's like you're in a "while(TRUE)" loop.

All you want to do is count the number of times it turns the light on and off and then do whatever. Like PaulS said.

nkuck:
Here is the entire sketch:

I don't see how that is supposed to blink the LED only 5 times. Once the for loop has executed, it may, or may not, have blinked the LED once. Then, loop() will run again, and the for loop might actually toggle the state of an LED again. But it won't be the next iteration of loop() that does that.

The LED will not quit blinking after 5 times. The for loop gets started over and over.

TheCityGame, I do understand that thr function loop() is a loop, and I do appreciate your statement:
"What you do is enter loop() under initial conditions, then you run your "for loop" 5 times, then you go back to the beginning of "loop()" and run your "for loop" 5 times, then you go back to the beginning of "loop()" and run your "for loop" 5 times, then.... "
but that is what I don't know how to do.
And Paul, if I was able to get it working the way I wanted it to do, I wouldn't be here.
What would be great if someone could actually show me the correct location of the for loop.
Thanks.

Try this on line 23:

 }while(1);

Thanks, but I am not sure that your line #23 is the same as my #23. Could you be more specific, and where the subsequent, additional curly brace would go, if there is one?

Your "for loop" is not executing 5 times because it executes 5 times and then it executes 5 more times and then it executes 5 more times and then it executes 5 more times and then it executes 5 more times and then it executes 5 more times and then it executes 5 more times and then it executes 5 more times. . .

Why do you think it would ever stop?

How do you ever make it stop?

Exactly my initial question, how should I write the code to execute just a preset number of times and then stop? I am a new to writing code.

how should I write the code to execute just a preset number of times and then stop?

One way would be to put the for loop in the setup() function and have nothing in the loop() function.

This is PaulS's code which turns off the light after 5 times. You might need a >= instead of a >. I'm not sure.

byte blinkCount = 0;
byte ledState = LOW;

void loop()
{
   if(blinkCount < 5)
   {
      // see if it is time to toggle the led
      // if it is
      {
         ledState = !ledState;
         digitalWrite(ledPin, ledState);
         if(ledState == HIGH)
            blinkCount++;
      }
   }

  if (blinkCount > 5) digitalWrite(ledPin, LOW);
}

You told me that you realize that loop() is a loop, but you don't understand why the light doesn't blink 5 times and then stop. The reason is that loop() is a loop. You exit your "for loop". You never exit loop(). You can't exit loop(). All you can do is turn off power to the LED.

In short, when you're in loop(), you want to count the number of blinks. When it gets to 5, turn it off and leave it off.

Got it, and thanks for the time given to explain.

Also, UKHeliBob has another solution.

If you put the for loop in setup(), it will run 5 times, and then leave. That's because setup() doesn't loop. It runs once and then moves to loop.

If you put nothing in loop(), then it will run setup once (and your for loop 5 times) and then exit setup(), and then go to loop() and then do nothing because you have nothing in there.

Well actually it repeatedly do nothing, because its being called from an infinite loop!

Most sketches, most of the time, consist of loop doing nothing (but after first checking
whether there's anything to do).

void loop ()
{
  if ( ... test1 ...)
    do_stuff_1 () ;
  if ( ... test2 ...)
    do_stuff_2 () ;
  if ( ... test3 ...)
    do_stuff_3 () ;
  ....
}

Its an event handler. If your loop() doesn't look like this, try to make it do so. With appropriately
named functions of course. If the test isn't simple, make the test a function returning bool.

Thanks to everyone who had responded. I have things working pretty well now after implimenting the suggestions.