Display static text on LED Module 7219 (12 pcs) using serial

Hi there !

I am struggling with displaying a custom text on LED module MAX7219. I have 12 pcs connected into one.
All works , I can display all I want except the custom messages from serial as a string. If its about bytes I am able to do it. My question is ... how to convert string into byte and reverse. Down there a piece of code ...

...
...
...

if(Serial1.available() > 0)      // Send data only when you receive data:
   {
      data = Serial1.read();        //Read the incoming data & store into data
      Serial1.print(data);          //Print Value inside data in Serial monitor
      Serial1.print("\n"); 
      if(data == '1') {

 if(data =='/'){ 

    String c = String(data);
    P.displayText(c,PA_CENTER,2,4,PA_OPENING_CURSOR, PA_NO_EFFECT);    
      while (!P.displayAnimate());

}
}

I am using Leonardo and serial1 to send data through bluetooth HC05 from PC to arduino.
I have TLazserial component for Lazarus IDE ( Delphi ) . I got a editbox there on a form and on button click I want to send string to LED Matrix. If its about the bytes in Lazarus like:

Form1.button1.click
Begin

Form1.Lazserial1.write(edit1.text);

end;

Works just fine. In arduino code I have assigned string to byte no. 1 and the LED Matrix works when I press the button. But to send a custom string I am having a problem.

Don’t put the input into a String in the first place use a char array, it seems silly putting them into a String only to put them back into a char array.

Thats the thing , I am good with Delphi and Visual Basic but in here I just can't figure it out how to set it out. I was trying to make char arrey with buffer but it failed. Can you help me with that ?

Sure post what failed and I will see if I can spot why.

Alright , let's do it ...

Summary:

  1. Arduino Leonardo
  2. LED Max 7219 - 12 modules
  3. HC 05 Bluetooth module - interfaced SPI
  4. Using Serial1 due to Leonardo serial(default) reserved.
  5. Using MD_Parola and MD_MAX_XXX libraries
  6. Lazarus IDE with TLazserial component.

Main funcionality:

Sending messages to the display from the application made in Lazarus using serial communication via bluetooth.

Arduino code so far:

#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>


#define MAX_DEVICES 12
#define CLK_PIN   15 //sck
#define DATA_PIN  16 // mosi
#define CS_PIN    17 //ss

MD_Parola P = MD_Parola(CS_PIN, MAX_DEVICES);


char data = 0; 

void setup(void)
{
  P.begin();
  P.setInvert(false);
   Serial1.begin(9600);

}

void loop(void){

   if(Serial1.available() > 0)     
   {
      data = Serial1.read();        
       
      if(data == '1') {           
      P.displayText("Empty totes down !" ,PA_CENTER,2,4,PA_OPENING_CURSOR, PA_NO_EFFECT);
      while (!P.displayAnimate());
   
      }

      if(data == '2'){
      P.displayText("Empty totes up !",PA_CENTER,2,4,PA_OPENING_CURSOR, PA_NO_EFFECT);
      while (!P.displayAnimate());
    
      }

      if(data =='3'){   
      P.displayText("Stop packing !",PA_CENTER,2,4,PA_OPENING_CURSOR, PA_NO_EFFECT);
      while (!P.displayAnimate());
      
      }

      if(data =='4'){ 
      P.displayText("Continue packing.",PA_CENTER,2,4,PA_OPENING_CURSOR, PA_NO_EFFECT);
      while (!P.displayAnimate());

      }

       if(data =='5'){ 
      P.displayClear();
      while (!P.displayAnimate());

      }

        if(data =='6'){ 
      P.displayText("Packages down !",PA_CENTER,2,4,PA_OPENING_CURSOR, PA_NO_EFFECT);
      while (!P.displayAnimate());

      }

        if(data =='7'){ 
      P.displayText("Packages up !",PA_CENTER,2,4,PA_OPENING_CURSOR, PA_NO_EFFECT);    
      while (!P.displayAnimate());

      }

      if(data =='/'){ 

        
    P.displayText(data,PA_CENTER,2,4,PA_OPENING_CURSOR, PA_NO_EFFECT);    
      while (!P.displayAnimate())    ;
      
      }

      
      } // end of serial input
  } // end of void loop

So that works perfect. In the attachment you can find screen from my app made in Delphi.
Basically when you chose one of the radioboxes and press "send" app is sending byte through serial on certain com port and baud rate which is equivalent of bluetooth setup.

This is the part of the Delphi code I have used:

procedure TForm1.Button5Click(Sender: TObject);
begin

   if form1.RadioButton2.Checked  =true  then
   begin
    form1.LazSerial1.WriteData('1');

   end;

   if form1.RadioButton3.Checked  =true  then
   begin
    form1.LazSerial1.WriteData('2');
   end;

   if form1.RadioButton4.Checked  =true  then
   begin
    form1.LazSerial1.WriteData('3');
   end;

   if form1.RadioButton5.Checked  =true  then
   begin
    form1.LazSerial1.WriteData('4');
   end;

   if form1.RadioButton6.Checked  =true  then
   begin
    form1.LazSerial1.WriteData('6');
   end;

   if form1.RadioButton7.Checked  =true  then
   begin
    form1.LazSerial1.WriteData('7');
   end;

end;

When the byte is send , same byte represents some chars in arduino code and that's how I display messages on LED Matrix.

As you can see on screen there is toogle box named "Manual Messages" where I would like to send message I want , but without scrolling , I had some examples on internet but I doesn't make the thing I would like to achieve.

So as I can assume we have to serial1.readString() from the Delphi application then we need to change that string into char that can be read in :

P.displayText(textfromdelphi ,PA_CENTER,2,4,PA_OPENING_CURSOR, PA_NO_EFFECT);
      while (!P.displayAnimate());

Also , as to be prepared for text input in delphi I would like to preface the procedure like :

char textfromdelphi;
...
...
...

if(data == '/') {

     texfromdelphi= serial1.read();
      
      P.displayText(textfromdelphi ,PA_CENTER,2,4,PA_OPENING_CURSOR, PA_NO_EFFECT);
      while (!P.displayAnimate());
   
      }

Btw. actually the component in Lazarus gives you quite nice tool to control things via serial and bluetooth. If I can help you making something for you in thanks for your help , just let me know.

Best Regards
Jakub

The Parola library has an example of how to read from the serial stream and collect the characters into a char array. The new char string is then loaded into the animation when the current animation is completed. Have you looked at this?

Yes I did , but that's a scrolling type of the message. Its not what I want o have ..

Sure, the animation is different, but how you build up the message as it arrives is the same. That seems to be where you are having problems.

I have tried a few solution and that one seem to work !
Maybe someone need it.

char data = 0; 
byte buffer[1];
char inData[64] = ""; // The string (max 64 bytes)
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character

...
...



if(data=='/');{
index=0;
 P.delChar(inChar);
      
  while(Serial1.available() > 0) // Don't read unless there is data
  {
 
    if(index < 64) // One less than the size of the array
    {
      inChar = Serial1.read(); // Read a character
      Serial1.write(inChar);
      inData[index] = inChar; // Store it
      index++; // Increment where to write next
      inData[index] = '\0'; // Null terminate the string
    }       
  }
 
  byte c;
  delay(100);
 P.displayText(inData,PA_CENTER,2,4,PA_PRINT, PA_NO_EFFECT);
      while (!P.displayAnimate());
      
}

code is useful when you want o send static text over the serial port.

Regards
Jakub