Chaplex is a library to easy control charlieplexed leds with more than one led switched ON at one time without any limit i.e. as a maximum all leds can be ON. Of course this cannot be done without multiplexing - so my library inherits an easy method to control the Arduino pins connected to the leds.
More you can find here.
To understand how this works let us look at the example of controlling six leds connected to the Arduino pins 2 (via R1), 3 (via R2) and 4 (via R3).
Chaplex uses some ideas of Alexander Brevigs library Charlieplex.
With the following statements an instance of Chaplex is created:
With the defined array ctrlpins it is now possible to describe the controlled leds.
This is done in the same way as in the Charlieplex library of Alexander Brevig. A typedef in Chaplex.h defines a pair of indexes in ctrlpins where anode and cathode of the led are connected to.
This defined type is called charlieLed in Chaplex. You can create single variables of type charlieLed for every led.
But if you want to control the leds in the same way it can be a better idea to use an array. The order of the leds as elements in the array should follow the mechanical order.
Let us assume that the leds shown in the plan above are mechanical side by side in this way:
Array declaration for this is:
If a charlieplexed structure with more leds would be in a square then there is nothing against creating a two dimensional array of type charlieLed.
Let us assume, Arduino pin 2 is set to OUTPUT/HIGH. Now pin 3 and pin 4 control LED1 and LED3 in this way:
|pin 3||pin 4||LED1||LED3|
So in one cycle all (two in our example) leds with the anode connected to the pin set to HIGH are controlled. Let us call this a row of leds.
The next cycle sets pin 3 to OUTPUT/HIGH and selects so the next row. Now pin 2 and pin 4 can control LED2 and LED5.
Third cycle sets pin 4 to OUTPUT/HIGH and selects the third and last row. Now pin 2 and pin 3 can control LED4 and LED6.
And now we are round. We only need these three cycles and when this is done in a loop fast enough you can light on all leds without flickering.
But it is necessary that there is an information how all leds should be switched, ON or OFF.
Chaplex divides setting of leds ON or OFF and output to the hardware. Methods for setting single leds ON or OFF and clearing all leds to OFF only write informations into a private 2-dimensional array of Chaplex:
|pin 2 - column 0||pin 3 - column 1||pin 4 - column 2|
|pin 2 - row 0||OFF (constant)||ON or OFF||ON or OFF|
|pin 3 - row 1||ON or OFF||OFF (constant)||ON or OFF|
|pin 4 - row 2||ON or OFF||ON or OFF||OFF (constant)|
A method for driving the leds takes the informations from this array and controls the Arduino pins - only one row. The next call of this method does the next row. Next call - the third and last row.
And again the next call of the output method starts again with the first row (in this example with three pins). It's your task of making the calling program this way that the output method is called fast enough again and again in a loop or via interrupt.
But you do not have to take care for the round going cycles over the rows, this is done in the method with a private counter.
Here you can download a .zip-file with the library.
Sets the given led (described by a single variable or an array element) to state OFF or ON in a private array of the instance. No real hardware output is caused. ON and OFF are defined in Chaplex.h as 1 and 0.
Sets for all leds the value in the private array to OFF. No real hardware output is caused.
Controls the Arduino pins with the values in the private array. The method makes the hardware output for one cycle and sets private counters so that next call of the method will do the next cycle (or the first).
The constructor of an instance also creates the private 2-dimensional array for the led values and the private loop counter variable for outRow(). At the end of the creator the funtion allClear() ist called to set all leds in the private array to OFF and function outRow() is called for hardware output of the first row (may be this is unnecessary).
There is no limit to create more instances of Chaplex to control different groups of charlieplexed leds connected to different Arduino pins. You have to secure that outRow() methods of all instances are called often enough to avoid flickering of controlled leds.
There are two examples:
I used work with the library Chaplex for learning how to create Arduino libraries. I have no project with charlieplexing so I will not develop this library further on. You are free to do it.
For example you could create a new public method which sets a limits for leds really switched to ON during hardware output in outRow(). If your project only needs much less then all leds in a row switched to ON you could take smaller resistors for more brightness and nevertheless be secure that in error case not too many leds can be really switched ON in outRow(). So an Arduino pin would be save during set to HIGH.