Skip to content

Instantly share code, notes, and snippets.

@StuffAndyMakes
Last active July 7, 2019 17:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StuffAndyMakes/ffb6c61871289b0d7b73 to your computer and use it in GitHub Desktop.
Save StuffAndyMakes/ffb6c61871289b0d7b73 to your computer and use it in GitHub Desktop.
Charlieplexing 20 LEDs
// pin defines
#define A 12
#define B 11
#define C 10
#define D 9
#define E 8
#define PIN_COUNT 5
#define PIN_CONFIG 0
#define PIN_STATE 1
#define LED_COUNT 20
/*
* If you want to save bytes in SRAM, this could be a collection of bytes for each DDRx and PORTx
* on the AVR chip (up to 8 bits or lines) (8 lines would allow for 56 LEDs)
*/
int matrix[LED_COUNT][2][PIN_COUNT] = {
/*
* Each row in this matrix respresents the pin modes and pin states for a single LED
*/
// PIN_CONFIG PIN_STATE
// A B C D E A B C D E
{ { OUTPUT, OUTPUT, INPUT, INPUT, INPUT }, { HIGH, LOW, LOW, LOW, LOW } }, // AB 0
{ { OUTPUT, OUTPUT, INPUT, INPUT, INPUT }, { LOW, HIGH, LOW, LOW, LOW } }, // BA 1
{ { INPUT, OUTPUT, OUTPUT, INPUT, INPUT }, { LOW, HIGH, LOW, LOW, LOW } }, // BC 2
{ { INPUT, OUTPUT, OUTPUT, INPUT, INPUT }, { LOW, LOW, HIGH, LOW, LOW } }, // CB 3
{ { OUTPUT, INPUT, OUTPUT, INPUT, INPUT }, { LOW, LOW, HIGH, LOW, LOW } }, // CD 4
{ { INPUT, INPUT, OUTPUT, OUTPUT, INPUT }, { LOW, LOW, LOW, HIGH, LOW } }, // DC 5
{ { INPUT, INPUT, INPUT, OUTPUT, OUTPUT }, { LOW, LOW, LOW, HIGH, LOW } }, // DE 6
{ { INPUT, INPUT, INPUT, OUTPUT, OUTPUT }, { LOW, LOW, LOW, LOW, HIGH } }, // ED 7
{ { OUTPUT, INPUT, OUTPUT, INPUT, INPUT }, { HIGH, LOW, LOW, LOW, LOW } }, // AC 8
{ { OUTPUT, INPUT, OUTPUT, INPUT, INPUT }, { LOW, LOW, HIGH, LOW, LOW } }, // CA 9
{ { INPUT, INPUT, OUTPUT, INPUT, OUTPUT }, { LOW, LOW, HIGH, LOW, LOW } }, // CE 10
{ { INPUT, INPUT, OUTPUT, INPUT, OUTPUT }, { LOW, LOW, LOW, LOW, HIGH } }, // EC 11
{ { INPUT, OUTPUT, INPUT, OUTPUT, INPUT }, { LOW, HIGH, LOW, LOW, LOW } }, // BD 12
{ { INPUT, OUTPUT, INPUT, OUTPUT, INPUT }, { LOW, LOW, LOW, HIGH, LOW } }, // DB 13
{ { OUTPUT, INPUT, INPUT, OUTPUT, INPUT }, { HIGH, LOW, LOW, LOW, LOW } }, // AD 14
{ { OUTPUT, INPUT, INPUT, OUTPUT, INPUT }, { LOW, LOW, LOW, HIGH, LOW } }, // DA 15
{ { INPUT, OUTPUT, INPUT, INPUT, OUTPUT }, { LOW, HIGH, LOW, LOW, LOW } }, // BE 16
{ { INPUT, OUTPUT, INPUT, INPUT, OUTPUT }, { LOW, LOW, LOW, LOW, HIGH } }, // EB 17
{ { OUTPUT, INPUT, INPUT, INPUT, OUTPUT }, { HIGH, LOW, LOW, LOW, LOW } }, // AE 18
{ { OUTPUT, INPUT, INPUT, INPUT, OUTPUT }, { LOW, LOW, LOW, LOW, HIGH } } // EA 19
};
void turnOn( int led ) {
// set all the pin modes
pinMode( A, matrix[led][PIN_CONFIG][0] );
pinMode( B, matrix[led][PIN_CONFIG][1] );
pinMode( C, matrix[led][PIN_CONFIG][2] );
pinMode( D, matrix[led][PIN_CONFIG][3] );
pinMode( E, matrix[led][PIN_CONFIG][4] );
// set all the pin states
digitalWrite( A, matrix[led][PIN_STATE][0] );
digitalWrite( B, matrix[led][PIN_STATE][1] );
digitalWrite( C, matrix[led][PIN_STATE][2] );
digitalWrite( D, matrix[led][PIN_STATE][3] );
digitalWrite( E, matrix[led][PIN_STATE][4] );
}
void setup() {}
void loop() {
for( int l = 0; l < LED_COUNT; l++ ) {
turnOn( l );
delay( 1000 / LED_COUNT );
}
}
@jeffwurz
Copy link

This code is so great and clean. I did notice 2 issues, when driving DC the pin state for D is input and A is output,
{ {OUTPUT , INPUT, OUTPUT, OUTPUT, INPUT }, { LOW, LOW, LOW, HIGH, LOW } }, // DC 5

Also the commas are missing after CE in the matrix.

But again, great code and very easy to understand how it works :)

@StuffAndyMakes
Copy link
Author

Hey Jeff! Thanks for pointing out the broken stuff. I added the commas, but it's been so long since I worked on this code that I can't see the issue in the pattern on the line for "DC 5" (nothing stood out just staring at it). I've been away from this code for a few years now.

@jeffwurz
Copy link

jeffwurz commented Jul 7, 2019

D and C are the channel 5 pins, and A and D and C are outputs for that channel, which causes problems when run. changing the line to below should fix it.
{ {INPUT, INPUT, OUTPUT, OUTPUT, INPUT }, { LOW, LOW, LOW, HIGH, LOW } }, // DC 5

@StuffAndyMakes
Copy link
Author

Ah, yes! Thank you for the correction! Code is updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment