Using FastLED library with HC-05

Hello all , this is my first post here so please tell me if anything is wrong.
So i have an LED matrix of 16x16 leds connected with an arduino mega and an hc-05 bluetooth module.
I want to make an android application where i can make a pixel art and send it via bluetooth to the arduino and display it on the LED matrix.Right now , from my phone , i am trying to send something like "MODExZZZZ" , where x is a number for the mode i want to run(right now there exist a mode for saving data and one for displaying it) and ZZZZ being 256 characters where (R-red , G-green and B-blue) , 1 for each LED.
Of what i see from writing some information in the serial terminal the mode of sending data works and saves the data in a buffer but when i try to display it on the matrix something breaks and my bluetooth connection disspears. Of what i read before posting here "FastLED.show()" disables all interrupts and this messes up my bluetooth connection.
I do not know how to fix it as i have tried even doing "FastLED.show()" only where "Serial1.available()" is 0 and even tried to do "FastLED.show()" after each LED color is choosen as i read it takes more time when you update more LEDs.

I will attach the code down below. Please help

#include <FastLED.h>

#define MATRIX_PIN  8
#define MatrixWidth 16
#define MatrixHeight 16

#define NUM_LEDS (MatrixWidth * MatrixHeight)

#define BRIGHTNESS 64

CRGB leds[NUM_LEDS];

#define MAX_MESSAGE_LENGTH 256

char Matrix[16][16] = {
   'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' ,
   'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' ,
   'B' , 'B' , 'B' , 'B' , 'B' , 'R' , 'B' , 'B' , 'B' , 'R' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' ,
   'G' , 'G' , 'G' , 'G' , 'R' , 'R' , 'R' , 'G' , 'R' , 'R' , 'R' , 'G' , 'G' , 'G' , 'G' , 'G' ,
   'B' , 'B' , 'B' , 'R' , 'R' , 'R' , 'R' , 'R' , 'R' , 'R' , 'R' , 'R' , 'B' , 'B' , 'B' , 'B' ,
   'G' , 'G' , 'G' , 'R' , 'R' , 'R' , 'R' , 'R' , 'R' , 'R' , 'R' , 'R' , 'G' , 'G' , 'G' , 'G' ,
   'B' , 'B' , 'B' , 'B' , 'R' , 'R' , 'R' , 'R' , 'R' , 'R' , 'R' , 'B' , 'B' , 'B' , 'B' , 'G' ,
   'G' , 'G' , 'G' , 'G' , 'G' , 'R' , 'R' , 'R' , 'R' , 'R' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' ,
   'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'R' , 'R' , 'R' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' ,
   'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'R' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' ,
   'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' ,
   'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' ,
   'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' ,
   'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' ,
   'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' , 'B' ,
   'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' , 'G' 
 };

String string = " ";
int state = '1';
bool isPainted = false;

const int numChars = 257;
char receivedChars[258];   // an array to store the received data

String messageBuffer="";
String message="";

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  Serial.begin(19200);
  Serial1.begin(9600); // Default communication rate of the Bluetooth module

  FastLED.addLeds<NEOPIXEL, MATRIX_PIN>(leds, NUM_LEDS);
  FastLED.setBrightness( BRIGHTNESS );
  
}
int n = 0;
void loop() 
  {

    recvWithEndMarker();
    if(message[0] == 'M' && message[1] == 'O' && message[2] == 'D' && message[3] == 'E' && state != message[4])
      {
        state = message[4]; 
        Serial.print("state = ");
        Serial.println((char)state);
         message[0] = '0';
      }
      
      else if (state == '9') 
      {
        FastLED.clear();
        FastLED.show();
        state = '1';
      }
      else if(state == '3')
      {
        n++;
        Serial.println(n);
        paintImage();
      }
      else if(state == '4')
      {
        inputToMatrix();
        state = '1';
        Serial.println("saved");
      }
     
  }

//De adaugat ce culori o sa existe in aplicatie
void paintImage()
{
  static int i = 0;
  static int j = 0;
  CRGB a; 

      Serial.print(" i = ");
      Serial.print(i);
      Serial.print(" j = ");
      Serial.print(j);
      Serial.print(" matrix = ");
      Serial.println((char) Matrix[15 - j][15 - i]);
      switch(Matrix[15 - j][15 - i])
      {
        case 'R':
       a = CRGB::Red;
       break;

       case 'G':
       case 'g':
       a = CRGB::Green;
       break;

       case 'B':
       a = CRGB::Blue;
       break;

       default:
       a = CRGB::Red;
       break;
      }

      leds[Coordonates_To_OrderNumber(i,j)] = a;
      delay(100);
      FastLED.show();
      j++;
      if(j == 16)
      {
        
        i++;
        j = 0;
      }
  if(i == 15 && j == 15)
  {   
  //FastLED.show();
  Serial.println("done printing");
  i = 0;
  j = 0;
  state = '1';
  }

}

uint16_t Coordonates_To_OrderNumber( uint8_t x, uint8_t y)
{
  uint8_t OrderNumber;
      if( y & 0x01) {
        // Odd rows run backwards
        uint8_t reverseX = (MatrixWidth - 1) - x;
        OrderNumber = (y * MatrixWidth) + reverseX;
      } else {
        // Even rows run forwards
        OrderNumber = (y * MatrixWidth) + x;
      }
  return OrderNumber;
}

void recvWithEndMarker() {
    if(Serial1.available())
    {
      Serial.println("here");
      messageBuffer = Serial1.readStringUntil(';');
      if(messageBuffer.length() > 0)
      {
        message = messageBuffer;
        Serial.println(message);
      }
    }
}

void inputToMatrix()
{
   for (int i=0 ; i < 256 ; i++)
   {
     Matrix[i/16][i%16] = message[i+5];
     receivedChars[i] = NULL;
   }
}

this is a weak way of receiving the data

void recvWithEndMarker() {
  if (Serial1.available())
  {
    Serial.println("here");
    messageBuffer = Serial1.readStringUntil(';');
    if (messageBuffer.length() > 0)
    {
      message = messageBuffer;
      Serial.println(message);
    }
  }
}

are you sending a line feed for example after the ; ?

if so the first batch will go through, and then the LF is still in the incoming buffer and so you call messageBuffer = Serial1.readStringUntil(';'); and since there is no new message, you never receive the semi colon and you exit with a timeout after 1 second with messageBuffer holding just one character, the line feed (or CR LF if that's what you sent). The length is then > 0 and so you update message with messageBuffer and if you call inputToMatrix() (not sure this can happen but just in case) you'll be trying to access data beyond the bound of the string with message[i + 5] which will return 0 (if I remember correctly what the String class does when accessing out of bound)

I'm don't think if FastLED.show() will kill your bluetooth because at the end of the day it's just the serial1 hardware UARTand worse case scenario you miss some data if a new frame was sent whilst you perform the display.


I would suggest to study Serial Input Basics to handle this: make the data reception more robust and non blocking and add some safeguards to ensure you got a full frame before doing some matrix manipulation

+1 for serial input basics tutorial

Thank you for the response.

I tried using exemple number 2 from the link you posted and that removed the need of the ';' in the message but after i do fastLED.show() i still get an error on my phone about "broken pipe" , this error occurs when there is no bluetooth connection anymore. This behaviour does not happen if i comment the "FastLED.show()" line in my code

the HC-05 should maintain the connection regardless of what happens on the board.
how is this powered? are you sure you have enough current to power everything? may be the HC-05 looses power which then would be noticed by the phone

is it possible that if i have the hc-05 connected to the same power supply as the LEDs that when i do .show the current is not enough for both and that is why it would stop the bluetooth connection ?

definitely possible if that power supply is not strong enough. the current draw sudden increase could result in a voltage drop which would be enough to reboot the HC-05

you could try to power the HC-05 through the arduino (assuming it has power from USB and it's not powering the LEDs)

tell us more about your LED matrix - what is this ? and how everything is wired / powered

(you should also call show() once, after updating the matrix)

That would brownout the Arduino and the 16x16 too, depending how many are on. What is the "power supply" in use?

the matix used is made out of WS2812B and it seems i did a rookie mistake by thinking that the arduino can supply enough current to support the hc-05 and the matrix . until now i only tried by turning on just some LEDs and not all 256 , i will try tomorrow to supply the LEDs from something else. i searched and it seems that the arduino mega can supply around 800mA and 1 led at full brightness can uyse up to 50mA.

I will try it and come back to tell if it worked tomorrow.
Thank you guys a lot , i was struggling with this for 2 days and only looked at the software part

you definitely need a strong power supply, go for at least 12 amps for example and be careful on the wiring. that's a lot of energy (something like Meanwell PSU 5V 12A 60W or even the 18A model to have plenty). (add a capacitor across the + and – terminals for more reliable operation)

power the arduino separately. if you power through USB, you have only 500mA available but that should be plenty for the Arduino and the HC-05

don't forget to join the GNDs

a good guide : The Magic of NeoPixels | Adafruit NeoPixel Überguide | Adafruit Learning System

Each '2812 draws nearly 1mA doing nothing (dark).

As well as the bulk capacitor (100 - 1000uF) on the strip power leads add a 220 - 470 Ohm resistor in series between the Arduino output and the strip's Din to protect the Arduino output.

Connecting external power, bulk capacitor and resistor to WS2812 strip.

Put the capacitor as close to the strip as possible.

I tried lowerig the brightness of the LEDs to the minimum just for a test and that fixed everything.
I am now waiting for a new power supply and the capacitor .

Thank you all a lot

Good :+1: