Now before we actually get to coding it, it would be a good idea to have a bit of a plan before just jumping in.
The principle is actually quite simple.
When the Arduino starts, change all pins connected to the Charlieplex to INPUT.
In the main loop:
- Turn off all LED's by setting all pins as INPUTS
- For each LED you want to turn on, look up its two pins:
Set both pins as output.
Set the pin connected to the anode HIGH
Set the pin connected to the cathode LOW
Wait a certain amount of time
- Turn off all LED's
- Turn on the next LED
- Repeat until the program ends
Let's assume I want to just sequence through all the LED's turning them on one at a time. For each LED, I need to set 5 pins to INPUT, then I need to set 2 pins to OUTPUT, do two digitalWrite() calls and then finally wait for a specified period. We need to do this 20 times.... As you can see, this gets very confusing and complex very quickly. Unless we can create some form of library, this will be another exercise in futility.
From the library, we would need the following:
- A list of all LED's in the matrix. Each LED should also specify what pins its anode and cathode is connected to.
- A function to turn off all LED's
- A function to turn on all LED's
- A function that will turn on all LED's defined by some form of mask for a specific period.
I'll explain the actual code, but I'm not going to explain how to create a library. If you are interested in that, there is a pretty good tutorial on the Arduino site:http://arduino.cc/en/Hacking/LibraryTutorial
Before you do a library, you should always get your code working as a normal sketch first in any case, so lets do that.