IAN LANG ELECTRONICS

You can use the code in the last chapter with a modification or two to make more complex animations. This code contracts and expands a box:

 

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

int pauseDelay = 1;    //the number of milliseconds to display each scanned line

//Pin Definitions

int rowA[] = {9,8,7,6,5,4,3,2};          

int colA[] = {17,16,15,14,13,12,11,10};  

byte data[] = {0,0,0,0,0,0,0,0};    

 

void setup(){

  for(int i = 0; i <8; i++){  //Set the 16 pins used to control the array as OUTPUTs

    pinMode(rowA[i], OUTPUT);

    pinMode(colA[i], OUTPUT);}}

 

void loop(){  

  data[0] = 255; //row 1s bit mask (1 LED is on 0 LED is off)

  data[1] = 129; //row 1s bit mask (1 LED is on 0 LED is off)

  data[2] = 129; //row 1s bit mask (1 LED is on 0 LED is off)

  data[3] = 129; //row 1s bit mask (1 LED is on 0 LED is off)

  data[4] = 129; //row 1s bit mask (1 LED is on 0 LED is off)

  data[5] = 129; //row 1s bit mask (1 LED is on 0 LED is off)

  data[6] = 129; //row 1s bit mask (1 LED is on 0 LED is off)  

  data[7] = 255; //row 1s bit mask (1 LED is on 0 LED is off)  

  showSprite(speed);

  data[0] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

  data[1] = 126; //row 1s bit mask (1 LED is on 0 LED is off)

  data[2] = 66; //row 1s bit mask (1 LED is on 0 LED is off)

  data[3] = 66; //row 1s bit mask (1 LED is on 0 LED is off)

  data[4] = 66; //row 1s bit mask (1 LED is on 0 LED is off)

  data[5] = 66; //row 1s bit mask (1 LED is on 0 LED is off)

  data[6] = 126; //row 1s bit mask (1 LED is on 0 LED is off)  

  data[7] = 0; //row 1s bit mask (1 LED is on 0 LED is off)  

  showSprite(speed);  

data[0] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

  data[1] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

  data[2] = 60; //row 1s bit mask (1 LED is on 0 LED is off)

  data[3] = 36; //row 1s bit mask (1 LED is on 0 LED is off)

  data[4] = 36; //row 1s bit mask (1 LED is on 0 LED is off)

  data[5] = 60; //row 1s bit mask (1 LED is on 0 LED is off)

  data[6] = 0; //row 1s bit mask (1 LED is on 0 LED is off)  

  data[7] = 0; //row 1s bit mask (1 LED is on 0 LED is off)  

  showSprite(speed);  

  data[0] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

  data[1] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

  data[2] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

  data[3] = 24; //row 1s bit mask (1 LED is on 0 LED is off)

  data[4] = 24; //row 1s bit mask (1 LED is on 0 LED is off)

  data[5] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

  data[6] = 0; //row 1s bit mask (1 LED is on 0 LED is off)  

  data[7] = 0; //row 1s bit mask (1 LED is on 0 LED is off)  

  showSprite(speed);

  data[0] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

  data[1] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

  data[2] = 60; //row 1s bit mask (1 LED is on 0 LED is off)

  data[3] = 36; //row 1s bit mask (1 LED is on 0 LED is off)

  data[4] = 36; //row 1s bit mask (1 LED is on 0 LED is off)

  data[5] = 60; //row 1s bit mask (1 LED is on 0 LED is off)

  data[6] = 0; //row 1s bit mask (1 LED is on 0 LED is off)  

  data[7] = 0; //row 1s bit mask (1 LED is on 0 LED is off)  

  showSprite(speed);

    data[0] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

  data[1] = 126; //row 1s bit mask (1 LED is on 0 LED is off)

  data[2] = 66; //row 1s bit mask (1 LED is on 0 LED is off)

  data[3] = 66; //row 1s bit mask (1 LED is on 0 LED is off)

  data[4] = 66; //row 1s bit mask (1 LED is on 0 LED is off)

  data[5] = 66; //row 1s bit mask (1 LED is on 0 LED is off)

  data[6] = 126; //row 1s bit mask (1 LED is on 0 LED is off)  

  data[7] = 0; //row 1s bit mask (1 LED is on 0 LED is off)  

  showSprite(speed);

   data[0] = 255; //row 1s bit mask (1 LED is on 0 LED is off)

  data[1] = 129; //row 1s bit mask (1 LED is on 0 LED is off)

  data[2] = 129; //row 1s bit mask (1 LED is on 0 LED is off)

  data[3] = 129; //row 1s bit mask (1 LED is on 0 LED is off)

  data[4] = 129; //row 1s bit mask (1 LED is on 0 LED is off)

  data[5] = 129; //row 1s bit mask (1 LED is on 0 LED is off)

  data[6] = 129; //row 1s bit mask (1 LED is on 0 LED is off)  

  data[7] = 255; //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 < 8; column++){            //iterate through each column

   for(int i = 0; i < 8; i++){                          

       digitalWrite(rowA[i], LOW);}                    //turn off all row pins  

   for(int i = 0; i < 8; 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 < 8; 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(1);}}  data[0] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

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

 

}

 

This one causes frankly bizzarre patterns:

 

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

int pauseDelay = 1;    //the number of milliseconds to display each scanned line

//Pin Definitions

int rowA[] = {9,8,7,6,5,4,3,2};          

int colA[] = {17,16,15,14,13,12,11,10};  

byte data[] = {0,0,0,0,0,0,0,0};    

 

void setup(){

  for(int i = 0; i <8; i++){  //Set the 16 pins used to control the array as OUTPUTs

    pinMode(rowA[i], OUTPUT);

    pinMode(colA[i], OUTPUT);}}

 

void loop(){

 for(int t=0;t<8;t++){

   data[t]=random(2,255);}

  showSprite(speed);

}

 

void showSprite(int speed2){

 for(int iii = 0; iii < speed2; iii++){                 //show the current frame speed2 times

  for(int column = 0; column < 8; column++){            //iterate through each column

   for(int i = 0; i < 8; i++){                          

       digitalWrite(rowA[i], LOW);}                    //turn off all row pins  

   for(int i = 0; i < 8; 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 < 8; 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(1);}}  data[0] = 0; //row 1s bit mask (1 LED is on 0 LED is off)

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

 

}

 

You may deduce from the above that it is possible to define a bitmap for the matrix to display and you would be quite right. Here's a sketch that produces a smily face:

 

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

int pauseDelay = 1;    //the number of milliseconds to display each scanned line

//Pin Definitions

int rowA[] = {9,8,7,6,5,4,3,2};          

int colA[] = {17,16,15,14,13,12,11,10};  

byte data[] = {0,0,0,0,0,0,0,0};    

 

void setup()

{

  Serial.begin(9600);         //Open the Serial port for debugging

  for(int i = 0; i <8; i++){  //Set the 16 pins used to control the array as OUTPUTs

    pinMode(rowA[i], OUTPUT);

    pinMode(colA[i], OUTPUT);

  }

}

 

//repeats  

void loop(){

  data[0] = B00000000; //row 1s bit mask (1 LED is on 0 LED is off)

  data[1] = B01100110; //row 1s bit mask (1 LED is on 0 LED is off)

  data[2] = B01100110; //row 1s bit mask (1 LED is on 0 LED is off)

  data[3] = B00000000; //row 1s bit mask (1 LED is on 0 LED is off)

  data[4] = B10000001; //row 1s bit mask (1 LED is on 0 LED is off)

  data[5] = B01000010; //row 1s bit mask (1 LED is on 0 LED is off)

  data[6] = B00111100; //row 1s bit mask (1 LED is on 0 LED is off)  

  data[7] = B00000000; //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 < 8; column++){            //iterate through each column

   for(int i = 0; i < 8; i++){                          

       digitalWrite(rowA[i], LOW);}                    //turn off all row pins  

   for(int i = 0; i < 8; 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 < 8; 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(1);}}}                       //leave the column on for pauseDelay microseconds (too high a delay causes flicker)

 

 

In the first two sketches we used decimal to give the bitmap mask and this is perfectly permissible and may even be preferable in some cases. Over the page, let's make smily sad.

8x8 LED Matrix-More Animations

Go Back More >