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
@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:
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.