LedControl Problem

I have a MAX7219 wired up to a Mega and it's works, but after I let the pin go low it takes about 3 seconds for the display to turn off, like it keep looping though the code after the pin goes low. Here is my code, any ideas?

#include "LedControl.h"

LedControl lc=LedControl(44,46,45,1);

void setup() {
 
  lc.shutdown(0,false);
  lc.setIntensity(0,1);
  lc.clearDisplay(0);
}

void light() {
  
  byte z[8]={B10101010,
             B01010101,
             B10101010,
             B01010101,
             B10101010,
             B01010101,
             B10101010,
             B01010101};
  
  lc.setRow(0,0,z[0]);
  lc.setRow(0,1,z[1]);
  lc.setRow(0,2,z[2]);
  lc.setRow(0,3,z[3]);
  lc.setRow(0,4,z[4]);
  lc.setRow(0,5,z[5]);
  lc.setRow(0,6,z[6]);
  lc.setRow(0,7,z[7]);

}

void loop() { 
 
  
  if (digitalRead(3)==HIGH)
    {
      light();
    }
  else
    {
    lc.clearDisplay(0);
    }
    
}

Do you have a pull down resistor on the pin to make it read low?

Alternately, use the internal pullup and take it low to make it active.

Where is the equivalent of this?
pinMode (3, INPUT);
add this after it
digitalWrite (3, HIGH); // enable internal pullup.

I did have a pull up resistor, also I think it would be more random if that was a problem (could be wrong) but it exactly the same amount of time every time. Kind of at a loss on this one.

You are looking for a high to enable it - so you need a pull DOWN resistor to bring it back low. This must be an external part, there are no internal pulldowns.
Or rewire like I suggested, internal pullup will read as high to disable, momentary switch to ground to enable.
You may need to look into debounce the switch also, there is stuff in the playground for doing, otherwise there is mechanical bounce as the switch makes contact, and the code may see that as series of 10101011111 or 0101010101000 before settling.

I've not used LedControl.h.

I'm sorry I ment pull down, I have a 10k resistor from the pin to ground. It's not a matter of debounce as I'm not using a switch, just a jumper wire on a breadboard. If I remove the jumper to VCC it will take about 3 seconds to turn off the display. I've added a line to clear the display when ever the routine is called (See code) and I can see the display blink very rapidly, so it must be calling the routine even after the pin has gone low when the jumper is removed.

#include "LedControl.h"

LedControl lc=LedControl(44,46,45,1);

void setup() {
 
  lc.shutdown(0,false);
  lc.setIntensity(0,1);
  lc.clearDisplay(0);
}

void light() {
  
  byte z[8]={B10101010,
             B01010101,
             B10101010,
             B01010101,
             B10101010,
             B01010101,
             B10101010,
             B01010101};
  
  lc.setRow(0,0,z[0]);
  lc.setRow(0,1,z[1]);
  lc.setRow(0,2,z[2]);
  lc.setRow(0,3,z[3]);
  lc.setRow(0,4,z[4]);
  lc.setRow(0,5,z[5]);
  lc.setRow(0,6,z[6]);
  lc.setRow(0,7,z[7]);
  lc.clearDisplay(0);
}

void loop() { 
 
  
  if (digitalRead(3)==HIGH)
    {
      light();
    }
  else
    {
    lc.clearDisplay(0);
    }
    
}

Anyone else have an idea?

Anyone else have an idea?

I would add Serial.print() calls to light(), and print the time that the function gets called, and when it ends. If the time that the function takes to execute matches the delay turning off, then the problem is that the "turn off" instruction (pulling the wire) is not detected immediately.