int cols[] = {2, 3, 4, 5};
int rows[] = {8, 9};
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(cols[i], OUTPUT);
digitalWrite(cols[i],HIGH);
}
for (int i = 0; i < 2; i++) {
pinMode(rows[i], OUTPUT);
digitalWrite(rows[i],LOW);
}
}
/*
* Display all LEDs on on a row of your choice.
*/
void rowDemo() {
digitalWrite(rows[0], HIGH); // Try all four combinations LOW/LOW, HIGH/LOW, LOW/HIGH, HIGH/HIGH
digitalWrite(rows[1], LOW); // Switch the LOW and HIGH values to observe how the rows and columns work.
for (int col = 0; col < 4; col++) {
// briefky pulse the LED
digitalWrite(cols[col], LOW);
delay(1); // Change this delay to higher values (e.g. 100) to see the strobing effect in slow motion
digitalWrite(cols[col], HIGH);
}
}
/*
* Alternate all LEDs on by enabling just one row at a time.
*/
void alternatingDemo() {
static unsigned long lastChangeTime;
static bool rowZeroOn = true;
const unsigned long interval = 500;
unsigned long _now = millis();
if (_now - lastChangeTime >= interval) {
lastChangeTime = _now;
rowZeroOn = ! rowZeroOn;
if (rowZeroOn) { // Turn row zero on
digitalWrite(rows[0], HIGH);
digitalWrite(rows[1], LOW);
} else { // Turn row one on.
digitalWrite(rows[0], LOW);
digitalWrite(rows[1], HIGH);
}
}
for (int col = 0; col < 4; col++) {
// briefky pulse the LED
digitalWrite(cols[col], LOW);
delay(1); // Change this delay to higher values (e.g. 100) to see the strobing effect in slow motion
digitalWrite(cols[col], HIGH);
}
}
/*
* Use the row and column in a coordinated fashion to display the binary representation
* of col on that specific column. i.e. column 0 displays a binary 00, column 1 -> 01, col 2 -> 10 and col 3 -> 11.
*/
void countDemo() {
// display the binary value of col on each of the led matrix's columns (00, 01, 10 and 11).
for (int col = 0; col < 4; col++) {
// briefky pulse the LED
digitalWrite(rows[0], col & 0x01);
digitalWrite(rows[1], col & 0x02);
digitalWrite(cols[col], LOW);
delay(1); // Change this delay to higher values (e.g. 100) to see the strobing effect in slow motion
digitalWrite(cols[col], HIGH);
digitalWrite(rows[0], LOW); // Not strictly required, but may avoid a ghosting effect IRL.
digitalWrite(rows[1], LOW);
}
}
void loop() {
rowDemo(); // Enable just one of these lines to see a different effect.
//alternatingRowDemo(); // Do not enable more than one line at a time otherwise the effects will "fight" with one another.
//countDemo();
}