Arduino and matrix with 4x4 LED

Hello, i have on my arduino clone with internal oscillator matrix display with 4x4 LED. Its work great when i set one pin as HIGH and another as LOW, so i can light up one LED. My problem is i want light up two leds and after second another two leds. When i trying this, i set reset on all pin so the culumns is LOW and rows is HIGH, after that i set one pin as HIGH and second as LOW, wait a while, set reset and another one pin as HIGH and second as LOW. After that i set delay 1 sec and repeat the proces on another pair of LED. So when i run it, first LED blink one very fast and second wait one second on another pair. How i can switching before first two LED while i wait second to light up second pair? I read something about internal interrupts, but i cannot find how to run on Arduino. Thanks for answer.

You need to post your code. It sounds like you are using delay, look at the blink without delay examples. What value resistor do you have? The pins will only handle 30mA.

int delayReset = 200;
int delayMillis = 100;

//**********************************************************************************************************
void resetLED(){
  //column:
  digitalWrite(5, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(12, LOW);
  //row:
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
}
//**********************************************************************************************************
void one(){
  digitalWrite(9, HIGH);
  digitalWrite(6, LOW);
  delayMicroseconds(delayMillis);
}
//**********************************************************************************************************
void two(){
  digitalWrite(9, HIGH);
  digitalWrite(11, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void three(){
  digitalWrite(9, HIGH);
  digitalWrite(7, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void four(){
  digitalWrite(9, HIGH);
  digitalWrite(10, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void five(){
  digitalWrite(8, HIGH);
  digitalWrite(10, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void six(){
  digitalWrite(8, HIGH);
  digitalWrite(7, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void seven(){
  digitalWrite(8, HIGH);
  digitalWrite(11, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void eight(){
  digitalWrite(8, HIGH);
  digitalWrite(6, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void nine(){
  digitalWrite(12, HIGH);
  digitalWrite(6, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void ten(){
  digitalWrite(12, HIGH);
  digitalWrite(11, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void eleven(){
  digitalWrite(12, HIGH);
  digitalWrite(7, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void twelve(){
  digitalWrite(12, HIGH);
  digitalWrite(10, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void thirteen(){
  digitalWrite(5, HIGH);
  digitalWrite(10, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void fourteen(){
  digitalWrite(5, HIGH);
  digitalWrite(7, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void fifteen(){
  digitalWrite(5, HIGH);
  digitalWrite(11, LOW);
  delayMicroseconds(delayMillis);
}
//********************************************************************************************************** 
void sixteen(){
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
  delayMicroseconds(delayMillis);
}
//**********************************************************************************************************
void full(){
  resetLED();
  one();
  resetLED();
  two();
  resetLED();
  three();
  resetLED();
  four();
  resetLED();
  five();
  resetLED();
  six();
  resetLED();
  seven();
  resetLED();
  eight();
  resetLED();
  nine();
  resetLED();
  ten();
  resetLED();
  eleven();
  resetLED();
  twelve();
  resetLED();
  thirteen();
  resetLED();
  fourteen();
  resetLED();
  fifteen();
  resetLED();
  sixteen();
}
//**********************************************************************************************************
void scan(){
  //1. fáze:
  resetLED();
  four();
  delayMicroseconds(delayMillis);
  five();
  delay(delayReset);
  //2. fáze:
  resetLED();
  three();
  delayMicroseconds(delayMillis);
  six();
  delay(delayReset);
  //3. fáze:
  resetLED();
  two();
  delayMicroseconds(delayMillis);
  seven();
  delay(delayReset);
  //*4. fáze:
  resetLED();
  one();
  delayMicroseconds(delayMillis);
  eight();
  delay(delayReset);
  //*5. fáze:
  resetLED();
  nine();
  delayMicroseconds(delayMillis);
  sixteen();
  delay(delayReset);
  //*6. fáze:
  resetLED();
  ten();
  delayMicroseconds(delayMillis);
  fifteen();
  delay(delayReset);
  //*7. fáze:
  resetLED();
  eleven();
  delayMicroseconds(delayMillis);
  fourteen();
  delay(delayReset);
  //*8. fáze:
  resetLED();
  twelve();
  delayMicroseconds(delayMillis);
  thirteen();
  delay(delayReset);
}
//**********************************************************************************************************
void setup()
{
  //*nastavení LED jako výstup? pro jejich ovládání
  byte i;
  for(i = 5; i < 13; i++){
    pinMode(i, OUTPUT);
  }
}
//**********************************************************************************************************
void loop(){

  //**********************************************************************************************************
scan();

I try use example blink without delay on it.

OK thanks for that but I have no idea what you are trying to do with that code. The basic idea when driving a matrix is to have some memory containing the bits representing if an LED is on or off and then output that memory to the hardware. Assuming you have the anodes connected to the rows and the cathodes (through a resistor) to the column. This involves setting one row high and all the others low. Then on each of the column outputs putting a zero on all the outputs where you want the LED to be on, on that row. Then put all those column outputs high, put the high row output to low and select the next row by putting the next row output high and repeating. You do this until all the rows have been scanned, then you start over again. Look at this project for an example of how to drive a matrix. http://www.thebox.myzen.co.uk/Hardware/Econo_Monome.html

I now have this code:

/*
 |***12****| |****5****|
 |6|11|7|10| |10|7|11|6|
 |6|11|7|10| |10|7|11|6|
 |****9****| |****8****|
 */
int row[5] = {5, 8, 9, 12};
int col[5] = {6, 7, 10, 11};

//**********************************************************************************************************
void setup()
{
  for(byte i = 5; i < 13; i++){
    pinMode(i, OUTPUT);
  }

  for (int i = 1; i <= 4; i++) {
    digitalWrite(row[i - 1], LOW);
  }

  for (int i = 1; i <= 4; i++) {
    digitalWrite(col[i - 1], LOW);
  }
}
//**********************************************************************************************************
void loop(){
}

So after that i can put one row high, three cols high and let one low which i want to light. I cannot let all cols low because i light completly one row and without enought current. After that i reset all pins to default and repeat the process. How can lightup two leds? And after second another two leds? I still dont understand how can i do.

cannot let all cols low because i light completly one row and without enought current.

You need some form of external drivers to do this, like a Darlington driver. Have you looked at the code in the link?

Yes, i can use some tranzistors, but why? When runs code too fast so switching between two leds is imperceptible = persistence of vision. And yes, i look to example but not full understand what they do.

But i found some code on oomlout.com and after few small updates its run great on my board. Here and example of code:

/*
 |***12****| |****5****|
 |6|11|7|10| |10|7|11|6|
 |6|11|7|10| |10|7|11|6|
 |****9****| |****8****|
 */
int colA[] = {6, 11, 7, 10};
int rowA[] = {12, 5, 9, 8};

int speed = 5; //the delay time in milliseconds

int pauseDelay = 1;    //the number of milliseconds to display each scanned line
byte data[] = {0,0,0,0};
//**********************************************************************************************************
void setup()
{
  for(byte i = 5; i < 13; i++){
    pinMode(i, OUTPUT);
  }
}
//**********************************************************************************************************
void loop(){
  data[0] = B0101; //row 1s bit mask (1 LED is on 0 LED is off)
  data[1] = B0101; //row 1s bit mask (1 LED is on 0 LED is off)
  data[2] = B0101; //row 1s bit mask (1 LED is on 0 LED is off)
  data[3] = B0101; //row 1s bit mask (1 LED is on 0 LED is off)
  showSprite(speed);
}

void showSprite(int speed2){
 for(int iii = 0; iii < speed2; iii++){                 //show the current frame speed2 times
  for(int column = 0; column < 4; column++){            //iterate through each column
   for(int i = 0; i < 4; i++){                          
       digitalWrite(rowA[i], LOW);                      //turn off all row pins  
   }
   for(int i = 0; i < 4; i++){ //Set only the one pin
     if(i == column){     digitalWrite(colA[i], LOW);}  //turns the current row on
     else{                digitalWrite(colA[i], HIGH); }//turns the rest of the rows off
   }

   for(int row = 0; row < 4; row++){                    //iterate through each pixel in the current column
    int bit = (data[column] >> row) & 1;
    if(bit == 1){ 
       digitalWrite(rowA[row], HIGH);                   //if the bit in the data array is set turn the LED on
    }

   }
   delay(pauseDelay);                       //leave the column on for pauseDelay microseconds (too high a delay causes flicker)
  } 
 }
 }

Works good and i love that solution to have HIGH only one ROW and COL.

Yes, i can use some tranzistors, but why?

You have to add resistors to limit the current through each LED to less than 10mA or use transistors to get more current for the LEDs. If you try and run more current from the pins than this then your arduino will be damaged.

http://www.thebox.myzen.co.uk/Tutorial/LEDs.html

Yes, yes a know this fact. But i dont have enought place to use tranzistors. So i must use presistence of vision and fast switch between LEDs. My problem is only in programming, not in electrical things :)

No presistence of vision and fast switch between LEDs does NOT remove the need for limiting the current to safe levels. You overload the arduino output and damage it.

Why? When is only one LED active, and arduino maximal ratings is 40mA. Its as a normal example of drive a LED, nothing special.

New code updates:

/*
 |***12****| |****5****|
 |6|11|7|10| |10|7|11|6|
 |6|11|7|10| |10|7|11|6|
 |****9****| |****8****|
 */
int colA[] = {
  6, 11, 7, 10};
int rowA[] = {
  12, 5, 9, 8};

int speed = 5; //the delay time in milliseconds

int pauseDelay = 1;    //the number of milliseconds to display each scanned line
byte data[] = {
  0,0,0,0};
//**********************************************************************************************************
void setup()
{
  for(byte i = 5; i < 13; i++){
    pinMode(i, OUTPUT);
  }
}
//**********************************************************************************************************
void loop(){
  long x = millis();
  if (x < 1000 || x > 3000){
full();
  }
  else {
    empty();
  }
}

void full(){
  data[0] = B1111;
  data[1] = B1111;
  data[2] = B1111;
  data[3] = B1111;
  showSprite(speed);
}
  
  void empty(){
  data[0] = B0000;
  data[1] = B0000;
  data[2] = B0000;
  data[3] = B0000;
  showSprite(speed);
}
  
  
void showSprite(int speed2){
  for(int iii = 0; iii < speed2; iii++){                 //show the current frame speed2 times
    for(int column = 0; column < 4; column++){            //iterate through each column
      for(int i = 0; i < 4; i++){                          
        digitalWrite(rowA[i], LOW);                      //turn off all row pins  
      }
      for(int i = 0; i < 4; i++){ //Set only the one pin
        if(i == column){     
          digitalWrite(colA[i], LOW);
        }  //turns the current row on
        else{                
          digitalWrite(colA[i], HIGH); 
        }//turns the rest of the rows off
      }

      for(int row = 0; row < 4; row++){                    //iterate through each pixel in the current column
        int bit = (data[column] >> row) & 1;
        if(bit == 1){ 
          digitalWrite(rowA[row], HIGH);                   //if the bit in the data array is set turn the LED on
        }

      }
      delay(pauseDelay);                       //leave the column on for pauseDelay microseconds (too high a delay causes flicker)
      for(int i = 0; i < 4; i++){                          
        digitalWrite(rowA[i], LOW);                      //turn off all row pins  
      }
    } 
  }
}

Add turn off on all row pins and now after power on light all for one second, then light off and then light on. This is path what i want, im on good way to complete.

When is only one LED active

OK but that is not the way a matrix works, you need 4 times the refresh rate and the LEDs will be 4 times less bright.

and arduino maximal ratings is 40mA.

No 40mA is the point where damage starts to occur, try and keep the current below 30mA.

Yes, arduino must refresh display 4x rather than normaly. But its very fast, so i used little delay to compensate brightness and its now full. With matrix 4x4 is speed acceptable and for eye stable without flickering. And in datasheet is 40mA marked as maximum rating. I agree its ever better was bellow maximal rating ofcourse. I can measure the speed of refreshing one LED on oscilloscope when its all LEDs lit.

And in datasheet is 40mA marked as maximum rating.

I think you underestimate what that means it says:-

Stresses beyond those listed under “Absolute Maximum Ratings” may cause permanent damage to the device. This is a stress rating only and functional operation of the device at these or other conditions beyond those indicated in the operational sections of this specification is not implied. Exposure to absolute maximum rating conditions for extended periods may affect device reliability.

Thank you, u saved my chip. I have fixed in my mind that is a maximum but acceptable operating parameters. Thanks!