Hope someone finds this stuff useful...
Cut and paste the following into the Arduino Editor, read the comments for details on wiring up the LEDs.
==========================================
/*
This program toggles the state of 4 leds through 16 combinations
using only 3 pins (without exclusive ground). This project was a proof of concept for implementation
of "charlieplexing"; multiplexing using dynamic use of anode and cathode- resulting in
efficient utilisation of pins for driving LED displays in a multiplexed mode.
Only one LED is on at any one time. The LEDs are flashed
alternately at a rate that achieves persistence of vision.
Known problems: This version of the program results in minor residual LED illumination
arising from sync issues during the switching phase.
Connect four LED's to your Arduino...
LED1: Cathode to pin 6, Anode to pin 5
LED2: Cathode to pin 5, Anode to pin 6
LED3: Cathode to pin 7, Anode to pin 6
LED4: Cathode to pin 6, Anode to pin 7
Todo: Rewrite in assembly, using timer interrupt to service LED sequencing.
Written by Hoastey, October 2007
*/
int ledPina=5;
int ledPinb=6; /* Use pins 5, 6 and 7 */
int ledPinc=7;
const int dly=100; /* delay time /
const int mpxdly=1; / multiplexed delay time */
void setup()
{
pinMode(ledPina, OUTPUT); /* Set the Arduino pins for output */
pinMode(ledPinb, OUTPUT);
pinMode(ledPinc, OUTPUT);
}
void loop() /* Sequence through all the LED combinations */
{
state(0,0,0,0,dly); // zero
state(0,0,0,1,dly); // one
state(0,0,1,0,dly); // two
state(0,0,1,1,dly); // three
state(0,1,0,0,dly); // four
state(0,1,0,1,dly); // five
state(0,1,1,0,dly); // six
state(0,1,1,1,dly); // seven
state(1,0,0,0,dly); // eight
state(1,0,0,1,dly); // nine
state(1,0,1,0,dly); // ten
state(1,0,1,1,dly); // eleven
state(1,1,0,0,dly); // twelve
state(1,1,0,1,dly); // thirteen
state(1,1,1,0,dly); // fourteen
state(1,1,1,1,dly); // fifteen
}
/*
Set LED state.
Accepts status for led1, led2, led3 and led4 (1=on, 0=off) and period (length of display)
*/
void state(boolean led1, boolean led2, boolean led3, boolean led4, int period)
{
for(int i=0; i<period; i++)
{
if (led1==1) {
digitalWrite(ledPina, HIGH);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
else {
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
delay(mpxdly);
if (led2==1) {
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, HIGH);
digitalWrite(ledPinc, HIGH);
}
else {
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
delay(mpxdly);
if (led3==1) {
digitalWrite(ledPina, HIGH);
digitalWrite(ledPinb, HIGH);
digitalWrite(ledPinc, LOW);
}
else {
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
delay(mpxdly);
if (led4==1) {
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, HIGH);
}
else {
digitalWrite(ledPina, LOW);
digitalWrite(ledPinb, LOW);
digitalWrite(ledPinc, LOW);
}
delay(mpxdly);
}
}
Regards,
Hoastey.