# 3 bit 7 segment display

This is a little higher level, created a function to make it much easier to use a 3 bit seven segment display. It uploads to the arduino just fine, I added a blink to pin 13 and the on board led flashes, so it is something wrong with the code. This is what I have, I have commented on the side of the array which number it shows. I have the 20401. I don’t have a decimal point option yet, eventually I will add one.
Magnitude is which digit is turned on. This should turn on a digit then turn it off, then go to the next digit, etc. etc.

``````const unsigned char segment[11][8] = {
//A,B,C,D,E,F,G,DP
{1,1,1,1,1,1,0,0},//0
{0,1,1,0,0,0,0,0},//1
{1,1,0,1,0,1,1,0},//2
{1,0,0,1,1,1,1,0},//3
{0,1,1,0,0,1,1,0},//4
{1,0,1,1,0,1,1,0},//5
{1,0,1,1,1,1,1,0},//6
{1,1,1,0,0,0,0,0},//7
{1,1,1,1,1,1,1,0},//8
{1,1,1,0,0,1,1,0},//9
{1,0,0,1,1,1,1,0}//Error 10
};
const int segApin = 11;
const int segBpin = 7;
const int segCpin = 4;
const int segDpin = 2;
const int segEpin = 1;
const int segFpin = 10;
const int segGpin = 5;
const int segDPpin = 3;

int TurnOnDigit(const int magnitude, int digit)
{
if (digit > 9 || digit < 0) { digit = 11;}//E for Error

digitalWrite(segApin, segment[digit][0]);
digitalWrite(segBpin, segment[digit][1]);
digitalWrite(segCpin, segment[digit][2]);
digitalWrite(segDpin, segment[digit][3]);
digitalWrite(segEpin, segment[digit][4]);
digitalWrite(segFpin, segment[digit][5]);
digitalWrite(segGpin, segment[digit][6]);
digitalWrite(segDPpin, segment[digit][7]);

int n1 = 1;//ones place
int n10 = 1;//tens place
int n100 = 1;//hundreds place

if (magnitude == 1) {
n1 = 0;//turn on digit for 1's place
}
else if (magnitude == 2) {
n10 = 0;// turn on digit for 10's place
}
else if (magnitude == 3) {
n100 = 0;//turn on digit for 100's place
}

digitalWrite(12,n100);
digitalWrite(9,n10);
digitalWrite(8,n1);

return digit;

digitalWrite(1,OUTPUT);
digitalWrite(2,OUTPUT);
digitalWrite(3,OUTPUT);
digitalWrite(4,OUTPUT);
digitalWrite(5,OUTPUT);
digitalWrite(6,OUTPUT);
digitalWrite(7,OUTPUT);
digitalWrite(8,OUTPUT);
digitalWrite(9,OUTPUT);
digitalWrite(10,OUTPUT);
digitalWrite(11,OUTPUT);
digitalWrite(12,OUTPUT);

}//end TurnOnDigit

void setup(){
digitalWrite(1,OUTPUT);
digitalWrite(2,OUTPUT);
digitalWrite(3,OUTPUT);
digitalWrite(4,OUTPUT);
digitalWrite(5,OUTPUT);
digitalWrite(6,OUTPUT);
digitalWrite(7,OUTPUT);
digitalWrite(8,OUTPUT);
digitalWrite(9,OUTPUT);
digitalWrite(10,OUTPUT);
digitalWrite(11,OUTPUT);
digitalWrite(12,OUTPUT);
digitalWrite(13,OUTPUT);
}

void loop(){
TurnOnDigit(1,6);
delay(5);
TurnOnDigit(2,5);
delay(5);
TurnOnDigit(3,7);
delay(5);
digitalWrite(13,1);
delay(1000);
digitalWrite(13,0);
delay(950);
}
``````

You should put your pin numbers in an array too, code like this:

``````digitalWrite(segApin, segment[digit][0]);
digitalWrite(segBpin, segment[digit][1]);
digitalWrite(segCpin, segment[digit][2]);
digitalWrite(segDpin, segment[digit][3]);
digitalWrite(segEpin, segment[digit][4]);
digitalWrite(segFpin, segment[digit][5]);
digitalWrite(segGpin, segment[digit][6]);
digitalWrite(segDPpin, segment[digit][7]);
``````

can then be:

``````  for (byte i = 0 ; i < 8 ; i++)
digitalWrite (seg_pin[i], segment [digit [i]]) ;
``````
``````const unsigned char segment[11][8]
``````

This takes up 8 times more memory than it needs to. You can improve this by using a 1-D array instead of your 2-D array and specifying the patterns in binary. Then use the bitRead() function to extract the bit you need. So...

``````  {1,1,1,1,1,1,0,0},//0
``````

becomes

``````  0b11111100, //0
``````

and

``````digitalWrite(segApin, segment[digit][0]);
``````

becomes

``````digitalWrite(segApin, bitRead(segment[digit],0));
``````

Combine the above with MarkT's suggestions and suddenly your sketch will be much shorter and more efficient.