Multiplexing via switching digital pins

Please be gentle... I'm quite new to the Arduino, haha...

I have an LED bargraph that has a common anode for each color (each segment has a green and a red LED in it).


I am wanting to multiplex them so that each LED is individually addressable (thanks to CrossRoads for the design premise).


(I know the resistor placement isn't the best, but I'm struggling for breadboard real estate at the moment)

I'm working from the bargraph tutorial on this... essentially if I run either anode off the +5V everything works (for single color) but as you can imagine there is significant dimming that occurs as the number of LEDs lighting up changes.

How would I go about multiplexing this? For example, if the range is such that the first 3 LEDs are lit, then I want those three pins (D2-D4) to cycle states rapidly in succession. Can they switch fast enough to pull it off?

I don't want anyone to waste a bunch of time writing me out a full-blown program here, I'm just curious as to the syntax for this. If I understand how a couple pins get switched the rest are cake.

For now, disregard the two different colors... stick with green :slight_smile:

Thanks in advance!

Just to get the concept down for your multiplexing, you would have your sketch perform a 20 step sequence using first output 1 on and then step from 2 to 11 then output 2 and again step 2-11.

The human eye won't detect flickering light if done faster then 16 times a second (old movie frame rate), so 20 steps need to be performed in less then 1.25 seconds or shorter. That is forever in arduino time as you could probably scan 20 output steps in just a few millseconds. You should use 10 resistors on the cathode leads and none on the two anode pins.

You could utilize the blink with no delay type method to execute your 20 step sequence or you could use a timer interrupt (use MStimer2 library) to perform the scanning logic and outputs and have the main loop just update when and if you need to change what leds should be on and off.

Good luck.

Lefty

Success! I hadn't thought to look at the blink tutorial for how to do this... BRILLIANT!

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}
void loop() {
  digitalWrite(13, LOW);
  digitalWrite(12, HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(2, LOW);
  delay(1);
  digitalWrite(2, HIGH);
  digitalWrite(4, LOW);
  delay(1);
  digitalWrite(4, HIGH);
  digitalWrite(6, LOW);
  delay(1);
  digitalWrite(6, HIGH);
  digitalWrite(8, LOW);
  delay(1);
  digitalWrite(8, HIGH);
  digitalWrite(10, LOW);
  delay(1);
  digitalWrite(10, HIGH);
  digitalWrite(12, LOW);
  digitalWrite(13, HIGH);
  digitalWrite(3, LOW);
  delay(1);
  digitalWrite(3, HIGH);
  digitalWrite(5, LOW);
  delay(1);
  digitalWrite(5, HIGH);
  digitalWrite(7, LOW);
  delay(1);
  digitalWrite(7, HIGH);
  digitalWrite(9, LOW);
  delay(1);
  digitalWrite(9, HIGH);
  digitalWrite(11, LOW);
  delay(1);
  digitalWrite(11, HIGH);
}

I did this just to see the different colors in real time side by side.

;D ;D ;D ;D

Thanks Lefty for the insight...

Now on to slipping this in with my main code :-?