Go Down

Topic: charlieplexing 72 LED's. Need help with code (Read 967 times) previous topic - next topic

nykffa

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();
}


westfw

Code: [Select]
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...)

nykffa

#2
Aug 12, 2008, 06:51 am Last Edit: Aug 12, 2008, 06:51 am by nykffa Reason: 1
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

CosineKitty

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

Code: [Select]

void light_cp_led(unsigned char N)


to

Code: [Select]

void light_cp_led(unsigned char n)

Grumpy_Mike

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

westfw

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...

follower

#6
Aug 17, 2008, 05:19 am Last Edit: Aug 17, 2008, 05:20 am by follower Reason: 1
Quote
should have made that more obvious...


Like this?

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

:D

--Phil.

[/silliness]

Go Up