-
Notifications
You must be signed in to change notification settings - Fork 2
/
trafficlights.c
61 lines (48 loc) · 995 Bytes
/
trafficlights.c
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
#include <signal.h>
#include <unistd.h>
#include "gpiosysfs.h"
#define GPIO_LOW "0"
#define GPIO_HIGH "1"
#define RED "9"
#define YELLOW "10"
#define GREEN "11"
static void allLightsOff();
static void interruptHandler(const int);
static void allLightsOff() {
gpioWrite(RED, GPIO_LOW);
gpioWrite(YELLOW, GPIO_LOW);
gpioWrite(GREEN, GPIO_LOW);
}
void interruptHandler(const int signal) {
allLightsOff();
gpioCleanup(RED);
gpioCleanup(YELLOW);
gpioCleanup(GREEN);
exit(0);
}
int main(void) {
signal(SIGINT, interruptHandler);
gpioSetup(RED);
gpioSetup(YELLOW);
gpioSetup(GREEN);
allLightsOff();
while(1) {
// Red
gpioWrite(RED, GPIO_HIGH);
sleep(3);
// Red and Yellow
gpioWrite(YELLOW, GPIO_HIGH);
sleep(1);
// Green
gpioWrite(RED, GPIO_LOW);
gpioWrite(YELLOW, GPIO_LOW);
gpioWrite(GREEN, GPIO_HIGH);
sleep(5);
// Yellow
gpioWrite(GREEN, GPIO_LOW);
gpioWrite(YELLOW, GPIO_HIGH);
sleep(2);
// Yellow off
gpioWrite(YELLOW, GPIO_LOW);
}
}