Hello everyone.
I was trying to build upon 2 circuits from Arduino CookBook. Multiplexing LEDs 8x8 matrix. The code and circuit controls 64 LEDs with 16 pins. For understanding I built just a 2x2 matrix (which uses 4 pins - apparently not useful but by increasing number of LEDs in a row or column does not increase the no. of pins so the same circuit can be extended).
The problem which I am facing is that I am not able to control individual LEDs as described in code. Whole row lights up together instead of single LED when I use the simple 1 pin HIGH and 1 pin LOW method. With another method described in that book which uses bitRead and bitmaps, the same circuit works fine. I am pasting the 2 codes here as well as attaching my circuit diagram and breadboard configuration. Perhaps someone can explain me why I am not getting desired result with 1st method while getting it correctly with 2nd?
1st method: (I change the code for 2x2 matrix replacing 8 by 2, 63 with 3.)
/*
matrixMpx sketch
Sequence LEDs starting from first column and row until all LEDS are lit
Multiplexing is used to control 64 LEDs with 16 pins
*/
const int columnPins[] = { 10, 9};
const int rowPins[] = { 4, 3};
int pixel = 0; // 0 to 63 LEDs in the matrix
int columnLevel = 0; // pixel value converted into LED column
int rowLevel = 0; // pixel value converted into LED row
void setup() {
for (int i = 0; i < 2; i++)
{
pinMode(columnPins[i], OUTPUT); // make all the LED pins outputs
pinMode(rowPins[i], OUTPUT);
}
}
void loop() {
pixel = pixel + 1;
if(pixel > 3)
pixel = 0;
columnLevel = pixel / 2; // map to the number of columns
rowLevel = pixel % 2; // get the fractional value
for (int column = 0; column < 2; column++)
{
digitalWrite(columnPins[column], LOW); // connect this column to Ground
for(int row = 0; row < 2; row++)
{
if (columnLevel > column)
{
digitalWrite(rowPins[row], HIGH); // connect all LEDs in row to +5 volts
}
else if (columnLevel == column && rowLevel >= row)
{
digitalWrite(rowPins[row], HIGH);
}
else
{
digitalWrite(columnPins[column], LOW); // turn off all LEDs in this row
}
delayMicroseconds(300); // delay gives frame time of 20ms for 64 LEDs
digitalWrite(rowPins[row], LOW); // turn off LED
}
// disconnect this column from Ground
digitalWrite(columnPins[column], HIGH);
}
}
2nd code:
byte bigHeart[] = {
B01,
B11};
byte smallHeart[] = {
B11,
B10};
const int columnPins[] = { 9, 10};
const int rowPins[] = { 4, 3};
void setup() {
for (int i = 0; i < 2; i++)
{
pinMode(rowPins[i], OUTPUT); // make all the LED pins outputs
pinMode(columnPins[i], OUTPUT);
digitalWrite(columnPins[i], HIGH); // disconnect column pins from Ground
}
}
void loop() {
int pulseDelay = 800 ; // milliseconds to wait between beats
show(smallHeart, 800); // show the small heart image for 100 ms
show(bigHeart, 1600); // followed by the big heart for 200ms
delay(pulseDelay); // show nothing between beats
}
// routine to show a frame of an image stored in the array pointed to by the
// image parameter.
// the frame is repeated for the given duration in milliseconds
void show( byte * image, unsigned long duration)
{
unsigned long start = millis(); // begin timing the animation
while (start + duration > millis()) // loop until the duration period has passed
{
for(int row = 0; row < 2; row++)
{
digitalWrite(rowPins[row], HIGH); // connect row to +5 volts
for(int column = 0; column < 2; column++)
{
boolean pixel = bitRead(image[row],column);
if(pixel == 1)
{
digitalWrite(columnPins[column], LOW); // connect column to Gnd
}
delayMicroseconds(300); // a small delay for each LED
digitalWrite(columnPins[column], HIGH); // disconnect column from Gnd
}
digitalWrite(rowPins[row], LOW); // disconnect LEDs
}
}
}
Whats wrong in 1st code and whats right in 2nd one? Secondly, i believe my circuit configuration is correct but if I am missing something and someone can point out?
I believe a pin is in undefined mode (neither HIGH nor LOW) until it is defined for 1st time in loop method. To return that pin to undefined mode again, should it be declared like
pinMode(pin no., INPUT);
? Is it correct way?

