This is my code for a multiplexed 3 digit 7 segment display almost same wiring to the schematic by Leon Heller just for three digits and without any resistors/transistors.
NumPrint () Translates each number to segments that need to be on and the ones that need to be off.
SegmentWrite () handles the lighting for each segment on the current digit.
Cursor () selects the digit.
UpdatePrint () prints the '-' symbol in all digits. Application specific you shouldnt need it.
You need to know what pin corresponds to which segment + the pins for each digit.
Basicaly LEDs that are off are reversed polarized and the ones that are on are correctly polarized. This is why you select the digit with the LOW value and the on segments with a HIGH value.
void setup() {
firstdigit =1;
seconddigit = 2;
thirddigit = 3;
}
void loop()
{
Print ();
}
void Print () {
Cursor(1); NumPrint(firstdigit);
Cursor(2); NumPrint(seconddigit);
Cursor(3); NumPrint(thirddigit);
}
void UpdatePrint() {
Cursor(4);
SegmentWrite(HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW);
}
void NumPrint (int number) {
switch ( number )
{
case 0:
SegmentWrite(LOW,LOW,LOW,LOW,LOW,LOW,HIGH);
break;
case 1:
SegmentWrite(LOW,HIGH,HIGH,LOW,HIGH,HIGH,HIGH);
break;
case 2:
SegmentWrite(HIGH,LOW,LOW,LOW,LOW,HIGH,LOW);
break;
case 3:
SegmentWrite(LOW,HIGH,LOW,LOW,LOW,HIGH,LOW);
break;
case 4:
SegmentWrite(LOW,HIGH,HIGH,LOW,HIGH,LOW,LOW);
break;
case 5:
SegmentWrite(LOW,HIGH,LOW,HIGH,LOW,LOW,LOW);
break;
case 6:
SegmentWrite(LOW,LOW,LOW,HIGH,LOW,LOW,LOW);
break;
case 7:
SegmentWrite(LOW,HIGH,HIGH,LOW,LOW,HIGH,HIGH);
break;
case 8:
SegmentWrite(LOW,LOW,LOW,LOW,LOW,LOW,LOW);
break;
case 9:
SegmentWrite(LOW,HIGH,LOW,LOW,LOW,LOW,LOW);
break;
case -1:
SegmentWrite(HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH);
break;
default:
SegmentWrite(HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,LOW);;
}
}
void SegmentWrite(bool a,bool b,bool c,bool d,bool e,bool f,bool g) {
digitalWrite(0, a);
digitalWrite(1, b);
digitalWrite(2, c);
digitalWrite(3, d);
digitalWrite(4, e);
digitalWrite(5, f);
digitalWrite(6, g);
}
void Cursor(int CurPos) {
delay (1);
SegmentWrite(HIGH,HIGH,HIGH,HIGH,HIGH,HIGH,HIGH);
switch ( CurPos )
{
case 1:
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
break;
case 2:
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
break;
case 3:
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
break;
case 4:
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
break;;
}
}