2개의 push button으로 FND의 숫자 카운트를 up/down 시키는 실습
회로도...여기서는 Common Anode 타입의 FND를 사용
브레드보드 연결도
4번핀에 연결된 스위치로 숫자 카운트를 up, 5번핀에 연결된 스위치로 숫자 카운트를 down하는 스케치
#define FND_A 13
#define FND_B 12
#define FND_C 11
#define FND_D 10
#define FND_E 9
#define FND_F 8
#define FND_G 7
#define FND_DP 6
#define UP 4
#define DN 5
#define dW digitalWrite
#define ON LOW
#define OFF HIGH
int gCnt = 0;
void setup() {
for (int i=6;i<14;i++) pinMode(i, OUTPUT);
pinMode(UP, INPUT_PULLUP);
pinMode(DN, INPUT_PULLUP);
}
void loop() {
readUpSw(); // UP 스위치 처리
readDnSw(); // DOWN 스위치 처리
dispFnd(gCnt);
}
void dispFnd(int val)
{
static int prevVal = -1;
if (prevVal != val) { // FND에 표시할 값이 바뀐 경우에만 표시를 변경
switch (val) {
case 0:
dW(FND_A, ON); dW(FND_B, ON); dW(FND_C, ON); dW(FND_D, ON); dW(FND_E, ON); dW(FND_F, ON); dW(FND_G, OFF);
break;
case 1:
dW(FND_A, OFF); dW(FND_B, ON); dW(FND_C, ON); dW(FND_D, OFF); dW(FND_E, OFF); dW(FND_F, OFF); dW(FND_G, OFF);
break;
case 2:
dW(FND_A, ON); dW(FND_B, ON); dW(FND_C, OFF); dW(FND_D, ON); dW(FND_E, ON); dW(FND_F, OFF); dW(FND_G, ON);
break;
case 3:
dW(FND_A, ON); dW(FND_B, ON); dW(FND_C, ON); dW(FND_D, ON); dW(FND_E, OFF); dW(FND_F, OFF); dW(FND_G, ON);
break;
case 4:
dW(FND_A, OFF); dW(FND_B, ON); dW(FND_C, ON); dW(FND_D, OFF); dW(FND_E, OFF); dW(FND_F, ON); dW(FND_G, ON);
break;
case 5:
dW(FND_A, ON); dW(FND_B, OFF); dW(FND_C, ON); dW(FND_D, ON); dW(FND_E, OFF); dW(FND_F, ON); dW(FND_G, ON);
break;
case 6:
dW(FND_A, ON); dW(FND_B, OFF); dW(FND_C, ON); dW(FND_D, ON); dW(FND_E, ON); dW(FND_F, ON); dW(FND_G, ON);
break;
case 7:
dW(FND_A, ON); dW(FND_B, ON); dW(FND_C, ON); dW(FND_D, OFF); dW(FND_E, OFF); dW(FND_F, ON); dW(FND_G, OFF);
break;
case 8:
dW(FND_A, ON); dW(FND_B, ON); dW(FND_C, ON); dW(FND_D, ON); dW(FND_E, ON); dW(FND_F, ON); dW(FND_G, ON);
break;
case 9:
dW(FND_A, ON); dW(FND_B, ON); dW(FND_C, ON); dW(FND_D, ON); dW(FND_E, OFF); dW(FND_F, ON); dW(FND_G, ON);
break;
}
prevVal = val;
}
}
boolean readUpSw()
{
static boolean prev = HIGH;
static unsigned long last;
boolean curr;
if ((millis()-last)>10) {
curr = digitalRead(UP);
if ((HIGH==prev)&&(LOW==curr)) { // UP SW가 눌린 시점
gCnt = (gCnt+1)%10;
}
prev = curr;
last = millis();
}
}
boolean readDnSw()
{
static boolean prev = HIGH;
static unsigned long last;
boolean curr;
if ((millis()-last)>10) {
curr = digitalRead(DN);
if ((HIGH==prev)&&(LOW==curr)) { // DOWN SW가 눌린 시점
gCnt = gCnt-1;
if (-1 == gCnt) gCnt = 9;
}
prev = curr;
last = millis();
}
}