//array for the input pins
int inputPin[] = {2, 3, 4, 5};
//array for the pins connected to the 7 segment display
int segments[] = {6, 7, 8, 9, 10, 11, 12};
//variable to hold the sum of the bits set by the input initialize to 0
int sum = 0;
byte digits[16][7] = {
//2D data array for the digits from 0 - F
{1,1,1,1,1,1,0},//0
{0,1,1,0,0,0,0},//1
{1,1,0,1,1,0,1},//2
{1,1,1,1,0,0,1},//3
{0,1,1,0,0,1,1},//4
{1,0,1,1,0,1,1},//5
{1,0,1,1,1,1,1},//6
{1,1,1,0,0,0,0},//7
{1,1,1,1,1,1,1},//8
{1,1,1,1,0,1,1},//9
{1,1,1,0,1,1,1},//a
{0,0,1,1,1,1,1},//b
{1,0,0,1,1,1,0},//c
{0,1,1,1,1,0,1},//d
{1,0,0,1,1,1,1},//e
{1,0,0,0,1,1,1},//f
};
void setup() {
//configuring inputPin as input
for(int x = 0; x < 4; x++){
pinMode(inputPin[x], INPUT);
}
//configuring segments as output
for(int y = 0; y < 7; y++){
pinMode(segments[y], OUTPUT);
}
}
void loop() {
//read input pins each multiplied by 2^n
int val1 = digitalRead(inputPin[0]) * 1; //LSB
int val2 = digitalRead(inputPin[1]) * 2;
int val3 = digitalRead(inputPin[2]) * 4;
int val4 = digitalRead(inputPin[3]) * 8; //MSB
sum = val1 + val2 + val3 + val4;//sum of the bits set
//output the designated digit given the binary input
for(int i = 0; i < 7; i++){
digitalWrite(segments[i], digits[sum][i]);
}
delay(10);
}