KeyPad 버튼에 해당하는 LED ON/OFF
- KeyPad 버튼을 누르고 있을 경우 동일한 번호의 LED가 ON
- KeyPad 버튼을 누르지 않을 경우 LED 전체 OFF
< keypad1.c >
#include <wiringPi.h>
const int LedRed[8] = { 4, 17, 18, 27, 22, 23, 24, 25};
const int Keypad[8] = { 16, 13, 12, 6, 21, 26, 20, 19};
// KeyPad 번호 읽어오기
int KeypadRead(){
int i, keypadnum = -1;
for(i=0; i<8; i++){
//
if(!digitalRead(Keypad[i])){
keypadnum = i;
break;
}
}
return keypadnum;
}
void LedControl(int keypadnum){
int i;
for(i=0; i<8; i++){
if(i==keypadnum)
digitalWrite(LedRed[i], HIGH);
else
digitalWrite(LedRed[i], LOW);
}
}
int main(void){
int i, keypadnum = -1;
if(wiringPiSetupGpio()==-1)
return 1;
for(i=0; i<8; i++){
pinMode(LedRed[i], OUTPUT);
digitalWrite(LedRed[i], LOW);
}
for(i=0; i<8; i++)
pinMode(Keypad[i], INPUT);
while(1){
keypadnum = KeypadRead();
LedControl(keypadnum);
}
return 0;
}
KeyPad 버튼에 해당하는 LED ON/OFF
- Key 4 선택 시 LED 1~8의 순서로 ON/OFF
- Key 5 선택 시 LED 8~1의 순서로 ON/OFF
< keypad2.c >
#include <wiringPi.h>
// KeyPad 번호 읽어오기
int KeypadRead(){
int i, keypadnum = -1;
for(i=0; i<2; i++){
//
if(!digitalRead(Keypad[i])){
keypadnum = i;
break;
}
}
return keypadnum;
}
void LedControl(int keypadnum){
int i;
for(i=0; i<8; i++){
if(i==keypadnum)
digitalWrite(LedRed[i], HIGH);
else
digitalWrite(LedRed[i], LOW);
}
}
int main(void){
int i, keypadnum = -1;
if(wiringPiSetupGpio()==-1)
return 1;
for(i=0; i<8; i++){
pinMode(LedRed[i], OUTPUT);
digitalWrite(LedRed[i], LOW);
}
for(i=0; i<8; i++)
pinMode(Keypad[i], INPUT);
while(1){
kepadnum = KeypadRead();
if(keypadnum==0){
for(i=0;i<8;i++){
LedControl(i);
delay(500):
}
digitalWrite(Led[i-1], LOW); // LED[7] 한번 쉬기
}
else if(keypadnum==1){
for(i=7;i>=0;i--){
LedControl(i);
delay(500):
}
digitalWrite(Led[i+1], LOW); // LED[1] 한번 쉬기
}
}
return 0;
}
< keypad3.c >
#include <wiringPi.h>
const int LedRed[8] = { 4, 17, 18, 27, 22, 23, 24, 25};
const int Keypad[8] = { 16, 13, 12, 6, 21, 26, 20, 19};
// Debouncing
int debounceDelay = 50;
unsigned long lastDebounceTime[8] = {0, 0, 0, 0, 0, 0, 0, 0};
int buttonstate[8] = {1, 1, 1, 1, 1, 1, 1, 1};
int lastButtonState[8]= {1, 1, 1, 1, 1, 1, 1, 1};
// KeyPad 번호 읽어오기
int KeypadRead(){
int i, reading, keypadnum = -1;
for(i=0; i<8; i++){
reading = digitalRead(Keypad[i]);
// Button의 변화가 있으면 변화 시간을 저장
if (reading != lastButtonState[i])
lastDebounceTime[i] = millis();
// DebounceDelay이상일 때만 실행
if ((millis() - lastDebounceTime) > debounceDelay){
if(!digitalRead(Keypad[i])){
keypadnum = i;
lastButtonState[i] = reading;
break;
}
}
}
return keypadnum;
}
void LedControl(int keypadnum){
int i;
for(i=0; i<8; i++){
if(i==keypadnum)
digitalWrite(LedRed[i], HIGH);
else
digitalWrite(LedRed[i], LOW);
}
}
int main(void){
int i, keypadnum = -1;
if(wiringPiSetupGpio()==-1)
return 1;
for(i=0; i<8; i++){
pinMode(LedRed[i], OUTPUT);
digitalWrite(LedRed[i], LOW);
}
for(i=0; i<8; i++)
pinMode(Keypad[i], INPUT);
while(1){
keypadnum = KeypadRead();
LedControl(keypadnum);
}
return 0;
}