Led matrix MAX7219 issue when displaying

Hi everyone,

So i’m using arduino and max7219 8x8 Led display. I wrote a code that takes binary in the serial port and converting it in decimals. Then I copy all the converted decimals in the display array.

The issue is that the last line of my led matrix is not displaying correctly it’s shifted. What i do : I open the serial monitor and paste the following binary code :

0011110000111100001111000011110000111100001111000011110000111100

This is the result on the led matrix :wrong display

and sometimes it displays correctly : Correct display

It always happen to the last line of the matrix.

Thank you.

Here is my code :

// Exemple de code Module Max7219 reseau de 8x8 LEDS
//
// Revision 19 08 201


unsigned char j;
unsigned char i;
unsigned char k;
unsigned char l;

String inChar[8];
String inCharTemp[8];
int ligne[8];


//Cablage du module (gnd et +V) utilise 3 pins
int Max7219_pinCLK = 10;
int Max7219_pinCS = 9;
int Max7219_pinDIN = 8;


//Définition des pixels a eclairer
//0-9 puis A-Z soit 10+26 = 36 caractères
char disp1[38][8];

//Autre exemple, caracteres speciaux (a definir soi meme)
//Voir explications sur http://tiptopboards.com/arduino_tutoriel/posting.php?mode=edit&f=2&p=6
//{0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55},  //damier1
// {0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA},  //damier2

//Ecriture d'un caractere 8x8
void Write_Max7219_byte(unsigned char DATA)
{
  unsigned char i;
  digitalWrite(Max7219_pinCS, LOW);
  for (i = 8; i >= 1; i--)
  {
    digitalWrite(Max7219_pinCLK, LOW);
    digitalWrite(Max7219_pinDIN, DATA & 0x80); // Extracting a bit data
    DATA = DATA << 1;
    digitalWrite(Max7219_pinCLK, HIGH);
  }
}

//Ecriture elementaire d une seule rangee
void Write_Max7219(unsigned char address, unsigned char dat)
{
  digitalWrite(Max7219_pinCS, LOW);
  Write_Max7219_byte(address);           //address,code of LED
  Write_Max7219_byte(dat);               //data,figure on LED
  digitalWrite(Max7219_pinCS, HIGH);
}

//Initialisation du module Max 7219
void Init_MAX7219(void)
{
  Write_Max7219(0x09, 0x00);       //decoding :BCD
  Write_Max7219(0x0a, 0x03);       //brightness
  Write_Max7219(0x0b, 0x07);       //scanlimit;8 LEDs
  Write_Max7219(0x0c, 0x01);       //power-down mode:0,normal mode:1
  Write_Max7219(0x0f, 0x00);       //test display:1;EOT,display:0
}

int convertir(String ligne)
{
  String sample_str = ligne;
  uint32_t result = 0;
  for (unsigned int i = 0; i < sample_str.length(); ++i)
  {
    result = result << 1;
    result = result | (sample_str[i] & 1);
  }

  return result;

}




//Le programme d affichage
void setup()
{
  //Pins a utiliser
  pinMode(Max7219_pinCLK, OUTPUT);
  pinMode(Max7219_pinCS, OUTPUT);
  pinMode(Max7219_pinDIN, OUTPUT);
  delay(50);  //Initialiser
  Serial.begin(9600);
  Init_MAX7219();
  for (int i = 0; i < 8; i++)
  {
    ligne[i] = 0;
  }
}


void loop()
{


  for (int a = 0; a < 8; a++)
  {
    for (int b = 0; b < 8; ++b)
    {

      if (Serial.available())
      {
        inChar[a] += Serial.read() - 48; // On lit les informations sur le port série puis on stock tout dans un tableau de string
        ligne[a] = convertir(inChar[a]); // On convertit en décimal chaque ligne qu'on reçoit
        Serial.println(inChar[a]);
      }
    }
  }


  // Boucle for qui assigne a chaque case de disp la ligne en décimal
  for (int a = 0; a < 38; a++)
  {
    for (int b = 0; b < 8; b++)
    {
      disp1[a][b] = ligne[b];
      inChar[b] = "";
    }
  }


  for (j = 0; j < 38; j++)
  {
    for ( i = 0; i < 9; i++)
    {

      Write_Max7219(i, disp1[j][i - 1]); // Affiche la matrice
    }

  }

}

I am not sure where you got that code.
Wouldn't it be easier to use the Parola library or the LedControl library?

ieee488:
I am not sure where you got that code.
Wouldn't it be easier to use the Parola library or the LedControl library?

It's a code using Max7219 functions i got it from here : Link

I modified it to receive binary. I chose to work this way because my goal is to read with processing a text file with binary in it and send it to arduino. Everything works great exept this last line in the matrix that won't display correctly.

I found the issue, i was using a 38x8 array to store the data, 38 lines are only required when you want to display 0 to 9 and A to Z in a sequence (each line represents one letter or number). Since my data is only stored in one line and eight rows i modified my array to 1 by 8. Now the last line works perfectly !
Thanks :slight_smile: