-
Notifications
You must be signed in to change notification settings - Fork 1
/
light_up_leds.py
147 lines (132 loc) · 4.42 KB
/
light_up_leds.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#defining the RPi's pins as Input / Output
import RPi.GPIO as GPIO
#importing the library for delaying command.
import time
class light_up_leds:
def __init__(self):
try:
#used for GPIO numbering
GPIO.setmode(GPIO.BOARD)
#defining the pins
self.green = 13
self.red = 11
self.blue = 15
#defining the pins as output
GPIO.setup(self.red, GPIO.OUT)
GPIO.setup(self.green, GPIO.OUT)
GPIO.setup(self.blue, GPIO.OUT)
except:
GPIO.cleanup()
def lightBlue(self): #blue 11
try:
GPIO.output(self.green, GPIO.HIGH)
GPIO.output(self.red, GPIO.HIGH)
GPIO.output(self.blue, GPIO.LOW)
except:
# the purpose of this part is, when you interrupt the code, it will stop the while loop and turn off the pins, which means your LED won't light anymore
GPIO.cleanup()
def lightGreen(self): #Green
try:
GPIO.output(self.green, GPIO.LOW)
GPIO.output(self.red, GPIO.HIGH)
GPIO.output(self.blue, GPIO.HIGH)
except:
# the purpose of this part is, when you interrupt the code, it will stop the while loop and turn off the pins, which means your LED won't light anymore
GPIO.cleanup()
def lightRed(self): #red
try:
GPIO.output(self.green, GPIO.HIGH)
GPIO.output(self.red, GPIO.LOW)
GPIO.output(self.blue, GPIO.HIGH)
except:
GPIO.cleanup()
def lightPurple(self): #blue
try:
GPIO.output(self.green, GPIO.HIGH)
GPIO.output(self.red, GPIO.LOW)
GPIO.output(self.blue, GPIO.LOW)
except:
# the purpose of this part is, when you interrupt the code, it will stop the while loop and turn off the pins, which means your LED won't light anymore
GPIO.cleanup()
def lightYellow(self):
try:
GPIO.output(self.green, GPIO.LOW)
GPIO.output(self.red, GPIO.LOW)
GPIO.output(self.blue, GPIO.HIGH)
except:
# the purpose of this part is, when you interrupt the code, it will stop the while loop and turn off the pins, which means your LED won't light anymore
GPIO.cleanup()
def lightLBlue(self):
try:
GPIO.output(self.green, GPIO.LOW)
GPIO.output(self.red, GPIO.HIGH)
GPIO.output(self.blue, GPIO.LOW)
except:
# the purpose of this part is, when you interrupt the code, it will stop the while loop and turn off the pins, which means your LED won't light anymore
GPIO.cleanup()
def lightWhite(self):
try:
GPIO.output(self.green, GPIO.LOW)
GPIO.output(self.red, GPIO.LOW)
GPIO.output(self.blue, GPIO.LOW)
except:
GPIO.cleanup()
def blink(self,color, blinks, interval):
try:
for x in range(0, blinks):
if color == 'white':
self.lightWhite()
elif color == 'red':
self.lightRed()
elif color == 'green':
self.lightGreen()
elif color == 'blue':
self.lightBlue()
elif color =='purple':
self.lightPurple()
elif color =='lightBlue':
self.lightLBlue()
time.sleep(interval/2)
self.off()
time.sleep(interval/2)
except:
GPIO.cleanup()
def destroy(self):
GPIO.cleanup()
print("destroying")
def off(self):
try:
GPIO.output(self.green, GPIO.HIGH)
GPIO.output(self.red, GPIO.HIGH)
GPIO.output(self.blue, GPIO.HIGH)
except KeyboardInterrupt:
GPIO.cleanup()
#a = light_up_leds()
#a.blink('red',3, 1)
#a.blink('green',3, 1)
#a.blink('blue',3, 1)
#a.blink('white',3, 1)
#a.blinkWhite(3, 0.5)
#a.lightRed()
#time.sleep(1)
#a.off()
#a.lightRed()
#time.sleep(1)
#time.sleep(2)
#a.lightGreen()
#time.sleep(2)
#a.lightBlue()
#time.sleep(2)
#a.lightLBlue()
#time.sleep(2)
#a.lightPurple()
#time.sleep(2)
#a.lightYellow()
#time.sleep(2)
#a.lightWhite()
#time.sleep(5)
#
##a.lightBlue()
##time.sleep(10)
#
#GPIO.cleanup()