Last active
February 19, 2025 18:36
-
-
Save gallaugher/99e5043a4cace76169a597913efda699 to your computer and use it in GitHub Desktop.
Should_stop_when_button_B_held_and_song_ends
This file contains hidden or 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
# sasa-chairs | |
# Note: This was demo code set up to show students how the debounced button press doesn't register until | |
# after the song plays. However for some reason I notice when run on a CPB using CircuitPython 9.2.4 | |
# that the song runs a second time, even after the update() has run, the button_B is pressed, and | |
# button_A: False, button_B: False prints <- not expected because my finger remains on button_B. | |
# Here is some sample output. I'm seeing that even after False / False, the sound play restarts & am unsure why. | |
# If I continue holding button_B through one more play, the sound stops, as expected, and button_B.value is True | |
# but I had expected this to occur earlier when I held B through the first complete sound play. | |
# If you'd like to test with my sound, it's in the sasa-chairs folder at: | |
# http://bit.ly/circuitpython-school-files | |
""" | |
button_A: True, button_B: False | |
Setting is_playing to True | |
About to play sound | |
Finished playing sound | |
button_A: False, button_B: False # button_B.value IS FALSE EVEN THOUGH MY FINGER IS DOWN ON button_B | |
About to play sound | |
Finished playing sound | |
button_A: False, button_B: True # I'VE KEPT MY FINGER ON BUTTON_B & IT'S FINALLY SEEN AS TRUE, BUT NOT 'TIL A SECOND PLAY | |
Button B detected - setting is_playing to False | |
button_A: False, button_B: False | |
""" | |
import board, digitalio | |
from adafruit_debouncer import Button | |
from audiopwmio import PWMAudioOut as AudioOut # for CPB & Pico | |
from audiocore import WaveFile # only needed for wav | |
# Debounced CPB Buttons | |
button_A_input = digitalio.DigitalInOut(board.BUTTON_A) | |
button_A_input.switch_to_input(digitalio.Pull.DOWN) # Note: Pull.UP for external buttons | |
button_A = Button(button_A_input, value_when_pressed = True) # NOTE: False for external buttons | |
button_B_input = digitalio.DigitalInOut(board.BUTTON_B) | |
button_B_input.switch_to_input(digitalio.Pull.DOWN) # Note: Pull.UP for external buttons | |
button_B = Button(button_B_input, value_when_pressed = True) # NOTE: False for external buttons | |
# set up the speaker | |
speaker = digitalio.DigitalInOut(board.SPEAKER_ENABLE) | |
speaker.direction = digitalio.Direction.OUTPUT | |
speaker.value = True | |
audio = AudioOut(board.SPEAKER) | |
# set path where sound files can be found CHANGE if different folder name | |
path = "sasa-chairs/" | |
# play_sound function - pass in the FULL NAME of file to play | |
def play_sound(filename): | |
with open(path + filename, "rb") as wave_file: | |
wave = WaveFile(wave_file) | |
audio.play(wave) | |
while audio.playing: | |
pass | |
print("SASA Chairs running") | |
is_playing = False | |
while True: | |
button_A.update() | |
button_B.update() | |
print(f"button_A: {button_A.pressed}, button_B: {button_B.pressed}") | |
if button_A.pressed: | |
print("Setting is_playing to True") | |
is_playing = True | |
if button_B.pressed: | |
print("Button B detected - setting is_playing to False") | |
is_playing = False | |
if is_playing: | |
print("About to play sound") | |
play_sound("musical-chairs.wav") | |
print("Finished playing sound") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the print & explanation.