Loop through LED matrix display LEDs with Millis?

Hello,
I have 5x5 LED matrix display and I want to turn each LED off one by one after specific delay, like a countdown timer.

void loop() {
timer(m5_state, m5_timer);
}

void timer (uint16_t m5_state, uint16_t m5_timer) {
    if (m5_state == 1) {
      timer_fill(); // fill all 25 LEDs 
      uint16_t my_delay = (m5_timer * 60 * 1000 / 25);

      // Loop through 25 LEDs and turn them off one by one
      for (int i = 0; i <= 24; i++) {
      	M5.dis.drawpix(i, 0x000000);
      	M5.update();
      	delay(my_delay);
      }            
    }
   else if (m5_state == 0) {
      M5.dis.clear();
    }
        
} // timer END

I would like to stop timer function by changing m5_state variable anytime I wanted but delay is blocking so I would like to use Millis instead. How can I do that? I read some tutorials but I am even more confused now.

Thank you.

Hi,
Can you please post your complete code?

Have you got a schematic of your project to work you code with?

Tom.. :slight_smile:

#define Interval 1000

void
loop (void)
{
    static unsigned long msecLst  = 0
           unsigned long msec     = millis();

    if (msec - msecLst > Interval) {
        msecLst = msec;
        // do something
    }
}

TomGeorge:
Hi,
Can you please post your complete code?

Have you got a schematic of your project to work you code with?

Tom.. :slight_smile:

Hello,
there is no schematics. I am using M5 Atom Matrix by M5Stack.

Here is whole code:

#include "M5Atom.h"
//GRB
void setup() {
  M5.begin(true, false, true);
  M5.dis.setBrightness(20);
  delay(50);
} // setup END


void loop() {
  timer(1, 1);
} // loop END

void timer (uint16_t m5_state, uint16_t m5_timer) {
    if (m5_state == 1) {
      timer_fill(); // fill all 25 LEDs 
      uint16_t my_delay = (m5_timer * 60 * 1000 / 25);

      // Loop through 25 LEDs and turn them off one by one
      for (int i = 0; i <= 24; i++) {
        M5.dis.drawpix(i, 0x000000);
        M5.update();
        delay(my_delay);
      }            
    }
   else if (m5_state == 0) {
      M5.dis.clear();
    }
        
} // timer END

void timer_fill() {
      uint16_t pauza = 20;
      for ( int i = 0; i <= 4; i++) { M5.dis.drawpix(i, 0, 0x0000ff); delay(pauza); M5.update(); }  
      for ( int i = 1; i <= 4; i++) { M5.dis.drawpix(4, i, 0x0000ff); delay(pauza); M5.update(); }
      for ( int i = 3; i >= 0; i--) { M5.dis.drawpix(i, 4, 0x0000ff); delay(pauza); M5.update(); }
      for ( int i = 3; i >= 1; i--) { M5.dis.drawpix(0, i, 0x0000ff); delay(pauza); M5.update(); }
      for ( int i = 1; i <= 3; i++) { M5.dis.drawpix(i, 1, 0x0000ff); delay(pauza); M5.update(); }
      for ( int i = 2; i <= 3; i++) { M5.dis.drawpix(3, i, 0x0000ff); delay(pauza); M5.update(); }
      for ( int i = 2; i >= 1; i--) { M5.dis.drawpix(i, 3, 0x0000ff); delay(pauza); M5.update(); }
      for ( int i = 1; i <= 2; i++) { M5.dis.drawpix(i, 2, 0x0000ff); delay(pauza); M5.update(); }  
      delay(1000);
  }

gcjr:

#define Interval 1000

void
loop (void)
{
    static unsigned long msecLst  = 0
          unsigned long msec    = millis();

if (msec - msecLst > Interval) {
        msecLst = msec;
        // do something
    }
}

gcjr:

#define Interval 1000

void
loop (void)
{
    static unsigned long msecLst  = 0
          unsigned long msec    = millis();

if (msec - msecLst > Interval) {
        msecLst = msec;
        // do something
    }
}

yes, I found this... unfortunately I have no idea how to implement that to my code... yet.

Thanks anyway.