0

16x32 RGB LED Matrix Software Serial Data Corruption
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

16x32 RGB LED Matrix Software Serial Data Corruption

by BigCat on Sat Dec 02, 2017 1:44 am

I have a 16x32 RBG LED matrix connected to a Arduino Uno. The Uno is also connected to an Huzzah ESP3266 which sends data from the internet to the Uno using software serial. The data from the ESP8266 is used to adjust what is displayed on the matrix. After getting the software serial portion of the code working I ran in to a problem when I added the matrix. With the matrix running the data from software serial is corrupted and without the matrix running everything work fine. To stop the matrix from running I commented out the following line,
Code: Select all | TOGGLE FULL SIZE
matrix.begin();

Here is an example what the ESP is sending:
Code: Select all | TOGGLE FULL SIZE
<R,0,12,0,12,0,9,2,12>
<G,6,9,3,8,0,3,0,8>
<B,5,12,10,12,13,9,13,12>


With the matrix running the data from the ESP gets corrupted:
Code: Select all | TOGGLE FULL SIZE
Data Received:
²L0L22L6L22L6L24L2L24
589
Data Received:
FL2L20L2L:L2L24L0L24
589
Data Received:
BL20L22L22lÃ2lc2lÃ4lÃ2L24


With the matrix not running the data looks good.
Code: Select all | TOGGLE FULL SIZE
Data Received:
R,0,12,5,12,5,14,1,14
589
Data Received:
G,3,10,1,9,1,14,0,14
589
Data Received:
B,10,12,12,12,12,14,13,14

This is the Arduino shield where the matrix and ESP are connected.
IMG_20171201_204746317.jpg
IMG_20171201_204746317.jpg (442.97 KiB) Viewed 41 times


The ESP and Uno are connected via a TXB0104 Bi-Directional Level Shifter:
IMG_20171201_204746318.jpg
IMG_20171201_204746318.jpg (301.04 KiB) Viewed 42 times


Here is the Arduino code.
Code: Select all | TOGGLE FULL SIZE
#include <Arduino.h>
#include <Adafruit_GFX.h>   // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
#include <NeoSWSerial.h>
#include "TempLight.h"

//Stuff for LED Panel
#define CLK 8  // MUST be on PORTB! (Use pin 11 on Mega)
#define LAT A3
#define OE  9
#define A   A0
#define B   A1
#define C   A2



//Stuff for the Software Serial
NeoSWSerial espSerial(11,10);
//char espSerialChar;
//int readIdx;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;

uint8_t row_index = 0;
uint8_t col_index = 0;

uint8_t colorIndex = 0;
uint8_t colorInterval = 100;
uint8_t blockSize=8;
boolean newForecast = false;


unsigned long continueTime = 0;
unsigned long colorWriteTime = 0;
uint8_t s=0,t=0,stDiff=0;
RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);


int freeRam () {
  extern int __heap_start, *__brkval;
  int v;
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

void writeColor(){
   //r - red portion of color
   //g - green value of color
   //s - spacer used to reduce the size of the rectangle drawn
   //t - last value of spacer, used to draw black rectangles
   uint8_t rowStart = row_index*blockSize;
   uint8_t colStart = col_index*blockSize;
   uint8_t r= colorValueR[colorIndex];
   uint8_t g= colorValueG[colorIndex];
   uint8_t b= colorValueB[colorIndex];

      if (t<s){ // if the rectanges need to be decreased write back unfilled rectanged around them
         stDiff = s-t;
         for (uint8_t i=0;i<stDiff;i++){
            matrix.fillRect(colStart+i,rowStart+i,blockSize-i,blockSize-i,matrix.Color444(0,0,0));
         }

      }
      matrix.fillRect(colStart+(s/2),rowStart+(s/2),blockSize-s,blockSize-s,matrix.Color444(r,g,b));
      row_index+=1;
      colorIndex+=1;
      if (row_index==2){
         row_index =0;
         col_index +=1;
      }
      if (col_index==4){col_index=0;}
      if (colorIndex==8){
         colorIndex=0;
         newForecast=false;
      }

   }


void recveiveForecastData(){
   static boolean recvInProgress = false;
   static byte ndx = 0;
   char startMarker = '<';
   char endMarker = '>';
   char rc;

   while (espSerial.available() > 0 && newData == false) {
      rc = espSerial.read();
      if (recvInProgress == true) {
         if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
               ndx = numChars - 1;
            }
         }
         else {
            receivedChars[ndx] = '\0'; // terminate the string
            recvInProgress = false;
            ndx = 0;
            newData = true;
         }
      }

      else if (rc == startMarker) {
         recvInProgress = true;
      }
   }
}

void parseData() {      // split the data into its parts

    char * strtokIdx; // this is used by strtok() as an index
    Serial.println(freeRam());
    Serial.println(F("Data Received:"));
    Serial.println(tempChars);
    const char * dataType;
    strtokIdx = strtok(tempChars,",");      // get the first part - the string

    dataType = strtokIdx;
    if (!strcmp(dataType, "R")){
      for (uint8_t arrayIdx=0;arrayIdx<8;arrayIdx++){
         strtokIdx = strtok(NULL, ",");
         colorValueR[arrayIdx] = atoi(strtokIdx);
         }
    }
    else if (!strcmp(dataType, "G")){
      for (uint8_t arrayIdx=0;arrayIdx<8;arrayIdx++){
         strtokIdx = strtok(NULL, ",");
         colorValueG[arrayIdx] = atoi(strtokIdx);
         }
        }
    else if (!strcmp(dataType, "B")){
      for (uint8_t arrayIdx=0;arrayIdx<8;arrayIdx++){
         strtokIdx = strtok(NULL, ",");
         colorValueB[arrayIdx] = atoi(strtokIdx);
         newForecast=true;
         }
        }
}
void setup(){
   matrix.begin();
   espSerial.begin(9600);
   espSerial.listen();
   Serial.begin(115200);
}
void loop(){
   recveiveForecastData();
   if (newData == true) {
      strcpy(tempChars, receivedChars);
      parseData();
      newData = false;
   }
   if (newForecast==true){
      continueTime = millis();
      if ((continueTime-colorWriteTime) > colorInterval){
         writeColor();
         colorWriteTime=continueTime;
      }
   }
}


I have tested the matrix with the examples in the library and it works fine.
Any help would be appreciated.

BigCat
 
Posts: 1
Joined: Fri Dec 01, 2017 1:00 am

Please be positive and constructive with your questions and comments.