lab2-3-1.c
970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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 };
int LedON[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int lastKey[8] = { 1, 1, 1, 1, 1, 1, 1, 1 };
int KeypadRead() {
int i, keypadnum=-1;
for(i=0; i<8; i++) {
if(!digitalRead(Keypad[i]) && lastKey[i]==1) {
keypadnum = i;
lastKey[i]=0;
break;
}
else if(digitalRead(Keypad[i]) && lastKey[i]==0)
lastKey[i]=1;
}
return keypadnum;
}
void LedControl(int keypadnum) {
int i;
for(i=0; i<8; i++) {
if(i==keypadnum) {
LedON[i]=!LedON[i];
digitalWrite(LedRed[i], LedON[i]);}
}
}
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], LedON[i]);
}
for(i=0; i<8; i++)
pinMode(Keypad[i], INPUT);
while(1) {
keypadnum = KeypadRead();
LedControl(keypadnum);
}
return 0;
}