-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProject03_SelectorOutput.py
100 lines (74 loc) · 3.71 KB
/
Project03_SelectorOutput.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
# Project 3
# Learning to program, writing functions, and using inputs and outputs
# Build the the Project 3 circuit and control a LED and buzzer with a selector
# Press and hold buttons A, B, and C on the selector
#Challenge 1
# Try changing the Pin_On and Pin_Off variables to change the blinking pattern
#Challenge 2
# Replace the color LED with buzzer or white LED to try other outputs
#Challenge 3
# Try changing input pins A and C in the While loop to switch what A and C do when pressed
#Challenge 4
# Try changing output pins LED and Buzzer in the While loop to switch what A and C do when pressed
#Challenge 5
# Try switching the order of the LED and Buzzer functions for a cool lightshow when pressing B
#Importing libraries
# Here we want the sleep function for timing and GPIO for the Pi's pins
from time import sleep
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
#Let's define variables so we can use them later
A_Pin = 7 #the internal Pi pin number that goes to snap 7
C_Pin = 18 #the internal Pi pin number that goes to snap 6
LED_Pin = 26 #the internal Pi pin number that goes to snap 3
Buzzer_Pin = 21 #the internal Pi pin number that goes to snap 4
# For challenge 1, we can try different values here to blink in new patterns
Pin_On = 3 #duration of LED flash, seconds
Pin_Off = 0.5 #duration in between flashes, seconds
#Setting up our pins
GPIO.setmode(GPIO.BOARD)
#Our output pins, start off
GPIO.setup(LED_Pin, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(Buzzer_Pin, GPIO.OUT, initial=GPIO.LOW)
#Our input pins from the selector
GPIO.setup(A_Pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C_Pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#Let's write some functions we can use to make the coding easier
# For a code snippet we will reuse, we can turn it into a function to call later
# The function name is in blue, and then the arguments it takes are in parentheses
#Here's a function for seeing if a selector button is pressed
# So, read_selector_button reads and returns the value of In_Pin
# This will be helpful for reading the A and C button pins
def read_selector_button(In_Pin):
return GPIO.input(In_Pin)
#Here's a function for turning an output pin on
#So, output_pin_on takes in the pin number and turns it on after a defined delay
def output_pin_on(Out_Pin, Delay):
sleep(Delay)
GPIO.output(Out_Pin, GPIO.HIGH)
#Here's a function for turning an output pin off, can you fill in the missing pieces?
# Replace the ?? with the variables and then uncomment
def output_pin_off(Out_Pin, Delay):
sleep(??) #wait the Delay
GPIO.output(??, GPIO.LOW) #turn the Out_Pin off
while True: #Looping over and over again
# Here we can use the functions we defined to read buttons and control outputs
# For the challenges, try changing the button and output pins in the below code
# If A is pressed and C is not, let's blink the LED
if read_selector_button(A_Pin) and not(read_selector_button(C_Pin)):
output_pin_on(LED_Pin, Pin_On)
output_pin_off(LED_Pin, Pin_Off)
# If C is pressed and A is not, let's buzz the buzzer
if read_selector_button(C_Pin) and not(read_selector_button(A_Pin)):
output_pin_on(Buzzer_Pin, Pin_On)
output_pin_off(Buzzer_Pin, Pin_Off)
# If A and C are both pressed, by pressing B, maybe we can flash both LED and buzzer?
# Replace the ?? with the LED_Pin and Buzzer_Pin variables and then uncomment
if read_selector_button(A_Pin) and read_selector_button(C_Pin):
output_pin_on(??, Pin_On) #LED On
output_pin_off(??, Pin_Off) #LED Off
output_pin_on(??, Pin_On) #Buzzer On
output_pin_off(??, Pin_Off) #Buzzer Off
# Wait 1 second to reset
sleep(1)
GPIO.cleanup()