can you show me the led() function code? or is that in a library?
Also, let my preface by saying I'm currently learning about multiplexing, myself. Here's my code for a common anode 7-segment display I'm multiplexing using the arduino and one 595 shift register. As a test, it just counts up in hundredths of a second. I connected the anodes to digital pins 2, 3, 4, and 5 (digits 0, 1, 2, and 3 respectively).
long time;
//for communicating with the 595
int latchPin = 7;
int clockPin = 6;
int dataPin = 8;
int nums[] = { //The numbers correspond to their indexes for ease of use
B11000000, //0
B11111001, //1
B10100100, //2
B10110000, //3
B10011001, //4
B10010010, //5
B10000010, //6
B11111000, //7
B10000000, //8
B10011000 }; //9
void setup()
{
//display digit anodes, [b]these will be doing the actual multiplexing[/b]
pinMode(2, OUTPUT); //0th digit
pinMode(3, OUTPUT); //1st digit
pinMode(4, OUTPUT); //2nd digit
pinMode(5, OUTPUT); //3rd digit
//for communicating with the 595
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
//start with all the digits off
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
//zeroing out the counter for the timer
time = millis();
}
void loop()
{
//how much time has passed?
long temp = millis() - time;
//display the time in hundredths of a second (passed to the display function)
display( temp / 10 );
}
void display( long num )
{
num = num%10000; //prevents numbers larger than 4 digits
burst( num/1000, 0 ); //gets the 0th digit, passed to burst function, 0th place
burst( (num/100)%10, 1 ); //gets the 1st digit, passed to burst function, 1st place
burst( (num/10)%10, 2 );//gets the 2nd digit, passed to burst function, 2nd place
burst( num%10, 3 );//gets the 3rd digit, passed to burst function, 3rd place
}
void burst( int num, int place ) {
boolean dot = false; //display a dot
if( place == 1 ) {
dot = true; //there will be a dot in the 1st position (I could do this in a less lazy way I'm sure)
}
//send the digit to the 595
digitalWrite(latchPin, 0);
int temp = nums[num];
if( dot ) {
temp = temp ^ B10000000; //uses XOR to add the dot to the binary to be sent to the 595
}
shiftOut(dataPin, clockPin, MSBFIRST, temp);
digitalWrite(latchPin, 1);
digitalWrite(place + 2, HIGH); //turns on the digit (again, lazy: 0th digit is digital pin 2)
delay(4); //digit stays on for 4ms
digitalWrite(place + 2, LOW); //turns of the digit
}
I hope my annotation helps explain the process. Basically each digit is turned on and off in order, and all the digits can share the same 595 shift register.