#include <Arduino.h>
byte seven_seg_digits[10][8] = { 
// 7 세그먼트에 표현할 숫자값을 2 진수 배열 형태로 선언 함
 // a, b, c, d, e, f, g, h
 { 1, 1, 1, 1, 1, 1, 0, 0 }, // 0
 { 0, 1, 1, 0, 0, 0, 0, 0 }, // 1
 { 1, 1, 0, 1, 1, 0, 1, 0 }, // 2
 { 1, 1, 1, 1, 0, 0, 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, 1, 0, 0 }, // 7
 { 1, 1, 1, 1, 1, 1, 1, 0 }, // 8
 { 1, 1, 1, 1, 0, 1, 1, 0 } // 9
};
/*각 핀정의 */
int Switch1_Pin = A1; // 스위치 1
int Switch2_Pin = A2; // 스위치 2
int Switch3_Pin = A3; // 스위치 3
int Switch4_Pin = A4; // 스위치 4
int Switch5_Pin = A5; // 스위치 5
int Fnd_a = 9; // 7 세그먼트 "a" 자리 핀
int Fnd_b = 8; // 7 세그먼트 "b" 자리 핀
int Fnd_c = 7; // 7 세그먼트 "c" 자리 핀
int Fnd_d = 6; // 7 세그먼트 "d" 자리 핀
int Fnd_e = 5; // 7 세그먼트 "e" 자리 핀
int Fnd_f = 4; // 7 세그먼트 "f" 자리 핀
int Fnd_g = 3; // 7 세그먼트 "g" 자리 핀
int Fnd_dp= 2; // 7 세그먼트 "dp,h" 자리 핀
void setup() // 초기화
{ 
 pinMode(Fnd_a, OUTPUT); // Fnd 'a' 위치의 핀출력 포트 설정
 pinMode(Fnd_b, OUTPUT); // Fnd 'b' 위치의 핀출력 포트 설정
 pinMode(Fnd_c, OUTPUT); // Fnd 'c' 위치의 핀출력 포트 설정
 pinMode(Fnd_d, OUTPUT); // Fnd 'd' 위치의 핀출력 포트 설정
 pinMode(Fnd_e, OUTPUT); // Fnd 'e' 위치의 핀출력 포트 설정
 pinMode(Fnd_f, OUTPUT); // Fnd 'f' 위치의 핀출력 포트 설정
 pinMode(Fnd_g, OUTPUT); // Fnd 'g' 위치의 핀출력 포트 설정
 pinMode(Fnd_dot, OUTPUT); // Fnd 'dot' 위치의 핀출력 포트 설정
 writeDot(0); // 7 세그먼트에 도트(Dot) LED OFF
 pinMode(Switch1_Pin, INPUT); // 스위치 1 번핀입력 포트 설정
 pinMode(Switch2_Pin, INPUT); // 스위치 2 번핀입력 포트 설정
 pinMode(Switch3_Pin, INPUT); // 스위치 3 번핀입력 포트 설정
 pinMode(Switch4_Pin, INPUT); // 스위치 4 번핀입력 포트 설정
 pinMode(Switch5_Pin, INPUT); // 스위치 5 번핀입력 포트 설정
} 
void writeDot(byte dot) // 도트(Dot) Led On/Off 함수
{ 
 digitalWrite(7, dot);
} 
void NumberDisplay(byte num) // 7 세그먼트 디스플레이 함수
{ 
 int j;
 for (j = 7; j >= 0; j--) // a ~ g
 { 
 digitalWrite(7 - j, seven_seg_digits[num][j]);
 } 
} 
/*스위치를 이용하여 7 세그먼트에 출력 되는 숫자를 표현할 수 있다. 
다수의 스위치를 사용하는 만큼 다른 기능도 추가하여 구현할 수 있다. */
void loop() // 무한루프
{ 
 if (digitalRead(Switch1_Pin) == LOW) // 첫번째 스위치 누르면
 { 
 NumberDisplay(1); // 7 세그먼트(1Bit 7 세그먼트)에 숫자 1 출력
 } 
 else if (digitalRead(Switch2_Pin) == LOW) // 두 번째 스위치 누르면
 { 
 NumberDisplay(3); // 7 세그먼트(1Bit 7 세그먼트)에 숫자 3 출력
 } 
 else if (digitalRead(Switch3_Pin) == LOW) // 세 번째 스위치 누르면
 {
 NumberDisplay(5); // 7 세그먼트(1Bit 7 세그먼트)에 숫자 5 출력
 } 
 else if (digitalRead(Switch4_Pin) == LOW) // 네 번째 스위치 누르면
 { 
 NumberDisplay(7); // 7 세그먼트(1Bit 7 세그먼트)에 숫자 7 출력
 } 
 else if (digitalRead(Switch5_Pin) == LOW) // 다섯 번째 스위치 누르면
 { 
 NumberDisplay(9); // 7 세그먼트(1Bit 7 세그먼트)에 숫자 9 출력
 } 
 else // 스위치를 누르지 않을 경우
 { 
 NumberDisplay(0); // 7 세그먼트(1Bit 7 세그먼트)에 숫자 0 출력
 } 
}