Help Neo Pixel problem

I have arduino Uno with neopixel used as a matrix. I'm using a BT module and android app to control the LEDs.

At boot, I have coded it to show RED,GREEN, BLUE in sequence.

#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>

SoftwareSerial SBT(2, 3);

//Pin where DIN is connected 
#define DATA_PIN 6

//Number of LEDs
#define COLUMNS 32
#define ROWS    8

uint32_t Wheel(byte WheelPos);
void ScrollRL(char* message,int delays);
void ScrollLR(char* message,int delays);


Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(COLUMNS, ROWS, DATA_PIN,
  NEO_MATRIX_TOP  + NEO_MATRIX_RIGHT +
  NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG +
  NEO_GRB         + NEO_KHZ800);

String strRec = "";
unsigned int colors[3];
char dataRec[255];

int rainbowDelay = 50;

char textString[255];
unsigned int textColors[3];
int textDelay = 50;
int dir = 1;

int pixelPerChar = 4;
int maxDisplacement;
int x = 0;

unsigned int dMode = 0;

int i, j, c;

char currentLine[255];
unsigned long rTime = 0;

void setup() {
  Serial.begin(9600);
  delay(100);
  Serial.println("Started!!!");
  SBT.begin(38400);
  delay(100);
  SBT.setTimeout(250);

  matrix.begin();
  delay(100);
  matrix.begin();
  matrix.setTextWrap(false);
  matrix.setBrightness(80);
  matrix.setTextColor(matrix.Color(255, 0, 0));

  Serial.println("Testing Matrix");
  Serial.println("RED");
  matrix.fillScreen(matrix.Color(255, 0, 0));
  matrix.show();
  delay(2000);
  Serial.println("GREEN");
  matrix.fillScreen(matrix.Color(0, 255, 0));
  matrix.show();
  delay(2000);
  Serial.println("BLUE");
  matrix.fillScreen(matrix.Color(0, 0, 255));
  matrix.show();
  delay(2000);

  Serial.println("Turning off lights");
  matrix.fillScreen(0);
  matrix.show();
  delay(100);

  Serial.println("All Started...");
}

void loop() {
  if(SBT.available() > 0){
    char c = SBT.read();
    Serial.println(c);
    delay(1);
    memset(currentLine, 0, sizeof(currentLine));
    if(c=='c'){
      for(int i=0; i<255; i++){
        delay(2);
        char ab = SBT.read();
        currentLine[i] = ab;
        if(ab == '\n') break;
      }
      char* pch = strtok(currentLine, ";");
      int b = 0;
      while(pch != NULL){
        colors[b] = atoi(pch);
        pch = strtok(NULL, ";");
        b++;
      }
      Serial.print(colors[0]);
      Serial.print(F("-"));
      Serial.print(colors[1]);
      Serial.print(F("-"));
      Serial.println(colors[2]);
      matrix.fillScreen(matrix.Color(colors[0], colors[1], colors[2]));
      matrix.show();
      Serial.println("Display a single color");
      dMode=1;  
    }else if(c=='r'){
      dMode=2;
      rTime = millis();
      for(int i=0; i<255; i++){
        delay(2);
        char ab = SBT.read();
        currentLine[i] = ab;
        if(ab == '\n') break;
      }
      matrix.fillScreen(0);
      matrix.show();
    }else if(c=='y'){
      for(int i=0; i<255;i++){
        delay(2);
        char ab = SBT.read();
        currentLine[i] = ab;
        if(ab == '\n') break;
      }
      rainbowDelay = atoi(currentLine);
      Serial.println(rainbowDelay);  
    }else if(c=='d'){
      for(int i=0; i<255;i++){
        delay(2);
        char ab = SBT.read();
        textString[i] = ab;
        if(ab == '\n') break;
      }
      Serial.println(textString);
    }
  }
  
  if(dMode==2){
    if(j < 255 && millis() - rTime > rainbowDelay){
      for(c = 0; c < ROWS; c++){
        for(i = 0; i < COLUMNS; i++) {
          matrix.drawPixel(i, c, Wheel((i*1+j) & 255));
        }
      }
      matrix.show();
      j++;
      rTime = millis();
    }else if(j == 255){
      j = 0;
    }  
  }
}

void ScrollLR(char* message,int delays) {
  maxDisplacement = strlen(message) * pixelPerChar;
  matrix.fillScreen(0);
  matrix.setCursor(x-maxDisplacement, 0);
  matrix.print(String(message));
  if(++x > (matrix.width() + maxDisplacement)) {
    x = 0;
  }
  matrix.show();
  delay(delays);
}

void ScrollRL(char* message,int delays) {
  maxDisplacement = strlen(message) * pixelPerChar + matrix.width();
  matrix.fillScreen(0);
  matrix.setCursor(x, 0);
  matrix.print(String(message));
  if(--x < -maxDisplacement) {
    x = matrix.width();
  }
  delay(delays);
  matrix.show();
}

uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
    return matrix.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } 
  else if(WheelPos < 170) {
    WheelPos -= 85;
    return matrix.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } 
  else {
    WheelPos -= 170;
    return matrix.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

This code compiles and works but not NeoPixel. It breaks after adding below piece of code (included in above code),

else if(c=='d'){
      for(int i=0; i<255;i++){
        delay(2);
        char ab = SBT.read();
        textString[i] = ab;
        if(ab == '\n') break;
      }
      Serial.println(textString);
    }

If I just removed it, everything works well. After adding that part, LED's doesn't light up at all, not even on 'setup()'

I confirmed that there's no data on data line by hooking it to an oscilloscope, happen just after adding that part. Tried another UNO, changed pins but no luck

Although I'm not familiar with the NeoMatrix, I suspect that you're running out of memory. Your code compiles to

Sketch uses 13846 bytes (45%) of program storage space. Maximum is 30720 bytes.
Global variables use 1215 bytes (59%) of dynamic memory, leaving 833 bytes for local variables. Maximum is 2048 bytes.

You will still have to add the the memory required for the NeoPixels which would be 768 bytes.

If possible, try it with 50% of the pixels and see if that improves the situation; it would be the indication that you're indeed running out of memory.

Another thing that might improve the situation would be to use the F() macro for all fixed text. E.g.

Serial.println(F("Started!!!"));

But if it will be enough is another question; it will save approx. 90 bytes of RAM.

1 Like

If the buffer runs dry before you reach the '\n' you will get a bunch of 255 characters in your buffer. You should use:
SBT.readBytesUntil('\n', buffer, sizeof buffer);

1 Like

You were right, I removed few variables and changed some to uint8_t, I could free up around 1100+ bytes and things are working again. Thank you for the help!

Thank you! I will look into it.