Sketch not incrementing

Good day,

Hope someone can point out what I’m doing wrong. I am trying to build a led strip light that will cycle through the assigned colours, however in “void loop” my variable “colour” is not incrementing. This is part of a larger sketch, but need to get this part going.

#include <FastLED.h>

#define LED_PIN 7
#define NUM_LEDS 28

int colour = 0;
CRGB leds[NUM_LEDS];

void setup() {

FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

Serial.begin(9600);
}

void loop() {

switch(colour){
case 0: // White
LEDstring(255, 255, 255); break;
case 1: // Red
LEDstring(255, 0, 0); break;
case 2: // Orange
LEDstring(255, 100, 0); break;
case 3: // Yellow
LEDstring(255, 175, 0); break;
case 4: // Green
LEDstring(0, 255, 0); break;
case 5: // Blue
LEDstring(0, 0, 255); break;
case 6: //violet
LEDstring(255, 0, 175); break;
}

colour++;

if (colour > 6){
//colour = 0; //shut off for troubleshooting
}

delay(500);
}

void LEDstring(int r, int g, int b){

Serial.print(“colour: “);
Serial.print(colour);
Serial.print(”\t(”);
Serial.print(r);
Serial.print(", “);
Serial.print(g);
Serial.print(”, “);
Serial.print(b);
Serial.println(”)");

for (int x = 0; x <= NUM_LEDS; x++){
leds = CRGB(r, g, b);
}

FastLED.show();

}

The serial monitor shows the variable “colour” never increments.

colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)
colour: 0 (255, 255, 255)

Try

++colour

G

Thanks for answering, but I have tried ++colour; with no difference. I've even tried colour = colour + 1;

There’s your problem:

for (int x = 0; x <= NUM_LEDS; x++){
                  ^^

oqibidipo:
There’s your problem:

for (int x = 0; x <= NUM_LEDS; x++){

^^

Thank you, that worked. But why is “<=” affecting my count on another variable?

colour is immediately after leds[] in memory. Setting leds[NUM_LEDS] to white overwrites colour to 0xFFFF == -1 which is then incremented back to 0.

oqibidipo: colour is immediately after leds[] in memory. Setting leds[NUM_LEDS] to white overwrites colour to 0xFFFF == -1 which is then incremented back to 0.

I carefully chose a variable that was not a reserved word in c. It's not making sense to me. As far as I can see, the only way colour is changed is when it's updated itself. Why would leds[] affect the variable "colour", note it's not "color". I need this to make logical sense.

CRGB leds[NUM_LEDS];

Valid index values for the leds array are 0 to NUM_LEDS-1. leds[NUM_LEDS] is outside the array and this is where colours is located in memory.

Thank you for that explanation. I understand now. 8)