-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add comments to all GPIO Zero examples.
- Loading branch information
Showing
4 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,44 @@ | ||
# Import the necessary libraries. | ||
# gpiozero contains our JamHat object. | ||
from gpiozero import JamHat | ||
from time import sleep | ||
|
||
# Initialise the JamHat object. | ||
jh = JamHat() | ||
|
||
# Build an array of notes in hertz. | ||
NOTES = [440.000, 391.995, 349.228, 329.628, 293.665, 261.626] | ||
|
||
# Initialise our counters for the while loop below. | ||
i = 0 | ||
j = 0 | ||
note = 0 | ||
|
||
# Setup a try/except block so we can run until CTRL+C is pressed. | ||
try: | ||
while True: | ||
if(jh.button_2.is_pressed): | ||
# If the second button is pressed, increment the counter into our NOTES array. | ||
note = (note + 1) % 6 | ||
# Using modular arithmetic, decrease the light counter. | ||
# Our lights are in a matrix with rows 0-1 and columns 0-2. | ||
# Eg. [0][1] is the top yellow LED. | ||
if(j == 2): | ||
# If we're at the end of the row, increment to the next row. | ||
i = (i + 1) % 2 | ||
# Increment the column. | ||
j = (j + 1) % 3 | ||
sleep(0.1) | ||
# Turn off the hat | ||
jh.off() | ||
# Turn on the next light in the i/jth slot. | ||
jh[i][j].on() | ||
|
||
if(jh.button_1.is_pressed): | ||
# If the button is pressed, play the current note from NOTES out of the buzzer. | ||
jh.buzzer.play(NOTES[note]) | ||
sleep(0.1) | ||
|
||
except KeyboardInterrupt: | ||
# If someone presses CTRL+C, close the JamHat, freeing of the Pins for use elsewhere. | ||
jh.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
# Import the necessary libraries. | ||
# gpiozero contains our JamHat object. | ||
from gpiozero import JamHat | ||
from time import sleep | ||
|
||
# Initialise the JamHat object. | ||
jh = JamHat() | ||
|
||
# Setup a try/except block so we can run until CTRL+C is pressed. | ||
try: | ||
while True: | ||
if(jh.button_1.is_pressed): | ||
# If the button on the left is pressed, beep midi note 80. | ||
jh.buzzer.play(80) | ||
elif(jh.button_2.is_pressed): | ||
# If the button on the right is pressed, beep midi note 60. | ||
jh.buzzer.play(60) | ||
else: | ||
# Turn off the buzzer. | ||
jh.off() | ||
except: | ||
except KeyboardInterrupt: | ||
# If someone presses CTRL+C, close the JamHat, freeing of the Pins for use elsewhere. | ||
jh.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
# Import the necessary libraries. | ||
# gpiozero contains our JamHat object. | ||
from gpiozero import JamHat | ||
from time import sleep | ||
|
||
# Initialise the JamHat object. | ||
jh = JamHat() | ||
|
||
# Setup a try/except block so we can run until CTRL+C is pressed. | ||
try: | ||
# Initialise counter | ||
counter = 0 | ||
while True: | ||
if jh.button_1.is_pressed or jh.button_2.is_pressed: | ||
# If either button is pressed... | ||
if jh.button_1.is_pressed: | ||
# If the left button is pressed, increment the counter. | ||
counter += 1 | ||
# Wait until the button is released before moving on. | ||
jh.button_1.wait_for_release() | ||
if jh.button_2.is_pressed: | ||
# If the right button is pressed, increment the counter. | ||
counter -= 1 | ||
# Wait until the button is released before moving on. | ||
jh.button_2.wait_for_release() | ||
# Print out the counter value to the screen. | ||
print("Counter: %d" % counter) | ||
sleep(0.1) | ||
except: | ||
except KeyboardInterrupt: | ||
# If someone presses CTRL+C, close the JamHat, freeing of the Pins for use elsewhere. | ||
jh.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,33 @@ | ||
# Import the necessary libraries. | ||
# gpiozero contains our JamHat object. | ||
from gpiozero import JamHat | ||
from time import sleep | ||
|
||
# Initialise the JamHat object. | ||
jh = JamHat() | ||
|
||
# i is the counter for the LED row. | ||
i = 0 | ||
# j is the counter for the LED column. | ||
j = 0 | ||
|
||
# Setup a try/except block so we can run until CTRL+C is pressed. | ||
try: | ||
while True: | ||
# Using modular arithmetic, decrease the light counter. | ||
# Our lights are in a matrix with rows 0-1 and columns 0-2. | ||
# Eg. [0][1] is the top yellow LED. | ||
if(j == 2): | ||
# If we're at the end of the column, increment to the next row. | ||
i = (i + 1) % 2 | ||
# Increment the column. | ||
j = (j + 1) % 3 | ||
sleep(0.2) | ||
# Turn the hat off. | ||
jh.off() | ||
# Turn on the LED at i j on the board. | ||
jh[i][j].on() | ||
|
||
except: | ||
except KeyboardInterrupt: | ||
# If someone presses CTRL+C, close the JamHat, freeing of the Pins for use elsewhere. | ||
jh.close() |