Display a led matrix animation while a song is playing - Beginner :D

Hello,

I would like to display a led animation while a song is playing. I have already programed both part separetly. However I don't how to combine them.

Can someone help me please?

Here's the led animation. (I'm using a 5X7 led matrix. And in the final sketch, a=random(1,7))

/* FACE : 8 EXPRESSION
 *  . X . . . X .         . . . . . X .         . X . . . . .         . . . . . . .         . . . . . . .         . . . . . . .         . . . . . . .         . . . . . . . 
 *  X . X . X . X         . . . . X . X         X . X . . . .         . . . . . . .         . . . . . . .         X . . . . . X         . . . . . . .         X . X . X . X 
 *  X . X . X . X         X X X . X . X         X . X . X X X         X X X . X X X         X X X . X X X         X X . . . X X         . X . . . X .         . X . . . X .
 *  X . X . X . X         . . . . X . X         X . X . . . .         . . . . . . .         X . X . X . X         X . X . X . X         X . X . X . X         X . X . X . X
 *  . X . . . X .         . . . . . X .         . X . . . . .         . . . . . . .         . X . . . X .         . X . . . X .         . . . . . . .         . . . . . . . 
 *      oh                  winka                   winkb                 zzz                 jaded                  mad                    happy                 dead
 */
 
int dead[]  =  {10,4,10,0,10,4,10};

int alarm_animation[7][7]= {
                             {14,17,14,0,14,17,14},            // oh
                             { 4, 4, 4,0,14,17,14},            // winka
                             {14,17,14,0, 4, 4, 4},            // winkb
                             { 4, 4, 4,0, 4, 4, 4},            // zzz
                             { 6, 5, 6,0, 6, 5, 6},            // jaded
                             {14, 5, 2,0, 2, 5,14},            // mad
                             { 2, 4, 2,0, 2, 4, 2}};           // happy

// déclaration des broches pour la gestion des lignes et des colonnes
int colonne[] = {1,2,3,4,5,6,7};         // matrice de colonne
int ligne[] = {8,9,10,11,12};            // matrice de ligne

void setup() {
  // initialisation des sorties de la matrice 5X7.
  for (int i= 0; i < 7; i++) pinMode(colonne[i], OUTPUT);
  for (int i= 0; i < 5; i++) pinMode(ligne[i], OUTPUT);
  //place tous les potentiels de colonne à 1 pour eteindre tous les points
  for (int i= 0; i < 7; i++) digitalWrite(colonne[i], HIGH);
}

// sous programme qui affiche la colonne de la matrice
void affiche_colonne(byte motif,int col){
    int masque=1;                                  // initialise le premier 1 de la série des ET logique
    digitalWrite(colonne[col],0);                  // selectionne la colonne
    for (int j=0;j<5;j++) {                        // parcours tous les lignes
      digitalWrite(ligne[j],(masque & motif));     // selectionne la colonne ou pas en fonction du motif
      masque = masque<<1;                          // décalle le 1 à gauche pour passer à la puissance de 2 suivante
    }
 }
// sous programme qui efface la colonne de la matrice
void efface_colonne(int col){
    digitalWrite(colonne[col],1);                            // desselectionne la colonne
}

// sous programme qui affiche l'animation de l'alarme
void affiche_alarm_animation(int a){
  for (int i=0; i<7; i++) {
    affiche_colonne(alarm_animation[a][i],i);
    delay(2);
    efface_colonne(i);
  }

Then here's the program for the music :

// déclaration des broches pour la gestion du buzzer
int buzzerPin = A2; 

// Liste des notes se trouvans dans la chanson
// Octave 3
#define E3   165        // Mi
#define Ab3  208        // Sol#
#define A3   220        // La
#define B3   247        // Si
// Octave 4
#define Db4   277       // Do#
#define D4    294       // Ré
#define E4    330       // Mi
#define Ab4   415       // Sol#
#define A4    440       // La
#define B4    494       // Si
// 0ctave 5
#define Db5   554       // Do#
#define D5    587       // Ré
#define E5    659       // Mi

// Mélodie : chaque note est jouée l'une après l'autre
int melody[] ={
  A4,  Ab4, A4,  Ab4, A4,  E4,  A4,  D4,  A3,  Db4, 
  A4,  Ab4, A4,  Ab4, A4,  E4,  A4,  D4,  A3,  Db4,
  A4,  Ab4, A4,  A3,  Ab4, A4,  A3,  Ab4, A4,  A3, E4,  A3,
  Db4, D4,  E4,  Db4, B3,  A3,  Ab3, A3,  E3,  A3, B3,  Db4, Db4, D4,
  E4,  D4,  Db4, B3,  A4,  Ab4, A4,  A3,  Ab4, A4, A3,  E4,  A4,  A3,  D4,  A3, Db4, D4,  E4, Db5, B4, E4,  B4,  A4, Ab4, 
  A4,  A3,  B3,  Db4, E3,  A3,  Db4, D4,  E4,  E3, Db4, D4,  Db4, B3,  A4,  B4, A4,  Ab4, A4, A3,  E4, A3,  A4,  B4, A4,  Ab4, A4, A3, E4, A3,  A4, B4, A4, Ab4,
  A4,  B4,  Db5, D5,  E5,  Db5, B4,  A4,  Ab4, B3, A4,  B4
  };
 
// Durée de chaque note : 4 => 1/4 de ton
// 8 => 1/8 de ton...
int noteDurations[] = {
  4, 4, 4, 4, 4, 4, 4, 1, 8, 8,
  4, 4, 4, 4, 4, 4, 4, 1, 8, 8,
  4, 8, 4, 8, 8, 4, 8, 8, 4, 8, 8, 8,
  4, 4, 4, 4, 2, 8, 8, 3, 8, 8, 8, 2, 8, 8,
  2, 8, 8, 2, 2, 8, 4, 8, 8, 4, 8, 8, 8, 3, 4, 4, 4, 4, 4, 2, 8, 8,
  2, 8, 8, 4, 4, 4, 8, 8, 4, 4, 4, 8, 8, 3, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 8, 8
  };

void setup() {
// initialisation du buzzer 
  pinMode(buzzerPin,OUTPUT);
}
void melodie_alarm() {
  for (int thisNote = 0; thisNote < 114; thisNote++) {                // joue chaque note
        int noteDuration = 1200/noteDurations[thisNote];              // Durée d'une note : 1200 ms divisé par la durée
        tone(buzzerPin, melody[thisNote],noteDuration);               // On joue la note
        affiche_alarm_animation(random(1,7));
        int pauseBetweenNotes = noteDuration * 1.20;                  // On fait une pause de 20% du temps de la note en plus pour pouvoir distinguer les notes à l'oreille
        delay(pauseBetweenNotes);
    }
}

Thanks in advance :smiley:

You need a script to tell the program what to display and when. This will be in the form of an array saying what order to display your frames.
Interlace the two programs using a state machine for each task. These links tell you how to do it:-
See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

This is something similar I did:-