LEDmatrix + push button with different sprites

Hi there,

It's my first post on the forum and I'm pretty new to arduino and coding. I already manage to try different projects and I understand a few basic things about what I want to make.
I'm sorry for the terms I use (english is not my first language). I'd love to manage to do my project by myself (there's so much stuff on this site already) but I can't find to put my hands on a specific tutorial or code.

To put this on simple words my project is this one :
On my Arduino Uno I have an 8x8 led matrix with a MAX7219 (soldered to it) with a push button wired to the arduino. I'm putting the schmatics for you to see.

I want to load my own animated sprites on it and with a button push browse through it (I have 2 animated loops but I want to add more later).

I've been searching for more than a week and I managed to get running codes that print my loops on the matrix but I can't find a way to separate them with the button push...
I tried to take inspiration with codes dealing with "button counter" and "matrix dice" projects but I can't find a way to put the information I need in good use...
I though about using the Parola library because they have a way to put custom characters but I failed to make them work on my code..
With all the codes I tried to mix or create I always ended up with lots of errors and very long lists... I'm sure there are simplier ways but I don't have enough knowledge.
The latest code I stumbled on is a matrix dice I managed to put on my arduino :

#include "LedControl.h"

/*

LedControl lc=LedControl(12,11,10,1);
int button =2;
/* we always wait a bit between updates of the display */
unsigned long delaytime=500;
  byte six[8]={B00000000,B11011011,B11011011,B00000000,B00000000,B11011011,B11011011,B00000000};
  byte five[8]={B00000000,B01100110,B01100110,B00011000,B00011000,B01100110,B01100110,B00000000};
  byte four[8]={B00000000,B01100110,B01100110,B00000000,B00000000,B01100110,B01100110,B00000000};
  byte three[8]={B11000000,B11000000,B00000000,B00011000,B00011000,B00000000,B00000011,B00000011};
  byte two[8]={B00000000,B00000000,B00000000,B01100110,B01100110,B00000000,B00000000,B00000000};
  byte one[8]={B00000000,B00000000,B00000000,B00011000,B00011000,B00000000,B00000000,B00000000};

void setup() {
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  pinMode(button,INPUT);
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,14);
  /* and clear the display */
  lc.clearDisplay(0);
}

/*
 This method will display the characters
 (you need at least 7x7 leds to see the whole chars)
 */
void one1() {
  /* here is the data for the characters */
  
  /* now display them one by one with a small delay */
  lc.setRow(0,0,one[0]);
  lc.setRow(0,1,one[1]);
  lc.setRow(0,2,one[2]);
  lc.setRow(0,3,one[3]);
  lc.setRow(0,4,one[4]);
  lc.setRow(0,5,one[5]);
  lc.setRow(0,6,one[6]);
  lc.setRow(0,7,one[7]);
  
  delay(delaytime);
}
void two2() {
  /* here is the data for the characters */
  
  /* now display them one by one with a small delay */
  lc.setRow(0,0,two[0]);
  lc.setRow(0,1,two[1]);
  lc.setRow(0,2,two[2]);
  lc.setRow(0,3,two[3]);
  lc.setRow(0,4,two[4]);
  lc.setRow(0,5,two[5]);
  lc.setRow(0,6,two[6]);
  lc.setRow(0,7,two[7]);
  
  delay(delaytime);
}
void three3() {
  /* here is the data for the characters */
  
  /* now display them one by one with a small delay */
  lc.setRow(0,0,three[0]);
  lc.setRow(0,1,three[1]);
  lc.setRow(0,2,three[2]);
  lc.setRow(0,3,three[3]);
  lc.setRow(0,4,three[4]);
  lc.setRow(0,5,three[5]);
  lc.setRow(0,6,three[6]);
  lc.setRow(0,7,three[7]);
  
  delay(delaytime);
}
void four4() {
  /* here is the data for the characters */
  
  /* now display them four by four with a small delay */
  lc.setRow(0,0,four[0]);
  lc.setRow(0,1,four[1]);
  lc.setRow(0,2,four[2]);
  lc.setRow(0,3,four[3]);
  lc.setRow(0,4,four[4]);
  lc.setRow(0,5,four[5]);
  lc.setRow(0,6,four[6]);
  lc.setRow(0,7,four[7]);
 
  delay(delaytime);
}
void five5() {
  /* here is the data for the characters */
  
  /* now display them five by five with a small delay */
  lc.setRow(0,0,five[0]);
  lc.setRow(0,1,five[1]);
  lc.setRow(0,2,five[2]);
  lc.setRow(0,3,five[3]);
  lc.setRow(0,4,five[4]);
  lc.setRow(0,5,five[5]);
  lc.setRow(0,6,five[6]);
  lc.setRow(0,7,five[7]);

  delay(delaytime);
}
void six6() {
  /* here is the data for the characters */
  
  /* now display them six by six with a small delay */
  lc.setRow(0,0,six[0]);
  lc.setRow(0,1,six[1]);
  lc.setRow(0,2,six[2]);
  lc.setRow(0,3,six[3]);
  lc.setRow(0,4,six[4]);
  lc.setRow(0,5,six[5]);
  lc.setRow(0,6,six[6]);
  lc.setRow(0,7,six[7]);
  
  delay(delaytime);
}


void loop() { 
  int x ;
  if(digitalRead(button)== HIGH )
{
 x = random(1,7);

switch(x){
case 1 : one1();
break;
case 2 : two2();
break;
case 3 : three3();
break;
case 4 : four4();
break;
case 5 : five5();
break;
case 6 : six6();
break;}
}
}

Obviously I would like to replace the random function by a "croissant?" (1 to ...) one and to replace the sprites/bytes by my own animated ones (i'm putting their codes) :

a kind of circle wheel

const byte IMAGES[][8] = {
{
  B00111100,
  B01000000,
  B10000000,
  B10000000,
  B10000001,
  B10000001,
  B01000010,
  B00111100
},{
  B00111100,
  B01000010,
  B10000000,
  B10000000,
  B10000000,
  B10000001,
  B01000010,
  B00111100
},{
  B00111100,
  B01000010,
  B10000001,
  B10000000,
  B10000000,
  B10000000,
  B01000010,
  B00111100
},{
  B00111100,
  B01000010,
  B10000001,
  B10000001,
  B10000000,
  B10000000,
  B01000000,
  B00111100
},{
  B00111100,
  B01000010,
  B10000001,
  B10000001,
  B10000001,
  B10000000,
  B01000000,
  B00111000
},{
  B00111100,
  B01000010,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B01000000,
  B00110000
},{
  B00111100,
  B01000010,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B01000010,
  B00100000
},{
  B00111100,
  B01000010,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B01000010,
  B00000100
},{
  B00111100,
  B01000010,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B00000010,
  B00001100
},{
  B00111100,
  B01000010,
  B10000001,
  B10000001,
  B10000001,
  B00000001,
  B00000010,
  B00011100
},{
  B00111100,
  B01000010,
  B10000001,
  B10000001,
  B00000001,
  B00000001,
  B00000010,
  B00111100
},{
  B00111100,
  B01000010,
  B10000001,
  B00000001,
  B00000001,
  B00000001,
  B01000010,
  B00111100
},{
  B00111100,
  B01000010,
  B00000001,
  B00000001,
  B00000001,
  B10000001,
  B01000010,
  B00111100
},{
  B00111100,
  B00000010,
  B00000001,
  B00000001,
  B10000001,
  B10000001,
  B01000010,
  B00111100
},{
  B00011100,
  B00000010,
  B00000001,
  B10000001,
  B10000001,
  B10000001,
  B01000010,
  B00111100
},{
  B00001100,
  B00000010,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B01000010,
  B00111100
},{
  B00000100,
  B01000010,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B01000010,
  B00111100
},{
  B00100000,
  B01000010,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B01000010,
  B00111100
},{
  B00110000,
  B01000000,
  B10000001,
  B10000001,
  B10000001,
  B10000001,
  B01000010,
  B00111100
},{
  B00111000,
  B01000000,
  B10000000,
  B10000001,
  B10000001,
  B10000001,
  B01000010,
  B00111100
}};
const int IMAGES_LEN = sizeof(IMAGES)/8;

a windmill one :

                  {B00000000,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00001000,
                   B00001100,
                   B00001110,
                   B00001100
                  };{B00000000,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00001111,
                   B00000111,
                   B00000010,
                   B00000000
                  };{B00000000,
                   B00000010,
                   B00000111,
                   B00001111,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00000000
                  };{ B00001100,
                    B00001110,
                    B00001100,
                    B00001000,
                    B00000000,
                    B00000000,
                    B00000000,
                    B00000000
                  };{B00110000,
                   B01110000,
                   B00110000,
                   B00010000,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00000000
                  };{B00000000,
                   B01000000,
                   B11100000,
                   B11110000,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00000000
                  };{ B00000000,
                    B00000000,
                    B00000000,
                    B00000000,
                    B11110000,
                    B11100000,
                    B01000000,
                    B00000000
                  };{B00000000,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00010000,
                   B00110000,
                   B01110000,
                   B00110000
                  };

Also , I'm putting the first code that I made with the animations working, there's no button involved in this one but maybe I can incorporate a switch case structure? I don't really know how and where to write it, I tried to look into it but I always end up with a bunch of errors.

#include <LedControl.h>

int DIN = 12;
int CS =  10;
int CLK = 11;
#define button 2
int state = 0;
int old = 0;
int buttonPoll = 0;

//Main
byte A[8] = {B00111100,
             B01000000,
             B10000000,
             B10000000,
             B10000000,
             B10000000,
             B01000010,
             B00111100
            };
byte B[] = {B00111100,
            B01000010,
            B10000000,
            B10000000,
            B10000000,
            B10000000,
            B01000000,
            B00111100
           };
byte C[] = { B00111100,
             B01000010,
             B10000001,
             B10000000,
             B10000000,
             B10000000,
             B01000000,
             B00111000
           };
byte D[] = {B00111100,
            B01000010,
            B10000001,
            B10000001,
            B10000000,
            B10000000,
            B01000000,
            B00110000
           };
byte E[] = {B00111100,
            B01000010,
            B10000001,
            B10000001,
            B10000001,
            B10000000,
            B01000000,
            B00100000
           };
byte F[] = {B00111100,
            B01000010,
            B10000001,
            B10000001,
            B10000001,
            B10000001,
            B01000000,
            B00000000
           };
byte G[] = {B00111100,
            B01000010,
            B10000001,
            B10000001,
            B10000001,
            B10000001,
            B00000010,
            B00000000
           };
byte H[] = {B00111100,
            B01000010,
            B10000001,
            B10000001,
            B10000001,
            B00000001,
            B00000010,
            B00000100
           };
byte I[] = {B00111100,
            B01000010,
            B10000001,
            B10000001,
            B00000001,
            B00000001,
            B00000010,
            B00001100
           };
byte J[] = {B00111100,
            B01000010,
            B10000001,
            B00000001,
            B00000001,
            B00000001,
            B00000010,
            B00011100
           };
byte K[] = {B00111100,
            B01000010,
            B00000001,
            B00000001,
            B00000001,
            B00000001,
            B00000010,
            B00111100
           };
byte L[] = {B00111100,
            B00000010,
            B00000001,
            B00000001,
            B00000001,
            B00000001,
            B01000010,
            B00111100
           };
byte M[] = {B00011100,
            B00000010,
            B00000001,
            B00000001,
            B00000001,
            B10000001,
            B01000010,
            B00111100
           };
byte N[] = {B00001100,
            B00000010,
            B00000001,
            B00000001,
            B10000001,
            B10000001,
            B01000010,
            B00111100
           };
byte O[] = {B00000100,
            B00000010,
            B00000001,
            B10000001,
            B10000001,
            B10000001,
            B01000010,
            B00111100
           };
byte P[] = {B00000000,
            B00000010,
            B10000001,
            B10000001,
            B10000001,
            B10000001,
            B01000010,
            B00111100
           };
byte Q[] = {B00000000,
            B01000000,
            B10000001,
            B10000001,
            B10000001,
            B10000001,
            B01000010,
            B00111100
           };
byte R[] = {B00100000,
            B01000000,
            B10000000,
            B10000001,
            B10000001,
            B10000001,
            B01000010,
            B00111100
           };
byte S[] = {B00110000,
            B01000000,
            B10000000,
            B10000000,
            B10000001,
            B10000001,
            B01000010,
            B00111100
           };
byte T[] = {B00111000,
            B01000000,
            B10000000,
            B10000000,
            B10000000,
            B10000001,
            B01000010,
            B00111100
           };
byte Design8[8] = {B00000000,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00001000,
                   B00001100,
                   B00001110,
                   B00001100
                  };
byte Design7[8] = {B00000000,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00001111,
                   B00000111,
                   B00000010,
                   B00000000
                  };
byte Design6[8] = {B00000000,
                   B00000010,
                   B00000111,
                   B00001111,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00000000
                  };
byte Design5[8] = { B00001100,
                    B00001110,
                    B00001100,
                    B00001000,
                    B00000000,
                    B00000000,
                    B00000000,
                    B00000000
                  };
byte Design4[8] = {B00110000,
                   B01110000,
                   B00110000,
                   B00010000,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00000000
                  };
byte Design3[8] = {B00000000,
                   B01000000,
                   B11100000,
                   B11110000,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00000000
                  };
byte Design2[8] = { B00000000,
                    B00000000,
                    B00000000,
                    B00000000,
                    B11110000,
                    B11100000,
                    B01000000,
                    B00000000
                  };
byte Design1[8] = {B00000000,
                   B00000000,
                   B00000000,
                   B00000000,
                   B00010000,
                   B00110000,
                   B01110000,
                   B00110000
                  };







LedControl lc = LedControl(DIN, CLK, CS, 0);

void setup() {
   pinMode(button,INPUT);
  lc.shutdown(0, false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0, 7);
  /* and clear the display */
  lc.clearDisplay(0);
}

void loop() {

  /*windmill pattern */

  printByte(Design1);

  delay(100);

  printByte(Design2);

  delay(100);

  printByte(Design3);

  delay(100);

  printByte(Design4);

  delay(100);

  printByte(Design5);

  delay(100);

  printByte(Design6);

  delay(100);

  printByte(Design7);

  delay(100);

  printByte(Design8);

  delay(100);
  

  /*wheel pattern */

  printByte(A);

  delay(100);

  printByte(B);

  delay(100);

  printByte(C);

  delay(100);

  printByte(D);

  delay(100);

  printByte(E);

  delay(100);

  printByte(F);

  delay(100);

  printByte(G);

  delay(100);

  printByte(H);

  delay(100);

  printByte(I);

  delay(100);

  printByte(J);

  delay(100);

  printByte(K);

  delay(100);

  printByte(L);

  delay(100);

  printByte(M);

  delay(100);


  printByte(N);

  delay(100);

  printByte(O);

  delay(100);


  printByte(P);

  delay(100);

  printByte(Q);

  delay(100);

  printByte(R);

  delay(100);


  printByte(S);

  delay(100);

  printByte(T);




}


void printByte(byte character [])
{
  int i = 0;
  for (i = 0; i < 8; i++)
  {
    lc.setRow(0, i, character[i]);
  }
}

Do you have some leads on where I should dig into? maybe there's a library made for this kind of stuff (the Parola is stuffed with cool effects but for text). If there's a function I should use ? Or maybe you know a project similar to mine.

Thanks for reading and sorry if I made some mistakes (I tried to do this by the rules).

Sure, easy. Just use a state machine with one state for each effect. Set up the button to cycle through the states. I've done that with NeoPixel effects, I'm sure many other people have...

Use an 'enum' with enumerated constants, an int state is usually too obfuscated.

Okay thank you for your reply!
I knew I would have to learn "state machine" but I didn't know how it was called before you taught me aha! I was just referring to it by "the if and else". So thank you for putting a light on it!

I'm posting where I'm at with my code for know, I found a quick course about it dealing with a push button and LEDs, I just need to find a way to replace the LEDs states by my sprites on the 8x8 matrix for now.

#include <LedControl.h>

//There will be 3 states, ONE, TWO, THREE, (+ one default called ZERO), each state is a sprite animation on the 8x8 LedMatrix,
//with the push of a button the states will go from one to an other

#define button 3

LedControl lc = LedControl(DIN, CLK, CS, 1);

int state = 0;              //integrer to hold current state   
int old = 0;                //integrer to hold last state   
int buttonPoll = 0;         //integrer to hold button state   




void setup() {
   pinMode (button, INPUT);        //button set as input
   lc.shutdown(0, false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0, 7);
  /* and clear the display */
  lc.clearDisplay(0);
}

void loop() {
                                         //debouncing routine to read button
    buttonPoll = digitalRead(button);    //poll the state of button
    if(buttonPoll == 1) {                //check if it has been pressed              
   delay(50);                            //wait 50ms
   buttonPoll = digitalRead(button);     //poll button again
   if (buttonPoll == 0) {                //if it is 0 considered one press
     state = old + 1;                    //increase state by 1
   }}
   else{                                  //iff button has not been pressed
    delay(100);                            //wait 100ms
   }
   switch (state) {                      //react to button press & state
 case 1:                                 // if state is 1
//insert ONE

    old = state;
    break;
 case 2:
    //insert TWO

    old = state;
    break;
 case 3:
    //insert THREE

    old = state;
    break;
  default:
  //insert ZERO                           //if state is not 1,2,3
                                          //ZERO
    old = 0;
    break;
  



      
   }
}


Okay so I tried to implement what I found on a Led Sprite Generator (https://xantorohara.github.io/led-matrix-editor) and I came up with this code :

#include <LedControl.h>

//There will be 3 states, ONE, TWO, THREE, (+ one default called ZERO), each state is a sprite animation on the 8x8 LedMatrix,
//with the push of a button the states will go from one to an other
//Using a MAX7219
// pin 12 is connected to the DataIn 
//pin 11 is connected to the CLK 
//pin 10 is connected to LOAD 
//pin 2 is connected to switch
#define button 2


LedControl display = LedControl(12, 11, 10, 1);

int state = 0;              //integrer to hold current state   
int old = 0;                //integrer to hold last state   
int buttonPoll = 0;         //integrer to hold button state   




const uint64_t ZERO[] PROGMEM = {
 0x3c4281818181423c,
  0x003c424242423c00,
  0x0000182424180000,
  0x0000001818000000,
  0x0000182424180000,
  0x0018244242241800,
  0x003c424242423c00,
  0x3c4281818181423c,
  0x6681810000818166,
  0x8100000000000081,
  0x4281000000008142
};
const int ZERO_LEN = sizeof(ZERO)/8;


const uint64_t ONE[] PROGMEM = {
 0x3c4201010101023c,
  0x3c0201010101423c,
  0x1c0201010181423c,
  0x0c0201018181423c,
  0x040201818181423c,
  0x000281818181423c,
  0x004081818181423c,
  0x204080818181423c,
  0x304080808181423c,
  0x384080808081423c,
  0x3c4080808080423c,
  0x3c4280808080403c,
  0x3c42818080804038,
  0x3c42818180804030,
  0x3c42818181804020,
  0x3c42818181814000,
  0x3c42818181810200,
  0x3c42818181010204,
  0x3c4281810101020c,
  0x3c4281010101021c
};
const int ONE_LEN = sizeof(ZERO)/8;

const uint64_t TWO[] PROGMEM = {
 0x0c0e0c0800000000,
  0x0002070f00000000,
  0x000000000f070200,
  0x00000000080c0e0c,
  0x0000000010307030,
  0x00000000f0e04000,
  0x0040e0f000000000,
  0x3070301000000000
};
const int TWO_LEN = sizeof(ZERO)/8;

const uint64_t THREE[] PROGMEM = {
 0x3c4e8d9999b1723c,
  0x3c46879db9e1623c,
  0x3c4287bffde1423c,
  0x3c4281ffff81423c,
  0x3c42e1f99f87423c,
  0x3c62f1b99d8f463c,
  0x3c72b199998d4e3c,
  0x3c5a999999995a3c
};
const int THREE_LEN = sizeof(ZERO)/8;

void setup() {
   pinMode (button, INPUT);        //button set as input
   display.shutdown(0, false);
  /* Set the brightness to a medium values */
  display.setIntensity(0, 7);
  /* and clear the display */
  display.clearDisplay(0);
}



void displayImage(uint64_t image) {
  for (int i = 0; i < 8; i++) {
    byte row = (image >> i * 8) & 0xFF;
    for (int j = 0; j < 8; j++) {
      display.setLed(0, i, j, bitRead(row, j));
    }
  }
}

int i = 0;
void loop() {
                                         //debouncing routine to read button
    buttonPoll = digitalRead(button);    //poll the state of button
    if(buttonPoll == 1) {                //check if it has been pressed              
   delay(10);                            //wait 50ms
   buttonPoll = digitalRead(button);     //poll button again
   if (buttonPoll == 0) {                //if it is 0 considered one press
     state = old + 1;                    //increase state by 1
   }}
   else{                                  //if button has not been pressed
    delay(10);                            //wait 50ms
   }
   switch (state) {                      //react to button press & state
 case 1:                                 // if state is 1
//insert ONE
 /*wheel pattern */
 uint64_t one;
  memcpy_P(&one, &ONE[i], 8);

  displayImage(one);
  if (++i >= ONE_LEN ) {
    i = 0;
  }
  delay(100);


    old = state;
    break;
 case 2:
    uint64_t two;
  memcpy_P(&two, &TWO[i], 8);

  displayImage(two);
  if (++i >= TWO_LEN ) {
    i = 0;
  }
  delay(100);
    old = state;
    break;
 case 3:
    //insert THREE
 uint64_t three;
  memcpy_P(&three, &THREE[i], 8);

  displayImage(three);
  if (++i >= THREE_LEN ) {
    i = 0;
  }
  delay(100);

    old = state;
    break;

    
  default:
  //insert ZERO     
  //if state is not 1,2,3
 uint64_t zero;
  memcpy_P(&zero, &ZERO[i], 8);

  displayImage(zero);
  if (++i >= ZERO_LEN ) {
    i = 0;
  }
  delay(100);
 
                                  
    old = 0;
    break;
 
   

    
   }
}

It kind of work but I need to press the button at the end of the loop if I want to show the next and the sprites are "melting" on eachothers ... Like the state ZERO (the default one) works great and the next one is missing half its sequence, the second one burrows sprites from the first one and the third one does the same thing (there is more images on the first loop than on the rest... is it because of this?).

So I guess there is no solutions...
I tried to use state machines with button counter (they works) but when I try to print the sprites the either do not move, or glitch...

Is there a tutorial or a project using button, state machine and ledMatrix around? I'm looking for something to twitch a little bit but the things that ressemble the most my project (Counter, Arduino Dice...) aren't very compatible..

Honestly, it's better to build up your understanding better using simpler projects, than to hunt and peck through the many online tutorials, there are just as many rocks in there as there are useful morsels.

When you have the experience and insight into your own code, it's just a matter of a few minutes or hours of your time to fix issues like you have.

Thing is, your need is too specific for there to be many or any projects that you can just clone.

At the moment, can you make one animated sprite work?

Yeah I did, that was the easy part. And I learned a lot looking for this specific stuff I wanted to do... but it was maybe too much even for the level I wanted to achieve.

I learned to build a state machine and but it was more complicated than that.

I finally got a solution on a french forum. An user named Guesset wrote some code in order to achieved the state machine, it didn't work at first but he figured it out and tried to explain the problems to me. Even for him it wasn't simple to come up with such a code..

I'm posting the code if you want to check or use it :

#include <avr/pgmspace.h>
#include <LedControl.h>
#include <MD_MAX72xx.h>
 
// there is 4 sequences, SEQ_0 (default), SEQ_1, SEQ_2, SEQ_3 running on the 8x8 LEDMatrix
// with a buttonpush you can go to the next animation 
// we are using a MAX7219
// pin 12 is connected to the DataIn
// pin 11 is connected to the CLK
// pin 10 is connected to LOAD
// pin 2 is connected to switch

#define button 2
 
LedControl display = LedControl(12, 11, 10, 1);  //LedControl display = LedControl(12, 11, 10, 1);   // (DIN, CLK, CS)
 
int seq     = 0;                                 // n° of active sequences
int Img_n   = 0;                                 // n° of frame in a sequence
 
const uint64_t SEQ_0[] PROGMEM = {
   0x3c4281818181423c,  0x003c424242423c00,  0x0000182424180000,  0x0000001818000000,
   0x0000182424180000,  0x0018244242241800,  0x003c424242423c00,  0x3c4281818181423c,
   0x6681810000818166,  0x8100000000000081,  0x4281000000008142
};
 
const uint64_t SEQ_1[] PROGMEM = {
   0x3c4201010101023c,  0x3c0201010101423c,  0x1c0201010181423c,  0x0c0201018181423c,
   0x040201818181423c,  0x000281818181423c,  0x004081818181423c,  0x204080818181423c,
   0x304080808181423c,  0x384080808081423c,  0x3c4080808080423c,  0x3c4280808080403c,
   0x3c42818080804038,  0x3c42818180804030,  0x3c42818181804020,  0x3c42818181814000,
   0x3c42818181810200,  0x3c42818181010204,  0x3c4281810101020c,  0x3c4281010101021c
};
 
const uint64_t SEQ_2[] PROGMEM = {
   0x0c0e0c0800000000,  0x0002070f00000000,  0x000000000f070200,  0x00000000080c0e0c,
   0x0000000010307030,  0x00000000f0e04000,  0x0040e0f000000000,  0x3070301000000000
};
 
const uint64_t SEQ_3[] PROGMEM = {
  0x3c66c38181c3663c,  0x1e2341818182c478,  0x06193162468c9860,  0x03070a142850e0c0,
  0x0102040810204080,  0x03050a142850a0c0,  0x0e19214182849870,  0x1c22418181824438,
  0x384482818141221c,  0x708884824121110e,  0xc0a05028140a0503,  0x8040201008040201,
  0xc0e05028140a0703,  0x709884c24321190e,  0x384482818141221c
};

 
const int SeqLens[4] = { sizeof(SEQ_0)/8, sizeof(SEQ_1)/8, sizeof(SEQ_2)/8, sizeof(SEQ_3)/8 };
 
void setup() {
   pinMode (button, INPUT);                      // button set as input
   display.shutdown(0, false);
   display.setIntensity(0, 3);                   // Set the brightness to a medium values
   display.clearDisplay(0);                      // and clear the display
}
 
void displayImage(uint64_t * pImage) {
   byte *p = (byte *)pImage;   
   for(int i = 0; i < 8; ++i) {
      byte row = pgm_read_byte(p++);
      for(int j = 0; j < 8; j++) {
         display.setLed(0, i, j, bool(row & 1));
         row >>= 1;
      }
   }
}
 
void loop() {
   if(digitalRead(button) == 1) {                // button pushed?
      do {                                       // repeat
         delay(20);                              // wait 20 ms
      } while(digitalRead(button) == 1);         // animation freeze during the buttonpush
      seq = (seq + 1) & 3;                       // n° of the next animation : 0, 1, 2, 3, 0, 1, 2... 
      Img_n = 0;                                 // new sequence begins at 0
   }
 
   uint64_t *pImg;                                  
   switch(seq) {                                 // go on the correct frame
      case 0: pImg = &SEQ_0[Img_n];       break;
      case 1: pImg = &SEQ_1[Img_n];       break;
      case 2: pImg = &SEQ_2[Img_n];       break;
      case 3: pImg = &SEQ_3[Img_n];    
   }
   displayImage(pImg);                           // read the Img_n of the active sequence
   if(++Img_n >= SeqLens[seq]) Img_n = 0;        //go to the next sequence (loop)
   delay(100);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.