forked from tdamdouni/Raspberry-Pi-DIY-Projects
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrafficKit02.py
100 lines (84 loc) · 2.27 KB
/
TrafficKit02.py
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
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# Traffic Lights Demo Sequence
# Runs until Ctrl/C is pressed
# Pressing button skips to Red
# Must be run as root - sudo python TrafficKit02.py
import time, random, RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
LED_RED = 7
LED_AMBER = 11
LED_GREEN = 13
BUTTON = 22
LEDOFF = 0
LEDON = 1
def setupgpio():
GPIO.setup(LED_RED, GPIO.OUT)
GPIO.setup(LED_AMBER, GPIO.OUT)
GPIO.setup(LED_GREEN, GPIO.OUT)
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def alloff (val):
GPIO.output (LED_RED, LEDOFF)
GPIO.output (LED_AMBER, LEDOFF)
GPIO.output (LED_GREEN, LEDOFF)
time.sleep(val)
def allon (val):
GPIO.output (LED_RED, LEDON)
GPIO.output (LED_AMBER, LEDON)
GPIO.output (LED_GREEN, LEDON)
time.sleep(val)
def red (val):
GPIO.output (LED_RED, LEDON)
GPIO.output (LED_AMBER, LEDOFF)
GPIO.output (LED_GREEN, LEDOFF)
time.sleep(val)
def redamber (val):
GPIO.output (LED_RED, LEDON)
GPIO.output (LED_AMBER, LEDON)
GPIO.output (LED_GREEN, LEDOFF)
time.sleep(val)
def amber (val):
GPIO.output (LED_RED, LEDOFF)
GPIO.output (LED_AMBER, LEDON)
GPIO.output (LED_GREEN, LEDOFF)
time.sleep(val)
def green (val):
GPIO.output (LED_RED, LEDOFF)
GPIO.output (LED_AMBER, LEDOFF)
GPIO.output (LED_GREEN, LEDON)
time.sleep(val)
def flashall (val):
count = 0
while count < val:
allon(0.1)
alloff(0.1)
count += 1
def basicSequence (val):
count = 0
while count < val:
while count < val:
red (4)
redamber (1)
if (GPIO.input(BUTTON) == 0): # If the button is pressed go to start of sequence
break
green (1)
if (GPIO.input(BUTTON) == 0): # If the button is pressed go to start of sequence
break
green (1)
if (GPIO.input(BUTTON) == 0): # If the button is pressed go to start of sequence
break
green (1)
if (GPIO.input(BUTTON) == 0): # If the button is pressed go to start of sequence
break
amber (1)
red (0)
count += 1
try:
while True:
setupgpio()
alloff(0)
flashall(5)
basicSequence (5)
except KeyboardInterrupt:
GPIO.cleanup()
exit()