Adafruit Neopixel Color Issues

Christian Robinson's icon

Hi C74 Forum!
A friend and I are working on a sketch that sends midi data to Max via Ableton, and then Max converts that midi data to a table of RGB values (using notein and coll), which are then converted to ASCII, and sent via Serial to an Arduino, which changes the color of a set of neopixel strips, according to the RGB value assigned in max.

Unfortunately, I'm having an issue where every time I set a color change in max via a new message, it sometimes gets the color right, sometimes sets it to the completely wrong color, and sometimes turns it off - even if I sent a message of (160 160 160 )- which is our R G B for the closest we can get to "white" with our current power configuration - and repeatedly send the same message, it will cycle through blues, greens, offs, and whites and never repeat the same cycle.

Please let me know if you have encountered this before, and if you have any solutions! The basic goal is to get the color right for each song of the set, and if we can do that, will do full-on choreographed lighting movements for each chorus, strobes, etc. I've copied the max and arduino code here - thanks for your help!

MAX CODE:

{
    "boxes" : [         {
            "box" :             {
                "maxclass" : "message",
                "text" : "160 160 160",
                "numoutlets" : 1,
                "patching_rect" : [ 200.0, 267.0, 77.0, 22.0 ],
                "id" : "obj-9",
                "outlettype" : [ "" ],
                "style" : "",
                "numinlets" : 2
            }

        }
,         {
            "box" :             {
                "maxclass" : "toggle",
                "numoutlets" : 1,
                "patching_rect" : [ 47.5, 386.0, 24.0, 24.0 ],
                "id" : "obj-6",
                "parameter_enable" : 0,
                "outlettype" : [ "int" ],
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "metro 100",
                "numoutlets" : 1,
                "patching_rect" : [ 60.0, 449.0, 65.0, 22.0 ],
                "id" : "obj-5",
                "outlettype" : [ "bang" ],
                "style" : "",
                "numinlets" : 2
            }

        }
,         {
            "box" :             {
                "maxclass" : "toggle",
                "numoutlets" : 1,
                "patching_rect" : [ 414.5, 200.5, 24.0, 24.0 ],
                "id" : "obj-4",
                "parameter_enable" : 0,
                "outlettype" : [ "int" ],
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "message",
                "text" : "embed 1",
                "numoutlets" : 1,
                "patching_rect" : [ 139.5, 95.0, 63.0, 23.0 ],
                "id" : "obj-1",
                "fontname" : "Arial",
                "outlettype" : [ "" ],
                "style" : "",
                "fontsize" : 13.0,
                "numinlets" : 2
            }

        }
,         {
            "box" :             {
                "maxclass" : "comment",
                "text" : "Double click the Coll object, copy and paste the text. Once this is done, click \"Embed 1,\" and it should save the data with the patch\n ",
                "linecount" : 6,
                "numoutlets" : 0,
                "patching_rect" : [ 485.5, 162.0, 182.0, 87.0 ],
                "id" : "obj-3",
                "style" : "",
                "fontface" : 1,
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "coll Setlist",
                "numoutlets" : 4,
                "patching_rect" : [ 252.0, 180.0, 65.0, 22.0 ],
                "id" : "obj-103",
                "outlettype" : [ "", "", "", "" ],
                "style" : "",
                "numinlets" : 1,
                "saved_object_attributes" :                 {
                    "embed" : 0
                }

            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "print To_Serial",
                "numoutlets" : 0,
                "patching_rect" : [ 74.0, 497.0, 87.0, 22.0 ],
                "id" : "obj-114",
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "serial usbmodem1411 9600",
                "numoutlets" : 2,
                "patching_rect" : [ 168.0, 497.0, 159.0, 22.0 ],
                "id" : "obj-115",
                "outlettype" : [ "int", "" ],
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "append 10",
                "numoutlets" : 1,
                "patching_rect" : [ 168.0, 455.5, 67.0, 22.0 ],
                "id" : "obj-116",
                "outlettype" : [ "" ],
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "atoi",
                "numoutlets" : 1,
                "patching_rect" : [ 195.0, 415.0, 40.0, 22.0 ],
                "id" : "obj-117",
                "outlettype" : [ "list" ],
                "style" : "",
                "numinlets" : 3
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "iter",
                "numoutlets" : 1,
                "patching_rect" : [ 195.0, 366.0, 27.0, 22.0 ],
                "id" : "obj-118",
                "outlettype" : [ "" ],
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "t l C",
                "numoutlets" : 2,
                "patching_rect" : [ 195.0, 318.0, 32.0, 22.0 ],
                "id" : "obj-119",
                "outlettype" : [ "", "C" ],
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "comment",
                "text" : "Manually adjust midi pitch for testing / table creation",
                "linecount" : 5,
                "numoutlets" : 0,
                "patching_rect" : [ 468.0, 86.0, 86.0, 74.0 ],
                "id" : "obj-98",
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "number",
                "numoutlets" : 2,
                "patching_rect" : [ 410.5, 85.0, 50.0, 22.0 ],
                "id" : "obj-99",
                "parameter_enable" : 0,
                "outlettype" : [ "", "bang" ],
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "number",
                "numoutlets" : 2,
                "patching_rect" : [ 410.5, 122.0, 50.0, 22.0 ],
                "id" : "obj-100",
                "parameter_enable" : 0,
                "outlettype" : [ "", "bang" ],
                "style" : "",
                "format" : 4,
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "print Pitch_Index",
                "numoutlets" : 0,
                "patching_rect" : [ 342.0, 175.0, 100.0, 22.0 ],
                "id" : "obj-101",
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "print RGB_Value",
                "numoutlets" : 0,
                "patching_rect" : [ 306.0, 243.0, 100.0, 22.0 ],
                "id" : "obj-102",
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "number",
                "numoutlets" : 2,
                "patching_rect" : [ 266.5, 133.0, 50.0, 22.0 ],
                "id" : "obj-104",
                "parameter_enable" : 0,
                "outlettype" : [ "", "bang" ],
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "notein",
                "numoutlets" : 3,
                "patching_rect" : [ 284.5, 96.0, 43.0, 22.0 ],
                "id" : "obj-105",
                "outlettype" : [ "int", "int", "int" ],
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "message",
                "text" : "port \"to Max 1\"",
                "numoutlets" : 1,
                "patching_rect" : [ 284.5, 59.0, 89.0, 22.0 ],
                "id" : "obj-106",
                "outlettype" : [ "" ],
                "style" : "",
                "numinlets" : 2
            }

        }
,         {
            "box" :             {
                "maxclass" : "newobj",
                "text" : "loadbang",
                "numoutlets" : 1,
                "patching_rect" : [ 284.5, 24.0, 60.0, 22.0 ],
                "id" : "obj-107",
                "outlettype" : [ "bang" ],
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "comment",
                "text" : "Sent to MEGA",
                "numoutlets" : 0,
                "patching_rect" : [ 349.0, 492.0, 167.0, 20.0 ],
                "id" : "obj-36",
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "comment",
                "text" : "?? Tells parse.Int when one character (instruction?) ends",
                "linecount" : 2,
                "numoutlets" : 0,
                "patching_rect" : [ 259.5, 449.0, 167.0, 33.0 ],
                "id" : "obj-32",
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "comment",
                "text" : "ASCI to integers for Serial message",
                "linecount" : 2,
                "numoutlets" : 0,
                "patching_rect" : [ 259.5, 409.5, 154.0, 33.0 ],
                "id" : "obj-31",
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "comment",
                "text" : "Break a list into individual messages",
                "linecount" : 2,
                "numoutlets" : 0,
                "patching_rect" : [ 245.5, 360.5, 150.0, 33.0 ],
                "id" : "obj-29",
                "style" : "",
                "numinlets" : 1
            }

        }
,         {
            "box" :             {
                "maxclass" : "comment",
                "text" : "Adds the \"C\" the arduino sketch is looking for",
                "linecount" : 2,
                "numoutlets" : 0,
                "patching_rect" : [ 237.0, 319.0, 150.0, 33.0 ],
                "id" : "obj-28",
                "style" : "",
                "numinlets" : 1
            }

        }
],
    "lines" : [         {
            "patchline" :             {
                "source" : [ "obj-103", 0 ],
                "destination" : [ "obj-102", 0 ],
                "hidden" : 0,
                "midpoints" : [ 261.5, 239.0, 315.5, 239.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-1", 0 ],
                "destination" : [ "obj-103", 0 ],
                "hidden" : 0,
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-103", 0 ],
                "destination" : [ "obj-9", 1 ],
                "hidden" : 0,
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-9", 0 ],
                "destination" : [ "obj-119", 0 ],
                "hidden" : 0,
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-106", 0 ],
                "destination" : [ "obj-105", 0 ],
                "hidden" : 0,
                "midpoints" : [ 294.0, 83.0, 294.0, 83.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-116", 0 ],
                "destination" : [ "obj-115", 0 ],
                "hidden" : 0,
                "midpoints" : [ 177.5, 478.0, 177.5, 478.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-99", 0 ],
                "destination" : [ "obj-105", 0 ],
                "hidden" : 0,
                "midpoints" : [ 420.0, 110.0, 338.5, 110.0, 338.5, 92.0, 294.0, 92.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-99", 0 ],
                "destination" : [ "obj-104", 0 ],
                "hidden" : 0,
                "midpoints" : [ 420.0, 110.0, 338.5, 110.0, 338.5, 128.0, 276.0, 128.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-99", 0 ],
                "destination" : [ "obj-100", 0 ],
                "hidden" : 0,
                "midpoints" : [ 420.0, 110.0, 420.0, 110.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-119", 0 ],
                "destination" : [ "obj-118", 0 ],
                "hidden" : 0,
                "midpoints" : [ 204.5, 343.0, 204.5, 343.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-119", 1 ],
                "destination" : [ "obj-118", 0 ],
                "hidden" : 0,
                "midpoints" : [ 217.5, 352.0, 204.5, 352.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-118", 0 ],
                "destination" : [ "obj-117", 0 ],
                "hidden" : 0,
                "midpoints" : [ 204.5, 391.0, 204.5, 391.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-117", 0 ],
                "destination" : [ "obj-116", 0 ],
                "hidden" : 0,
                "midpoints" : [ 204.5, 439.0, 180.0, 439.0, 180.0, 451.0, 177.5, 451.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-116", 0 ],
                "destination" : [ "obj-114", 0 ],
                "hidden" : 0,
                "midpoints" : [ 177.5, 478.0, 83.5, 478.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-107", 0 ],
                "destination" : [ "obj-106", 0 ],
                "hidden" : 0,
                "midpoints" : [ 294.0, 47.0, 294.0, 47.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-105", 0 ],
                "destination" : [ "obj-104", 0 ],
                "hidden" : 0,
                "midpoints" : [ 294.0, 119.0, 276.0, 119.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-105", 0 ],
                "destination" : [ "obj-100", 0 ],
                "hidden" : 0,
                "midpoints" : [ 294.0, 119.0, 420.0, 119.0 ],
                "disabled" : 0
            }

        }
,         {
            "patchline" :             {
                "source" : [ "obj-6", 0 ],
                "destination" : [ "obj-5", 0 ],
                "hidden" : 0,
                "disabled" : 0
            }

        }
],
    "appversion" :     {
        "major" : 7,
        "minor" : 3,
        "revision" : 1,
        "architecture" : "x86",
        "modernui" : 1
    }

}

====ARDUINO CODE====
/*
Simple BlinkyStrip control
This sketch allows serial control over a BlinkyStrip.
To set any pixel in the strip, send the following serial string:
Cr,g,b
r, g, and b are ASCII numbers for the red, green, and blue brightness
levels, from 0 to 255.
This will probably work for any string of NeoPixels from Adafruit as well.
Uses Adafruit's NeoPixel library: https://github.com/adafruit/Adafruit_NeoPixel
created 4 Dec 2014
by Tom Igoe

modified for Infinity Mirror Project
by August Luhrs -- 1 Nov 2017
*/
#include <Adafruit_NeoPixel.h>

#define PAIR1 8 //cat5 in pin8 (left)
#define PAIR2 32 //cat5 in pin 16 (center)
#define PAIR3 24 //cat5 in pin 24 (right)

Adafruit_NeoPixel strip = Adafruit_NeoPixel(171, PAIR1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(140, PAIR2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip3 = Adafruit_NeoPixel(171, PAIR3, NEO_GRB + NEO_KHZ800);

//const int numPixels = 1;
//const int numPixels = 240;

int pixel = 0; // pixel number
int red = 0; // red value
int green = 0; // green value
int blue = 0; // blue value

void setup() {
Serial.begin(9600); // initialize serial communication
Serial.setTimeout(100); // set serial timeout
strip.begin(); // initialize pixel strip
strip.show(); // Initialize all pixels to 'off'
strip2.begin();
strip2.show();
strip3.begin();
strip3.show();
}

void loop() {
// listen for serial:
if (Serial.available() > 0) {
if (Serial.read() == 'C') { // string should start with C
//pixel = Serial.parseInt(); // then an ASCII number for pixel number
red = Serial.parseInt(); // then an ASCII number for red
green = Serial.parseInt(); // then an ASCII number for green
blue = Serial.parseInt(); // then an ASCII number for blue
}
}
//for (red = 0; red <= 255; red++) {
/*for (int pixel = 0; pixel < numPixels; pixel++); {
strip.setPixelColor(pixel, red, green, blue, 0);// set the color for this pixel
}
strip.show(); // refresh the strip
*/

for (int i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, red, green, blue);
}
for (int i=0; i< strip2.numPixels(); i++) {
strip2.setPixelColor(i, red, green, blue);
}
for (int i=0; i< strip3.numPixels(); i++) {
strip3.setPixelColor(i, red, green, blue);
}
strip.show();
strip2.show();
strip3.show();

delay(10);
}

//}