interrupt doesn't work

hi everyone, i'm trying to do a simple programm for a 3x3 led matrix, there is an function that converts a matrix(led[3][3]) to leds(open or close), then in the loop there is a switch that depending on a variable(tipo), and a interrupt function that in the case that the button is pressed changes the variable.

// programma per controllo di una matrice 3x3
// contatori
int tipo=1, k=50;
//matrice   0=spento 1=accesso
int led[3][3] = { {0,0,0}, 
                  {0,0,0}, 
                  {0,0,0}
                };
                
// inizializzazione 
void setup()
{
  // colonne
  pinMode (3, OUTPUT);
  pinMode (5, OUTPUT);
  pinMode (6, OUTPUT);
  // righe
  pinMode (9, OUTPUT);
  pinMode (10, OUTPUT);
  pinMode (11, OUTPUT);
  //imposizione pin
 digitalWrite(3, HIGH);
 digitalWrite(5, HIGH);
 digitalWrite(6, HIGH);
 digitalWrite(9, LOW);
 digitalWrite(10, LOW);
 digitalWrite(11, LOW);
  //inizializzazione interrupt
  pinMode (2, INPUT);
  digitalWrite (2, HIGH);
  attachInterrupt(0,cambio,FALLING);
  
}

void loop()
{
  switch (tipo)
  {
    case (1):
    tipo1();
    break;
    case (2):
    tipo2();
    break;
    
    default:
    delay(1);
  }
    
    
    
    applicazione();
  
}

void tipo1()
{
  led[1][1]=1;
  led[2][1]=1;
  led[3][1]=1;
  led[1][2]=1;
  led[2][2]=1;
  led[3][2]=1;
  led[1][3]=1;
  led[2][3]=1;
  led[3][3]=1;
  

}

void tipo2()
{
  led[1][1]=1;
  led[2][1]=1;
  led[3][1]=1;
  led[1][2]=0;
  led[2][2]=0;
  led[3][2]=0;
  led[1][3]=1;
  led[2][3]=1;
  led[3][3]=1;
  
}





//interrupt
void cambio ()
{
  tipo++;
  if (tipo == 3)
  {
    tipo=1;
  }
  
}








// funzione che permette di applicare la combinazione di acceso-spento dei led
void applicazione()
{
for ( int i=1;i<k;i++)
  digitalWrite(3, LOW);
  digitalWrite(9, led[1][1]);
  digitalWrite(10, led[2][1]);  
  digitalWrite(11, led[3][1]);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(3, HIGH);

  digitalWrite(5, LOW);
  digitalWrite(9, led[1][2]);
  digitalWrite(10, led[2][2]);
  digitalWrite(11, led[3][2]); 
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(5,HIGH);

  digitalWrite(6, LOW);
  digitalWrite(9, led[1][3]);
  digitalWrite(10, led[2][3]);
  digitalWrite(11, led[3][3]);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(6,HIGH);  
}

so the last function is for applicating the matrix to leds, and it does work, the switch works when i start the arduino opening all leds(1=open 0=closed), for now there are only two functions, one that opens all leds, one that closes the middle line, but when i connect the pin 2(interrupt 0) to ground it happens something strange, it can close all leds permanently, or it opens some random leds, but 90% of the times it does nothing....
what's wrong with it??

"tipo" should really be "volatile".
What does the serial debug output look like?
Oh, you'll be wanting to fix that then.

You need to de-bounce the switch!

Mark

still nothing,

i put volatile int tipo=1;
but it's the same

How does the serial output look?

so i added the serial.begin
and inside the interrupt Serial.println("interrupt");

it doesn't give out anything

Your array indices are all off by one. When you declare an array of size 3, the elements are 0, 1, 2 not 1, 2, 3.

well strangely it does work... but actually even if it started from 0 it would still get 0, 1, 2, 3 so four elements instead of 3, but it doesn't explain why the interrupt doesn't work...

euge95:
and inside the interrupt Serial.println("interrupt");

That's because Serial.prints don't work inside interrupts.

well strangely by sheer good fortune it does appear to work.

ok, i'm so stupid,
int led[4][4] = { {0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}
};

changed to this and now works....
good job peter!

AWOL:

well strangely by sheer good fortune it does appear to work.

yes ahahaha