NeoPixel 12 Bits Ring Counter

Hello!
I try to make a counter up to 60 using a neopixel led ring which should actually represent seconds. I divided the counter into 5 stages (5 * 12 = 60) each with a different color of the LEDs on. This program crashes at stage 1, meaning it counts to 12 and stays that way. How should I write this program for counting to continue until 60?

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      12

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second
int s,i,j,k,l,m;
void setup() {
  Serial.begin(9600);
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  for(s = 0; s < 60; s++){
    
    if(s < 12){
    for(i = 0; i < NUMPIXELS; i++){
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(150,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
        }// 1st For
        if(i==NUMPIXELS){
          pixels.clear();
          }   
    }// 1st If

    else if((s > 11) && (s < 24)){  
    for(j = 0; j < NUMPIXELS; j++){
    pixels.setPixelColor(j, pixels.Color(0,150,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).           
        }// 2st For
        if(j==NUMPIXELS){
          pixels.clear();
          }     
    }// 2st If    

    else if((s > 23) && (s < 36)){
    for(k = 0; k < NUMPIXELS; k++){
    pixels.setPixelColor(k, pixels.Color(0,0,150)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).           
        }// 3st For
        if(k==NUMPIXELS){
          pixels.clear();
          }     
    }// 3st If

    else if((s > 35) && (s < 48)){    
    for(l = 0; l < NUMPIXELS; l++){
    pixels.setPixelColor(l, pixels.Color(150,150,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).        
        }// 4st For  
        if(l==NUMPIXELS){
          pixels.clear();
          }     
    }// 4st If    
        
    else if((s > 47) && (s < 60)){
    for(m = 0; m < NUMPIXELS; m++){
    pixels.setPixelColor(m, pixels.Color(0,150,150)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).            
        }// 5st For  
        if(m==NUMPIXELS){
          pixels.clear();
          }
    }// 5st For 

    if(s == 60){
    pixels.setPixelColor(m, pixels.Color(150,150,150)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware. 
    delay(1000); 
    }// 6st If
 
  }// BIG For
    
}// loop
  for (int s = 0; s < 60; s++) {
    Serial.println(s);
  }// For

The only thing your initial loop does its is print s. The following compares never see s changing since they are outside the for() loop.

You told the compiler you only have 12 neopixels

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      12

You have several globals that appear to be confused with locals. e.g. m. You use it as a loop counter and then do this:

    if (m == NUMPIXELS)

At that point in the code, m is and always will be, zero.

wildbill:
You have several globals that appear to be confused with locals. e.g. m. You use it as a loop counter and then do this:

To be more specific, you declare it in a for() statement as well as globally.

int s,i,j,k,l,m;
...
    for(int m = 0; m < NUMPIXELS; m++){

Just drop the int declaration from the for:

    for(m = 0; m < NUMPIXELS; m++){

Please, don't use single letter variables! Use meaningful, readable names always! You started to do it here:

int delayval = 500; // delay for half a second

What happened then?

See above replies for where things go wrong.

One advise: do not use single letter global variables; finding the use of the variable i or s back in your code is a mission. See aargs reply

Your variables hold something, e.g. a counter; give them appropriate names, so you (and we) know what they are for and are easier to find

The letter 's' occurs 142 times, the letter 'i' 183 times

dougp:

  for (int s = 0; s < 60; s++) {

Serial.println(s);
 }// For




The only thing your initial loop does its is print s. The following compares never see s changing since they are outside the for() loop.

So, I should bring other if and for statements into this loop ?

aarg:
To be more specific, you declare it in a for() statement as well as globally.

int s,i,j,k,l,m;

...
   for(int m = 0; m < NUMPIXELS; m++){




Just drop the int declaration from the for:


for(m = 0; m < NUMPIXELS; m++){




Please, don't use single letter variables! Use meaningful, readable names always! You started to do it here:


int delayval = 500; // delay for half a second



What happened then?

I missed that I already declared the variables as global.

david_2018:
You told the compiler you only have 12 neopixels

// How many NeoPixels are attached to the Arduino?

#define NUMPIXELS      12

That's because I have 1 piece 12 bits led ring. The goal is to represent the seconds in 5 stages in different colors using this led ring. Do you have any suggestions?

Now it makes sense. Something like

Neopixel 1
0 - RED
1 - GREEN
2 - BLUE
3 - YELLOW
4 - VIOLET

Neopixel 2
5 - RED
6 - GREEN
7 - BLUE
8 - YELLOW
9 - VIOLET

Neopixel 3
10 - RED
11 - GREEN
12 - BLUE
13 - YELLOW
14 - VIOLET

Neopixel 4
15 - RED
16 - GREEN
17 - BLUE
18 - YELLOW
19 - VIOLET

and so on...

What is it supposed to do?

I'll guess that you intended to use five different colors and to show one to twelve pixels in each as the seconds tick by.

One way to represent 60 seconds using only 12 RGB LEDs.

He could do it the other way around. Each set of 12 seconds lights up the whole ring

RED
0 to 11

GREEN
12 to 23

BLUE
24 to 35

YELLOW
36 to 47

VIOLET
48 to 59

wildbill:
What is it supposed to do?

I'll guess that you intended to use five different colors and to show one to twelve pixels in each as the seconds tick by.

Exactly like hzrnbgy says below.

RED
0 to 11

GREEN
12 to 23

BLUE
24 to 35

YELLOW
36 to 47

VIOLET
48 to 59

hzrnbgy:
One way to represent 60 seconds using only 12 RGB LEDs.

He could do it the other way around. Each set of 12 seconds lights up the whole ring

RED
0 to 11

GREEN
12 to 23

BLUE
24 to 35

YELLOW
36 to 47

VIOLET
48 to 59

You are absolutely right, that's exactly what I want to do. Using this led ring I will represent the seconds counted by a real time clock, but first I have to make a simple counter. How should I write this in the program I showed you?

So, i made some changes in the program, i need to find way to break the 1st for loop (counter 0-11) because the program stuck here. Also, every 60 seconds, the led ring will light white color.

cristian10001:
Hello!
I try to make a counter up to 60 using a neopixel led ring which should actually represent seconds. I divided the counter into 5 stages (5 * 12 = 60) each with a different color of the LEDs on. This program crashes at stage 1, meaning it counts to 12 and stays that way. How should I write this program for counting to continue until 60?

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson

// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef AVR
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      12

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 1000; // delay for half a second
int s,i,j,k,l,m;
void setup() {
  Serial.begin(9600);
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (AVR_ATtiny85)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

for(s = 0; s < 60; s++){
    Serial.println(s);
    for(s = 0; s < 12; s++){
    for(i = 0; i < NUMPIXELS; i++){
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(150,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
        }// 1st For
        if(i==NUMPIXELS){
          pixels.clear();
          break;
          } 
    }// 1st If

for(s = 11; s < 24; s++){ 
    for(j = 0; j < NUMPIXELS; j++){
    pixels.setPixelColor(j, pixels.Color(0,150,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).         
        }// 2st For
        if(j==NUMPIXELS){
          pixels.clear();
          break;
          }   
    }// 2st If

for(s = 23; s < 36; s++){
    for(k = 0; k < NUMPIXELS; k++){
    pixels.setPixelColor(k, pixels.Color(0,0,150)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).         
        }// 3st For
        if(k==NUMPIXELS){
          pixels.clear();
          break;
          }   
    }// 3st If

for(s = 35; s < 48; s++){   
    for(l = 0; l < NUMPIXELS; l++){
    pixels.setPixelColor(l, pixels.Color(150,150,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).       
        }// 4st For 
        if(l==NUMPIXELS){
          pixels.clear();
          break;
          }   
    }// 4st If   
       
    for(s = 47; s < 60; s++){
    for(m = 0; m < NUMPIXELS; m++){
    pixels.setPixelColor(m, pixels.Color(150,0,150)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).           
        }// 5st For 
        if(m==NUMPIXELS){
          pixels.clear();
          break;
          }
    }// 5st For

if((s > 59) && (s < 61)){
    pixels.setPixelColor(m, pixels.Color(150,150,150)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(1000);
    }// 6st If

}// BIG For
   
}// loop

I made some changes based on your suggestions, now the program behaves a bit better, it works almost exactly the way I want it to. Do you think I can make it glow white when it reaches 60?
Since I will be using this program with an RTC, do you think I can write s = now.second () so that I can synchronize the counter with the seconds of the clock?