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;
}
}
