lab2-3-2_new.c
1.64 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Wiring Pi Library
#include <wiringPi.h>
const int LedRed[8] = { 4, 17, 18, 27, 22, 23, 24, 25 };
const int Keypad[5] = { 6, 12, 13, 16, 19 };
int state = 0;
int KeypadRead()
{
int i, keypadnum = -1;
for (i = 0; i < 5; i++) {
if (!digitalRead(Keypad[i])) {
state = 1;
keypadnum = i; break;
}
}
return keypadnum;
}
void LedControl(int num)
{
int i;
for (i = 0; i < 8; i++) {
if (i == num)
digitalWrite(LedRed[i], HIGH);
else
digitalWrite(LedRed[i], LOW);
}
}
void stop() {
LedControl(-1);
state = 0;
}
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 < 5; i++)
pinMode(Keypad[i], INPUT);
if (wiringPiISR(Keypad[2], INT_EDGE_FALLING, &stop) < 0) {
return 1;
}
while (1) {
keypadnum = KeypadRead();
if (keypadnum == 0) {
while (1) {
for (i = 0; i < 8; i++) {
if (state == 0) break;
LedControl(i);
delay(500);
}
digitalWrite(LedRed[i - 1], LOW);
if (state == 0) break;
}
}
else if (keypadnum == 1) {
while (1) {
for (i = 7; i >= 0; i--) {
if (state == 0) break;
LedControl(i);
delay(500);
}
digitalWrite(LedRed[i + 1], LOW);
if (state == 0) break;
}
}
else if (keypadnum == 3) {
for (i = 0; i < 8; i++) {
if (state == 0) break;
LedControl(i);
delay(500);
}
digitalWrite(LedRed[i - 1], LOW);
}
else if (keypadnum == 4) {
for (i = 7; i >= 0; i--) {
if (state == 0) break;
LedControl(i);
delay(500);
}
digitalWrite(LedRed[i + 1], LOW);
}
}
return 0;
}