Unexplainable int behavior in encoder/LED project I am trying to use a rotary encoder to cycle through the brightest versions of R, G &amp; B. And it works! Red goes to blue just fine. Then G happens. For some reason, the int for green goes completely haywire. And it does this the same way on two Arduinos. 

So my question is this: why would two, identically assigned variables treat ++ completely differently? After hours (and hours and hours) of looking, I'm completely out of possible answers. 

#include &lt;Encoder.h&gt;
#include &lt;FastLED.h&gt;

/* Encoder */
Encoder myEnc(2, 3);
const int swPin = 4 ;
long oldPosition  = -999;

/* LEDs */
#define LED_PIN     7
#define NUM_LEDS    150
CRGB leds[NUM_LEDS];

/* RGB Loop */
volatile int cycle = 1;
volatile int brightness = 0;
volatile int r = 127;
volatile int g = 0;
volatile int b = 0;

void setup() {
  pinMode(swPin, INPUT);
  Serial.begin(9600);
  FastLED.addLeds&lt;WS2812, LED_PIN, GRB&gt;(leds, NUM_LEDS);
}

void loop() {
  // Read from rotary encoder.
  long newPosition = myEnc.read();

  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition); /* Sends elsewhere */
  }

  /* Will eventually move this inside the above if 
   * statement so the color shift is controlled by
   * a rotary encoder.  Down here for debugging. 
   */
  delay(50);
  updateColor( );
}

void updateColor(  ) {

  Serial.println(" "); // For readability. 

  if ( brightness &gt;= 127 ) {
    brightness = 1;
    if ( cycle &gt; 6 ) {
      cycle = 1;
    } else {
      cycle++;
    }
  } else {
    brightness++;
  }

  if ( cycle == 1) {
    b++;
  }
  else if ( cycle == 2) { /* rb --&gt; b */
    r--;
  }
  else if ( cycle == 3) { /* b --&gt; bg */
    g++;
  }
  else if ( cycle == 4) { /* bg --&gt; g */
    b--;
  }
  else if ( cycle == 5) { /* g --&gt; rg */
    r++;
  }
  else if ( cycle == 6) { /* g --&gt; rg */
    g--;
  }
  else {
    Serial.print("==================== ");
  }

  /* Debugging */
  Serial.print("brightness:");
  Serial.print(brightness);
  Serial.print("   cycle:");
  Serial.print(cycle);
  Serial.print("   r:");
  Serial.print(r);
  Serial.print(" g:");
  Serial.print(g); // goes completely bonkers
  Serial.print(" b:");
  Serial.print(b);
  Serial.println(" ");


  for (int i = 0; i &lt;= NUM_LEDS; i++) {
    leds[i].setRGB( r, g, b );
  }

  FastLED.show();

}


spits out the following in the serial monitor : 

brightness:1   cycle:1   r:127 g:0 b:1 

brightness:2   cycle:1   r:127 g:127 b:2 

brightness:3   cycle:1   r:127 g:32639 b:3 

brightness:4   cycle:1   r:127 g:32639 b:4 

brightness:5   cycle:1   r:127 g:32639 b:5 

brightness:6   cycle:1   r:127 g:32639 b:6 



Red and blue are doing exactly what I want. Green is off on Mars.

Any/all help would be enormously appreciated.  Thanks in advance.
Here:

CRGB leds[NUM_LEDS];


you are defining an array of NUM_LEDS objects of the CRGB type.
Valid indices for this array range from 0 to NUM_LEDS-1.

Here:

for (int i = 0; i &lt;= NUM_LEDS; i++) {
  leds[i].setRGB( r, g, b );
}


you are accessing the array elements at indices 0 to NUM_LEDS. The
last iteration is an out-of-bounds access, which is causing memory
corruption. You should instead write

for (int i = 0; i &lt; NUM_LEDS; i++) ...