// Pin definitions
const int rowPins[6] = {2, 3, 4, 5, 6, 7}; // Rows (positive side)
const int colPins[6] = {8, 9, 10, 11, 12, 13}; // Columns (negative side)
enum
{
SHOW_ROW=0,
DEAD_TIME
};
const uint8_t letterI[] =
{
0b00001100,
0b00001100,
0b00001100,
0b00001100,
0b00001100,
0b00001100
};
const uint8_t letterL[] =
{
0b00100000,
0b00100000,
0b00100000,
0b00100000,
0b00100000,
0b00111111
};
const uint8_t letterO[] =
{
0b00011110,
0b00100001,
0b00100001,
0b00100001,
0b00100001,
0b00011110
};
const uint8_t letterV[] =
{
0b00100001,
0b00100001,
0b00100001,
0b00010010,
0b00010010,
0b00001100
};
const uint8_t letterE[] =
{
0b00111111,
0b00100000,
0b00111110,
0b00100000,
0b00100000,
0b00111111
};
const uint8_t letterU[] =
{
0b00100001,
0b00100001,
0b00100001,
0b00100001,
0b00100001,
0b00011110
};
uint8_t *grpLetters[] =
{
letterI, letterL, letterO, letterV, letterE, letterU, NULL
};
uint8_t msgLetterIndex;
// Function to initialize pins
void setup()
{
msgLetterIndex = 0;
// Set all row pins as OUTPUT
for (int i = 0; i < 6; i++)
{
pinMode(rowPins[i], OUTPUT);
digitalWrite(rowPins[i], LOW); // Start with rows off
}
// Set all column pins as OUTPUT
for (int i = 0; i < 6; i++)
{
pinMode(colPins[i], OUTPUT);
digitalWrite(colPins[i], HIGH); // Start with columns off
}
}//setup
void loop()
{
static uint32_t
tLetter = 0ul;
uint32_t tNow = millis();
if( tNow - tLetter > 1000ul )
{
tLetter = tNow;
msgLetterIndex++;
if( grpLetters[msgLetterIndex] == NULL )
msgLetterIndex = 0;
}//if
DriveMatrix();
}//loop
void DriveMatrix()
{
uint8_t
*pLetter,
idx,
colVal,
mask;
static uint8_t
state = SHOW_ROW,
lastRow,
rowNo = 0;
uint32_t
tNow;
static uint32_t
tRow;
//to update the matrix at, say, 30Hz (roughly every 33mS) each row is driven for 5.5mS
//actual drive time will be 5mS with 500uS dead time between rows
tNow = micros();
switch( state )
{
case SHOW_ROW:
if( (tNow - tRow) >= 5000ul )
{
//row off
digitalWrite( rowPins[rowNo], LOW );
//prep for next row
rowNo++;
if( rowNo == 6 )
rowNo = 0;
tRow = tNow;
state = DEAD_TIME;
}//if
break;
case DEAD_TIME:
if( (tNow - tRow) >= 500ul )
{
//get the column pattern
pLetter = grpLetters[msgLetterIndex];
colVal = *(pLetter + rowNo);
//drive new column pattern
mask = 0x20;
idx = 0;
while( mask )
{
digitalWrite( colPins[idx], (colVal & mask) ? LOW : HIGH );
idx++;
mask >>= 1;
}//for
//turn on new row
digitalWrite( rowPins[rowNo], HIGH );
tRow = tNow;
state = SHOW_ROW;
}//if
break;
}//switch
}//DriveMatrix