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??