8x8_RGB_LED_MATRIX.ino:
#include "8x8RGBMatrix.h"
Matrix8x8 matrix;
void setup()
{
matrix.begin();
matrix.clearBoard();
for (int i = 0; i < 8; i ++)
{
for (int j = 0; j < 8; j ++)
{
matrix.drawPixel(j,i,0,0,255);
}
}
}
void loop()
{
matrix.activateBoard();
}
8x8RGBMatrix.h:
#ifndef rgbm
#define rgbm
#include "Arduino.h"
#include <TimedAction.h>
#define V1 12
#define V2 11
#define V3 10
#define V4 9
#define V5 8
#define V6 7
#define V7 6
#define V8 5
#define G1 30
#define G2 31
#define G3 32
#define G4 33
#define G5 34
#define G6 35
#define G7 36
#define G8 37
#define R1 38
#define R2 39
#define R3 40
#define R4 41
#define R5 42
#define R6 43
#define R7 44
#define R8 45
#define B1 46
#define B2 47
#define B3 48
#define B4 49
#define B5 50
#define B6 51
#define B7 52
#define B8 53
class Matrix8x8 {
public:
//Constructor
Matrix8x8();
//Methods
static void begin();//Sets the ports needed for the matrix
static void drawPixel(int posx, int posy, int r, int b, int g);//Turns on or off the LED to a certain x or y value
static void clearBoard();//Turns all of the LEDs on the board off
static void activateBoard();
private:
//Variables
static int Volt[];
static int Red[];
static int Green[];
static int Blue[];
static int matrixRed[8][8];
static int matrixBlue[8][8];
static int matrixGreen[8][8];
static void LED_Delay(int d);
};
#endif
8x8RGBMatrix.cpp:
#include "8x8RGBMatrix.h"
int Matrix8x8::Volt[] = {V1, V2, V3, V4, V5, V6, V7, V8};
int Matrix8x8::Red[] = {R1, R2, R3, R4, R5, R6, R7, R8};
int Matrix8x8::Green[] = {G1, G2, G3, G4, G5, G6, G7, G8};
int Matrix8x8::Blue[] = {B1, B2, B3, B4, B5, B6, B7, B8};
int Matrix8x8::matrixRed[8][8] = {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
int Matrix8x8::matrixBlue[8][8] = {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
int Matrix8x8::matrixGreen[8][8] = {
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
Matrix8x8::Matrix8x8()
{
}
void Matrix8x8::begin()//Sets the ports needed for the matrix
{
//Sets all of the Volt pins
for (int i = 5; i <= 12; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
//Sets all of the RGB ground pins
for (int i = 30; i <= 53; i ++)
{
pinMode(i, OUTPUT);
digitalWrite(i, HIGH);
}
}
void Matrix8x8::activateBoard()
{
for (int i = 0; i < 8; i ++)
{
//Red LEDS
digitalWrite(Red[i], LOW);
for (int j = 0; j < 8; j ++)
{
analogWrite(Volt[j], matrixRed[i][j]);
delayMicroseconds(100);
digitalWrite(Volt[j], LOW);
}
digitalWrite(Red[i], HIGH);
//Green LEDS
digitalWrite(Green[i], LOW);
for (int j = 0; j < 8; j ++)
{
analogWrite(Volt[j], matrixGreen[i][j]);
delayMicroseconds(100);
digitalWrite(Volt[j], LOW);
}
digitalWrite(Green[i], HIGH);
//Blue LEDS
digitalWrite(Blue[i], LOW);
for (int j = 0; j < 8; j ++)
{
analogWrite(Volt[j], matrixBlue[i][j]);
delayMicroseconds(100);
digitalWrite(Volt[j], LOW);
}
digitalWrite(Blue[i], HIGH);
}
}
void Matrix8x8::drawPixel(int posx, int posy, int r, int g, int b)//Turns on or off the LED to a certain x or y value
{
if (r >= 0)
{
if (r > 255)
{
matrixRed[posx][posy] = 255;
}
else if (r < 0)
{
matrixRed[posx][posy] = 0;
}
else
{
matrixRed[posx][posy] = r;
}
}
if (g >= 0)
{
if (g > 255)
{
matrixGreen[posx][posy] = 255;
}
else if (g < 0)
{
matrixGreen[posx][posy] = 0;
}
else
{
matrixGreen[posx][posy] = g;
}
}
if (b >= 0)
{
if (b > 255)
{
matrixBlue[posx][posy] = 255;
}
else if (b < 0)
{
matrixBlue[posx][posy] = 0;
}
else
{
matrixBlue[posx][posy] = b;
}
}
}
void Matrix8x8::clearBoard()//Turns all of the LEDs on the board off
{
for (int i = 0; i < (sizeof(matrixRed)/sizeof(matrixRed[0])); i ++)
{
for (int j = 0; j < (sizeof(matrixRed[0])/sizeof(matrixRed[0][0])); j++)
{
matrixRed[i][j] = 0;
}
}
for (int i = 0; i < (sizeof(matrixBlue)/sizeof(matrixBlue[0])); i ++)
{
for (int j = 0; j < (sizeof(matrixBlue[0])/sizeof(matrixBlue[0][0])); j++)
{
matrixBlue[i][j] = 0;
}
}
for (int i = 0; i < (sizeof(matrixGreen)/sizeof(matrixGreen[0])); i ++)
{
for (int j = 0; j < (sizeof(matrixGreen[0])/sizeof(matrixGreen[0][0])); j++)
{
matrixGreen[i][j] = 0;
}
}
}
void Matrix8x8::LED_Delay(int d)
{
int y;
y = d * 10;
while(y--);
}