I am trying to use a rotary encoder to cycle through the brightest versions of R, G & 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 <Encoder.h>
#include <FastLED.h>
/* 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<WS2812, LED_PIN, GRB>(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 >= 127 ) {
brightness = 1;
if ( cycle > 6 ) {
cycle = 1;
} else {
cycle++;
}
} else {
brightness++;
}
if ( cycle == 1) {
b++;
}
else if ( cycle == 2) { /* rb --> b */
r--;
}
else if ( cycle == 3) { /* b --> bg */
g++;
}
else if ( cycle == 4) { /* bg --> g */
b--;
}
else if ( cycle == 5) { /* g --> rg */
r++;
}
else if ( cycle == 6) { /* g --> 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 <= 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.