Saturday, May 11, 2013

Here is our first LED Sketch

First, I have to admit, I haven't figured out how to mix the colors. When I try to do an analogWrite(), I get nothing. So here's a pic of the display.
And, as promised, here is the code. Remember, this will only work on a Mega and Due. An Uno will not have enough have enough pins.

int colsr[]={9,10,11,12,13,14,15,16};
int colsg[]={28,27,26,25,24,23,22,21};
int colsb[]={1,2,3,4,5,6,7,8};
int rows[]={17,18,19,20,29,30,31,32};


void setup() {
  // put your setup code here, to run once:
   for(int pin=1;pin<=32;pin++){
     pinMode(pin,OUTPUT);
   }
   for(int idx=1;idx<=9;idx++){
     digitalWrite(colsr[idx-1],HIGH);
     digitalWrite(colsg[idx-1],HIGH);
     digitalWrite(colsb[idx-1],HIGH);
     digitalWrite(rows[idx-1],LOW);
   }
}

void loop() {
  // put your main code here, to run repeatedly: 
/*  for(int x=0;x<8;x++){
    digitalWrite(colsg[x],LOW);
//    digitalWrite(colsr[x],LOW);
    digitalWrite(rows[0],HIGH);
  }
*/  
  for(int st=0;st<8;st++){
    digitalWrite(colsg[st],LOW);
    digitalWrite(rows[st],HIGH);
//    delay(500);
    digitalWrite(colsg[st],HIGH);
    digitalWrite(rows[st],LOW);
//    delay(500);
    digitalWrite(colsb[7-st],LOW);
    digitalWrite(rows[st],HIGH);
//    delay(500);
    digitalWrite(colsb[7-st],HIGH);
    digitalWrite(rows[st],LOW);
  }
  for(int st=0;st<3;st++){
    digitalWrite(colsr[st+1],LOW);
    digitalWrite(colsr[st+4],LOW);
    digitalWrite(rows[0],HIGH);
    digitalWrite(rows[7],HIGH);
    digitalWrite(colsr[st+1],HIGH);
    digitalWrite(colsr[st+4],HIGH);
    digitalWrite(rows[0],LOW);
    digitalWrite(rows[7],LOW);
  }
  for(int st=0;st<4;st++){
    digitalWrite(colsr[st+2],LOW);
    digitalWrite(rows[1],HIGH);
    digitalWrite(rows[6],HIGH);
    digitalWrite(colsr[st+2],HIGH);
    digitalWrite(rows[1],LOW);
    digitalWrite(rows[6],LOW);

//    digitalWrite(colsb[st+2],LOW);
//    digitalWrite(rows[1],HIGH);
//    digitalWrite(colsb[st+2],HIGH);
//    digitalWrite(rows[1],LOW);
  }
  for(int st=3;st<5;st++){
    digitalWrite(colsr[st],LOW);
    digitalWrite(rows[2],HIGH);
    digitalWrite(rows[5],HIGH);
    digitalWrite(colsr[st],HIGH);
    digitalWrite(rows[2],LOW);
    digitalWrite(rows[5],LOW);
  }
  for(int st=1;st<4;st++){
    digitalWrite(colsr[0],LOW);
    digitalWrite(colsr[7],LOW);
    digitalWrite(rows[st],HIGH);
    digitalWrite(rows[7-st],HIGH);
    digitalWrite(colsr[0],HIGH);
    digitalWrite(colsr[7],HIGH);
    digitalWrite(rows[st],LOW);
    digitalWrite(rows[7-st],LOW);
  }
  for(int st=2;st<4;st++){
    digitalWrite(colsr[1],LOW);
    digitalWrite(colsr[6],LOW);
    digitalWrite(rows[st],HIGH);
    digitalWrite(rows[7-st],HIGH);
    digitalWrite(colsr[1],HIGH);
    digitalWrite(colsr[6],HIGH);
    digitalWrite(rows[st],LOW);
    digitalWrite(rows[7-st],LOW);
  }
  for(int st=3;st<5;st++){
    digitalWrite(colsr[2],LOW);
    digitalWrite(colsr[5],LOW);
    digitalWrite(rows[st],HIGH);
    digitalWrite(colsr[2],HIGH);
    digitalWrite(colsr[5],HIGH);
    digitalWrite(rows[st],LOW);
  }
}

Not the most elegant, but it works.

No comments:

Post a Comment