Pages: [1]   Go Down
Author Topic: charlieplexing 72 LED's. Need help with code  (Read 834 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello
My apologies, I'm a hard working newbie so please excuse my english:

I want to create an array of 72 led's placed horizontally. When I press a switch, the first LED goes on, then the second and so on, in an array.

I am charlieplexing with digital pins 0 to 9.

Below is the code I made (based on knight rider 01 example) using only three pins.  (5, 6, 7)
and a schematics here: www.nicoykatiushka.org/arduino/schematics/72leds.jpg

My problem is that if I keep using this code, I'll have a long and nasty code.

Any suggestions on how to clean or make a nicer code?


thanks

nico


int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int timer = 500;

void setup(){

  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
}

void loop() {
   pinMode(pin7, INPUT);
   digitalWrite(pin5, HIGH);
   digitalWrite(pin6, LOW);
   delay(timer);

   digitalWrite(pin6, HIGH);
   digitalWrite(pin5, LOW);
   delay(timer);

   pinMode(pin5, INPUT);
   pinMode(pin7, OUTPUT);

   digitalWrite(pin6, HIGH);
   digitalWrite(pin7, LOW);
   delay(timer);

   digitalWrite(pin7, HIGH);
   digitalWrite(pin6, LOW);
   delay(timer);

   pinMode(pin6, INPUT);
   pinMode(pin5, OUTPUT);

   digitalWrite(pin5, HIGH);
   digitalWrite(pin7, LOW);
   delay(timer);

   digitalWrite(pin7, HIGH);
   digitalWrite(pin5, LOW);
   delay(timer);

   setup();
}

Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6637
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
unsigned char cathodes[72] = {1,2,3,4,5,6,7,8,9, 0,2,3,4,5,6, ...};
unsigned char anodes[72] =   {0,0,0,0,0,0,0,0,0, 1,1,1,1,1,1,...};

void light_cp_led(unsigned char N)
{
  static unsigned char last_n;
  pinMode(cathodes[last_n], INPUT);  // turn off previous LED
  pinMode(anodes[last_n], INPUT);  // on both ends
  pinMode(cathodes[n], OUTPUT);  // turn on new LED cathode
  digitalWrite(cathodes[n], LOW);
  pinMode(anodes[n], OUTPUT);  // turn on new LED annode
  digitalWrite(anodes[n], HIGH);
}

You can put the arrays in PROGMEM, or derive the anodes[n] and cathodes[n] algorithmically to save additional space, but this is nicely obvious and easily editable.  (and a very similar scheme works for two-dimensional arrays of LEDs that don't have to match the charlieplexing dimensions...)
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Might be a dumb question but when i use this code (the one from the post above) arduino says "n was not declared in this scope"
I've been studying this option and I finally understand a little more, however I cant make it work.
Best
nico
« Last Edit: August 11, 2008, 11:51:51 pm by nykffa » Logged

Florida, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like a case-sensitivity issue in C++.  Try changing:

Code:
void light_cp_led(unsigned char N)

to

Code:
void light_cp_led(unsigned char n)
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 597
Posts: 33328
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your also missing the following line:-
last_n = n;
to be placed as the last one in the function.
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6637
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry; my code snippet was supposed to be illustrating an easy way to map LED numbers to charlieplex parameters, but was (obviously) not tested or intended to be complete.  I should have made that more obvious...
Logged

New Zealand
Offline Offline
God Member
*****
Karma: 0
Posts: 999
Arduino pebbles
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
should have made that more obvious...

Like this?

[size=36]not tested or intended to be complete!!1![/color][/size][/b]

smiley-grin

--Phil.

[/silliness]
« Last Edit: August 16, 2008, 10:20:09 pm by follower » Logged

Pages: [1]   Go Up
Jump to: