Ending a For Loop(s)

Hi all,

Using a 60 neopixel strip I'm trying to break the for loop so it only runs the two for loops once each.

The behaviour at the moment is turning on every LED 1 by 1 up until the 60th pixel then it comes down 1 by 1 in a different colour.

By the nature of the for loop it just carries on but possibly with "break" function I can stop it, so it does one sequence turning all the lights on and one sequence turning them all a different colour on the way back down 1 by 1.

I understand there needs to be an IF statement but I have tried several combinations and have been unsuccessful so far.

Here is the code:

#include <Adafruit_NeoPixel.h>
int LEDPIN = 7;
int NUMPIXELS = 60;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, LEDPIN, NEO_GRB + NEO_KHZ800);
int i = 0;

void setup() {
  strip.begin(); // This initializes the NeoPixel library.
  strip.show();
}
  void loop() {
   for(int i;i<60;i++) {
    strip.setPixelColor(i,150,0,0);
    strip.show();
    delay(10);
    }
   for(int i=60; i >= 0;i--){
   strip.setPixelColor(i,0,0,120);
   strip.show();
   delay(10);
   }}}

Any help is massively appreciated, many thanks!
Ben

We cannot see your attempt at the if( ) ?

Do not use a for( ) loop, do not use delay( ).

Use a millis( ) TIMER for all delays. This allows you to do other things at the same time as animations.

Read this discussion:

If you want the content of loop() to only be executed once, you can put it in setup().

That's a great idea which I wouldnt have never thought of even though it's kind of right in front of me.

For now that works but the plan was eventually to have something like:

  1. 4 x RFID tags placed in correct locations
  2. Neopixel lights count all the way up and all the way back down again
  3. Mag lock unlocks (yes I work in an escape room :stuck_out_tongue: )

To break the loop I tried something like this:

void loop() {
   for(int i;i<60;i++) {
if (i<59) {
break;
    strip.setPixelColor(i,150,0,0);
    strip.show();
    delay(10);
    }
   for(int i=60; i >= 0;i--){
   strip.setPixelColor(i,0,0,120);
   strip.show();
   delay(10);
   }}}

But that didn't work.
Many thanks for any help
Ben

Oops

That bit is wrong yes but I copied the code wrong. It’s the placement and structure of the break; I could do with a massive hand on

Thanks!

@benjam2k4
You are on the wrong track trying to break a for loop. It you need a for loop at all you need to know before the start how many times it is to go round, and set the initial variables as appropriate.

If you really do want to get out early then just change the value of i within the loop so it exceeds the limit.

You need to learn how to write multitasking, non-blocking code. Start with blink without delay in the IDE examples, then have a look at these tutorials:

Using millis for timing
Demonstration for several things at the same time
Finite state machine tutorial

Also learn to break your code into functions, each function should do one well defined thing then return to loop. The only code in loop should be function calls.

This for loop will abort immediately, was this your intention?

Fixed now.

well the loop will run only if i is 59 and what is the chance of that?

Depends on sizeof(int)

True, but still pretty slim

@benjam2k4 Just to add a little to the discussion, to keep things moving fast, you not only have break(), but also continue().

break() has been explained, and it's cousin continue() immediately bails on the current iteration, and starts a new one.

reference

...neither of which is a function, so not requiring parentheses.