NeoPixels - Nested For Loops

Hi there,

I'm trying to make a creative countup timer with 1 LED equalling 1 minute and some intermittent effects to signify longer periods of time being passed like 5 minutes.

At the moment doing it on a smaller time scale and trying to create the following behaviours on a 60 LED neopixel strip:

  1. Start with no lights, add 1 green light every delay of 500 and a buzzer noise (this I have managed to do)
  2. For every green light, the next light along is flashing red, to indicate it is next (I have managed this with the following code, but without the flashing part:
#include <Adafruit_NeoPixel.h>
int LEDPIN = 6;
const int buzzer = 2;
int NUMPIXELS = 60;
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LEDPIN, NEO_GRB + NEO_KHZ800);
int delayval = 500; // delay for half a second
int delayblinkval = 100;
int i = 0;

void setup() {
  pixels.clear();
  pixels.begin(); // This initializes the NeoPixel library.
  pinMode(buzzer, OUTPUT);
}

void loop() {
  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  for(int i=0;i<60;i++){
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,150,0));
    pixels.setPixelColor(i+1, pixels.Color(150,0,0));
    tone(buzzer,1000);
    delay(50);
    tone(buzzer,500);
    delay(50);
    tone(buzzer,1500);
    noTone(buzzer);
    pixels.show();
    delay(delayval);
  }
  pixels.clear();
}

Then:
3. Every 5 lights, play a separate buzzer sound and flash all lights to indicate that 5 lights have passed.

I've tried to do it with a nested "for" loop but it's conflicting somewhere and not showing any lights.

#include <Adafruit_NeoPixel.h>
int LEDPIN = 6;
const int buzzer = 2;
int NUMPIXELS = 60;
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LEDPIN, NEO_GRB + NEO_KHZ800);
int delayval = 500; // delay for half a second
int delayblinkval = 100;
int i = 0;

void setup() {
  pixels.clear();
  pixels.begin(); // This initializes the NeoPixel library.
  pinMode(buzzer, OUTPUT);
}

void loop() {
  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  for(int i=0;i<60;i++) {
   pixels.setPixelColor(i, pixels.Color(0,150,0));
    pixels.setPixelColor(i+1, pixels.Color(150,0,0));
    tone(buzzer,1000);
    delay(50);
    tone(buzzer,500);
    delay(50);
    tone(buzzer,1500);
    noTone(buzzer);
    pixels.show();
    delay(delayval);  
    for(int k=i;k<60;k+5){
    tone(buzzer,1000);
    delay(500);
    tone(buzzer,500);
    delay(500);
    tone(buzzer,1500);
    noTone(buzzer);
  }}
  pixels.clear();
}

Any help very much appreciated.
Ben

k+5

should be

k+=5

Otherwise, k never changes and your for-loop goes on forever.

But I don't see how this for-loop can achieve your goal #3. I suspect you don't want another for-loop, just an if statement.

EDIT: oh, there is a way to do it using another for-loop.

Your outer for-loop would make k go from 0 to 55 in steps of 5. Your inner for-loop would make i go from k to k+4. That way, i goes through all values from 0 to 59 as it does now, but you can put the code for your extra sounds in the outer loop, after the inner loop has finished.

for (int k=0; k<60; k+=5) {
  for (int i=k; i<k+5; i++) {
    //Do stuff for each second
    }
  //Do stuff for each 5-second
}

OK great thanks, I will give that a go!

Edit: worked perfectly, many thanks!

Ben