Skip to content

Update piano example #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions examples/piano.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# when an input is touched. Note only one note is played at a time!
# For use with microcontrollers or computers with PWM support only!
# Author: Tony DiCola
import time
# Modified by: Carter Nelson

import board
import busio
Expand Down Expand Up @@ -43,31 +43,19 @@
buzzer = pulseio.PWMOut(BUZZER_PIN, duty_cycle=TONE_OFF_DUTY, frequency=440,
variable_frequency=True)

# Main loop.
# First grab an initial touch state for all of the inputs. The touched()
# function can quickly get the state of all input pins and returns them as a
# 12-bit value with a bit set to 1 for each appropriate input (i.e. bit 0 is
# input 0, bit 1 is input 1, etc.)
last = mpr121.touched()
last_note = None
while True:
# Every loop iteration get an updated touch state and look to see if it
# changed since the last iteration.
current = mpr121.touched()
if last != current:
# Some pin changed, turn off playback and look for any touched pins.
# Get touched state for all pins
touched = mpr121.touched_pins
# If no pins are touched, be quiet
if True not in touched:
last_note = None
buzzer.duty_cycle = TONE_OFF_DUTY
# Loop through all 12 inputs (0-11) and look at their bits in the
# current touch state. A bit that's set is touched!
for i in range(12):
if (1 << i) & current > 0:
print('Input {} touched!'.format(i))
# Grab the frequency for the associated pin and check that it's
# not zero (unused).
freq = NOTE_FREQS[i]
if freq != 0:
# Pin with a specified frequency was touched, play the tone!
buzzer.frequency = NOTE_FREQS[i]
buzzer.duty_cycle = TONE_ON_DUTY
# Update touch state and delay a bit before next loop iteration.
last = current
time.sleep(0.01)
continue
# Get index of touched pin
note = touched.index(True)
# Play note if pin is different and has a defined note
if note != last_note and NOTE_FREQS[note] != 0:
last_note = note
buzzer.frequency = NOTE_FREQS[note]
buzzer.duty_cycle = TONE_ON_DUTY