Skip to content

Instantly share code, notes, and snippets.

@stefreak
Last active December 31, 2015 13:49
Show Gist options
  • Save stefreak/7995899 to your computer and use it in GitHub Desktop.
Save stefreak/7995899 to your computer and use it in GitHub Desktop.
arduino fritzing led matrix shades of gray
int rowPins[8] = {
9,22,18,12,15,11,7,8}; // matrix rows connected to the Arduino
int colPins[8] = {
13,8,17,10,5,16,19,14}; // matrix columns connected to the Arduino
int image[8][8]={ // the image displayed on the matrix : 1 = LED on, 0 = LED off
{1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 },
{2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 },
{3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 },
{4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 },
{6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 },
{8 , 8 , 8 , 8 , 8 , 8 , 8 , 8 },
{10 , 10 , 10 , 10 , 10 , 10 , 10 , 10 },
{12 , 12 , 12 , 12 , 12 , 12 , 12 , 12 },
};
void setup(){
for (int i=0; i<8; i++){ // all pins are declared as outputs
pinMode(rowPins[i],OUTPUT);
pinMode(colPins[i],OUTPUT);
}
}
unsigned int counter;
unsigned int ticker;
int mode = 0;
void loop(){
loop:
counter++;
if (counter % 100 == 0) {
tick();
}
for (int y=0; y<8; y++){ // rowwise
for (int x=0; x<8; x++){ // check all entries of the array from left to right
digitalWrite(colPins[x], (counter % image[x][y] == 0) ? HIGH : LOW); // switch on column pin
}
int pin = rowPins[y];
digitalWrite(pin,LOW); // switch the row pin to LOW (because it is the cathode of the LED; LOW means ON)
delayMicroseconds(60); // stop the program for 100 milliseconds
digitalWrite(pin,HIGH); // switch the row pin to HIGH (this means OFF)
}
goto loop;
}
void tick() {
for (int y=0; y<8; y++){ // rowwise
for (int x=0; x<8; x++){ // check all entries of the array from left to right
switch (mode) {
case 0: image[x][y] = int((sin(x+y+counter)+1)*3)+1; break;
case 1: image[x][y] = int((tan(y-x+counter)+1)*3)+1; break;
case 2: image[x][y] = int((cos(y+counter)+1)*3)+1; break;
case 3: image[x][y] = int((sin(x-y+counter)+1)*3)+1; break;
case 4: image[x][y] = int((tan(x+counter)+1)*3)+1; break;
}
}
}
if (++ticker % 15 == 0 && ++mode > 4) {
mode = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment