Taalik
Taalik

Reputation: 11

Issue displaying data on 8x8 led matrix using ARDUINO and PROCESSING

So i'm actually making a little project with arduino and processing. Basically i'm using a C++ program to draw on a 8x8 matrix. This program saves your drawing into a text file in this format : 00000000 00111000 00010000 00111000 00000000 00000000 00000000 00000000

And displays it to a led 8x8 matrix connected to an arduino.

So first in processing, I load this file into a string and then send it to the serial port line by line :

Processing code :

import processing.serial.*;
import java.io.*;
Serial myPort;


void setup() {
  //Make sure the COM port is correct
  myPort = new Serial(this, "COM5", 9600);
  myPort.bufferUntil('\n');
}

void draw() {


  String[] lines= loadStrings("matrice.txt");



            for(int a=0;a<(lines.length);a++)
             {

                    myPort.write(lines[a]);

                    //println(lines.length);


             }
             delay(1000);

} 

Then in arduino i read the serial port and store the data into a string, i convert each line of this string to decimal for exemple if the value of string[0] is "00111100" the conversion will return 60. And then i store each decimals into the led matrix and display it.

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;


        ligne[a] = convertir(inChar[a]);
        Serial.println(ligne[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] = "00000000";
    }
  }


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

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

  }

}

The content of the text file is successfully displayed on the LED matrix but the issue is that after a few seconds, my entire drawing is shifting before returning to its place, it does not stay at the same place it should be. For exemple if i display the letter A this is the result : 'A' displayed | 'A' shifting

I think the issue is a synchronization problem between arduino and processing, processing sends the data in the text file continously and arduino doesn't read the correct data. But honestly i have no clue on how to fix that so i'm asking you guys help.

EDIT : I think i have to send only new data in Processing, for example we read the file if there is no new data we don't send anything to the serial port, if there is new data we send the new data to the serial port. I tried to create a second array that takes data from the string lines to compare it but it doesn't work because at each loop, the second array always equals to the first ...

Thank you for your time !

Upvotes: 1

Views: 654

Answers (2)

Taalik
Taalik

Reputation: 11

Hi Delta_G thank you very much for your help !

I finally managed to make it work. So basically what i was doing wrong is that i used a 38 by 8 array to display the data. This 38 by 8 is only used to display 0 to 9 and A to Z in a sequence. Since my data is only stored in one line and 8 rows i changed the array to 1 by 8. Now the last line is perfectly displayed.

Sure it would be smarter to use bitshifts yeah, it will be a good exercise for me !

So this is the final result i get : C++ program | Led matrix display

Upvotes: 0

Delta_G
Delta_G

Reputation: 3243

result = result | (sample_str[i] & 1);

Does that line work? sample_str isn't an array, so addressing it like one isn't really appropriate. It is a String, so it seems to me you'd want to use the charAt method. Or ditch the String class entirely and keep the string in a char array would be even smarter since String has a nasty habit of crashing Arduino programs.

Also the things stored in the String, are they 0's and 1's or ascii representations of 0 and 1 (ie. 48 and 49). I guess since the LSB will still be the right 0 or 1 it might work, but it is certainly misleading. I would write this line as:

result = result | (sample_str.charAt(i) == '1'? 1:0)

Upvotes: 1

Related Questions