Application does not work when trying to access X NeoPixel S

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Samaranth
 
Posts: 2
Joined: Thu May 30, 2019 5:46 am

Application does not work when trying to access X NeoPixel S

Post by Samaranth »

Please see sidenote at the end!!!

I'm currently working on an 14x14 interactive LED table for an university assignment. Therefore I use 14 SK6812 strips each being 14 NeoPixels long. Each strip is run on a separate pin of an Arduino Mega 2560. To get the information which LEDs should be set to which color I pass an int[14][14][3] JSON object as a string, deserialize it and get the RGB values from it to set the color. This works fine as long as I only control 7 LED strips in my code. However if there are more than 7 strips accessed at any point in the code it appears that the JSON object can't be read (at least the arduino prints only 0 to the console when reading from the deserialized object).

There is no problem with serial communication as I can perfectly read and process the JSON object when not using more than 7 strips. My code at the moment looks like this:

Code: Select all

#include <ArduinoJson.h>
#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif

#define PINROW0 2
#define PINROW1 3
#define PINROW2 4
#define PINROW3 5
#define PINROW4 6
#define PINROW5 7
#define PINROW6 8
#define PINROW7 22
#define PINROW8 24
#define PINROW9 26
#define PINROW10 28
#define PINROW11 30
#define PINROW12 32
#define PINROW13 34

#define NUMPIXELS 14 //Anzahl der Pixel pro Reihe

/*Adafruit_NeoPixel row0 = Adafruit_NeoPixel(NUMPIXELS, PINROW0, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row1 = Adafruit_NeoPixel(NUMPIXELS, PINROW1, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row2 = Adafruit_NeoPixel(NUMPIXELS, PINROW2, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row3 = Adafruit_NeoPixel(NUMPIXELS, PINROW3, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row4 = Adafruit_NeoPixel(NUMPIXELS, PINROW4, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row5 = Adafruit_NeoPixel(NUMPIXELS, PINROW5, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row6 = Adafruit_NeoPixel(NUMPIXELS, PINROW6, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row7 = Adafruit_NeoPixel(NUMPIXELS, PINROW7, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row8 = Adafruit_NeoPixel(NUMPIXELS, PINROW8, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row9 = Adafruit_NeoPixel(NUMPIXELS, PINROW9, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row10 = Adafruit_NeoPixel(NUMPIXELS, PINROW10, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row11 = Adafruit_NeoPixel(NUMPIXELS, PINROW11, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row12 = Adafruit_NeoPixel(NUMPIXELS, PINROW12, NEO_GRB + NEO_KHZ800);
  Adafruit_NeoPixel row13 = Adafruit_NeoPixel(NUMPIXELS, PINROW13, NEO_GRB + NEO_KHZ800);
*/

//Adafruit_NeoPixel currentStrip = Adafruit_NeoPixel(NUMPIXELS, pins[0], NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel row[] = { //Initialisieren des Arrays, das die addressierbaren LED Streifen im Adafruit Format enthält
  Adafruit_NeoPixel(NUMPIXELS, PINROW0, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW1, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW2, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW3, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW4, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW5, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW6, NEO_GRB + NEO_KHZ800)/*,
  Adafruit_NeoPixel(NUMPIXELS, PINROW7, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW8, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW9, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW10, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW11, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW12, NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(NUMPIXELS, PINROW13, NEO_GRB + NEO_KHZ800)*/
};


#define DELAY 1000 //Refresh Zyklus auf 10 Millisekunden setzen
#define NUMSTRIPS 7/*(sizeof(row)/sizeof(row[0]))*/ //Anzahl der verbundenen LED Streifen definieren


int values[14][14][3];
String matrixAsString = "";

void setup() {

#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif

  /*Seriellen Port über den der Pi sich mit dem Arduino verbindet einrichten*/
  Serial.begin(115200); //setzen der Bitrate auf 115200 Bit pro Sekunde
  Serial.setTimeout(100000);

  /*NeoPixel Library initialisieren*/
  for (int i = 0; i < NUMSTRIPS; i++) {
    row[i].begin();
    row[i].show();
  }
}

void process(String matrixAsString) {
  DynamicJsonDocument doc(4372);
  Serial.println(matrixAsString);
  deserializeJson(doc, matrixAsString);

  Serial.println((int)(doc[2][10][0]));
  Serial.println((int)(doc[2][10][0]));
  Serial.println((int)(doc[5][10][0]));
  Serial.println((int)(doc[0][1][2]));
  Serial.println((int)(doc[0][0][1]));

  for (int i = 0; i < NUMSTRIPS; i++) {
    for (int j = 0; j < NUMPIXELS; j++) {
      for (int k = 0; k < 3; k++) {
        values[i][j][k] = (int)(doc[i][j][k]);
      }
    }
  }
}

void paint() {
  int r = 0;
  int g = 0;
  int b = 0;
  for (int i = 0; i < NUMSTRIPS; i++) {
    for (int j = 0; j < NUMPIXELS; j++) {
      r = values[i][j][0];
      g = values[i][j][1];
      b = values[i][j][2];
      row[i].setPixelColor(j, row[i].Color(r, g, b));
      row[i].show();
    }
  }
}

//infinite loop refreshing the matrix
void loop() {

  while (Serial.available()) {
    char c = Serial.read();
    matrixAsString += c;
    if (c == '\n') {
      process(matrixAsString);
      paint();
      matrixAsString = "";
    }

  }

}
As you can see in the code I tried different approaches to solving the problem. Neither did it work using Adafruit_NeoPixel array as is, nor did it work to split the array into two arrays containing 7 strips. Also declaring each strip individually didn't work as well as using only one declared strip and changing the pin it's connected to (using the setPin() function) each time I want to access a different strip.

The LEDs get their power by a separate PC power supply unit attached to the table so an inappropriate amount of power supply doesn't seem to be a problem here. Also trying to run the code on 2 Arduino Duemilanove and assigning 7 strips to each of them didn't solve the problem.

Sidenote:
The behaviour described above is based on the results I got from testing tonight. However after I just tested my code somehow I'm now only able to address 3 LED strips at once. If I try to use more than 3 strips now the application prints 0 to the console instead of the correct values.
For testing consider using this data which is a correct representation of an int[7][14][3]:
[[[0,0,0],[253,47,14],[247,255,158],[102,27,49],[174,28,214],[152,94,233],[88,191,33],[98,155,212],[236,248,0],[0,225,48],[130,88,246],[110,85,156],[240,100,157],[50,247,165]],[[58,173,117],[94,72,171],[88,58,115],[145,216,137],[86,6,132],[183,76,142],[161,10,135],[158,84,240],[250,29,67],[73,44,83],[9,131,139],[254,243,6],[10,104,226],[16,168,56]],[[32,181,156],[233,3,126],[7,47,165],[76,201,169],[19,86,14],[254,20,62],[17,217,245],[186,153,182],[137,134,242],[222,81,181],[206,36,102],[46,204,39],[9,51,142],[200,1,125]],[[22,118,77],[74,127,167],[118,232,100],[223,224,2],[189,173,115],[229,64,94],[38,194,34],[46,69,126],[109,220,207],[165,145,190],[54,171,139],[52,177,94],[168,172,195],[108,164,177]],[[36,237,198],[164,171,19],[156,127,173],[40,126,252],[131,99,227],[160,249,39],[117,100,94],[172,27,76],[111,204,150],[176,185,191],[170,83,126],[147,148,6],[231,214,27],[195,106,166]],[[69,79,30],[166,125,92],[52,131,50],[142,231,212],[118,109,33],[238,141,107],[22,140,165],[45,59,189],[73,49,251],[228,214,179],[168,159,210],[161,76,22],[255,99,143],[232,250,151]],[[92,251,102],[138,149,24],[29,122,180],[46,66,226],[188,114,201],[42,204,18],[197,43,53],[209,158,67],[146,114,102],[84,217,136],[135,135,208],[145,147,208],[64,14,151],[29,97,2]]]

Or this for an int[14][14][3]:
[[[0,0,0],[173,204,86],[4,136,15],[210,234,185],[86,9,54],[231,81,107],[102,28,127],[181,161,131],[75,190,182],[176,246,35],[12,178,20],[200,91,70],[208,96,182],[156,150,86]],[[212,236,18],[67,253,232],[37,49,223],[227,138,158],[30,29,243],[56,230,99],[15,111,103],[176,176,161],[13,20,232],[132,216,93],[212,212,208],[205,217,3],[42,63,83],[132,182,107]],[[108,116,229],[2,3,98],[233,132,104],[183,227,245],[23,32,226],[113,97,101],[47,34,97],[219,201,42],[5,63,252],[9,249,182],[203,178,90],[126,222,26],[140,134,6],[202,85,250]],[[142,119,65],[27,181,99],[116,81,226],[37,1,26],[25,88,188],[112,242,9],[225,228,163],[9,2,224],[127,180,84],[90,62,190],[16,82,239],[250,159,55],[245,70,141],[81,252,140]],[[60,38,22],[225,34,72],[74,90,158],[160,236,178],[89,54,12],[11,192,248],[180,195,62],[210,138,17],[38,224,3],[220,129,220],[74,70,18],[134,127,167],[8,77,16],[142,160,27]],[[127,71,211],[27,78,154],[166,30,142],[49,244,145],[26,189,67],[180,21,74],[244,21,110],[98,101,192],[235,1,26],[53,67,157],[20,26,118],[170,73,182],[18,154,251],[228,120,241]],[[140,42,110],[125,191,106],[165,29,105],[224,83,119],[92,46,196],[103,26,200],[82,41,192],[247,253,74],[145,195,66],[125,199,51],[98,36,102],[177,219,46],[100,153,25],[243,188,135]],[[140,28,15],[235,44,155],[214,83,18],[1,43,183],[133,170,72],[13,23,127],[136,216,206],[255,239,79],[109,84,23],[119,156,220],[178,35,233],[194,236,66],[251,197,69],[193,186,49]],[[95,240,243],[157,204,249],[78,172,86],[210,178,191],[124,121,252],[136,176,142],[50,134,253],[252,22,163],[91,19,15],[154,140,41],[70,105,162],[17,237,6],[214,172,31],[16,191,61]],[[240,236,220],[107,214,220],[165,220,180],[218,152,62],[191,95,42],[120,55,76],[140,196,93],[200,72,78],[11,131,75],[128,245,97],[70,11,111],[154,220,99],[202,71,49],[164,64,201]],[[19,11,70],[95,125,78],[181,255,229],[42,107,80],[225,59,98],[233,53,76],[81,183,2],[59,117,169],[94,22,190],[85,128,144],[44,4,31],[180,176,34],[86,200,52],[84,120,146]],[[43,126,132],[244,84,69],[9,7,243],[10,102,149],[109,84,119],[68,160,76],[57,142,178],[156,106,136],[55,120,232],[103,188,135],[60,13,16],[112,69,168],[89,147,232],[125,35,131]],[[131,32,3],[84,68,20],[120,223,143],[66,35,29],[28,174,120],[7,212,251],[167,90,253],[161,221,150],[148,196,229],[14,227,89],[212,31,219],[191,160,42],[210,190,152],[75,109,138]],[[22,235,71],[221,130,169],[53,98,215],[245,138,147],[172,227,198],[197,76,30],[64,136,166],[36,31,172],[140,95,30],[117,245,78],[5,32,11],[61,121,19],[243,13,240],[72,28,77]]]
Thanks in advance for your time and your help. I really appreciate any possible tips.

User avatar
adafruit_support_bill
 
Posts: 88099
Joined: Sat Feb 07, 2009 10:11 am

Re: Application does not work when trying to access X NeoPix

Post by adafruit_support_bill »

I suspect a memory issue. I don't have any experience with the JSON lib you are using. But string processing in general tends to be quite memory intensive. And with limited SRAM on the Mega, the heap is easily fragmented. https://learn.adafruit.com/memories-of- ... ot-dot-dot

One obvious optimization is for your array:

Code: Select all

int values[14][14][3];
That currently occupies 1176 bytes (out of the 8K available). Since pixel values range from 0-255, You can cut that size in half by making it a uint_8 (byte) array instead of an int array:
https://learn.adafruit.com/memories-of- ... ables-9-11

User avatar
Samaranth
 
Posts: 2
Joined: Thu May 30, 2019 5:46 am

Re: Application does not work when trying to access X NeoPix

Post by Samaranth »

Thank you for your suggestion. It was totally right. After fiddling around for the past 4-5 hours I finally got it working.
The mainroblem was that by using a JSON document we probably got about 13-18k byte when deserializing it due to its wrapping components. The final solution was backing away completely from using a JSON document and sent the data as bytes as you suggested. However I needed to push them to an int value as otherwise due to the signed character of the byte I'd get -1 instead of 255.

After I implemented some appropriate reading logic it almost works correctly though there seems to still be points where data is being skipped or whatever. I should be able to figure that out for myself now. Thanks again for that awesome advice. I guess otherwise I would not have been able to solve this!!!

User avatar
adafruit_support_bill
 
Posts: 88099
Joined: Sat Feb 07, 2009 10:11 am

Re: Application does not work when trying to access X NeoPix

Post by adafruit_support_bill »

otherwise due to the signed character of the byte
'byte' is not signed. The 'char' data type is signed. 'byte', 'uint8_t' and 'unsigned char' are all unsigned 8-bit data types.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”