def change_tone_if_needed(self): """Find the frequency for the current arpeggio and tilt, and restart the tone if changed""" arpeggio_index = 0 if cp.switch else 1 freq = self.freq_maker.freq(TiltingArpeggios.tilt(), arpeggio_index) if freq != self.last_freq: self.last_freq = freq cp.stop_tone() cp.start_tone(freq)
def Update( self ): #update the state of this button. Needed to incriment timer in button. slide = Button.CURRENT_ACTIVATED is not None and Button.LAST_ACTIVATED is self #If there was a button activated last frame that wasn't us. if self.touchCondition( ) and not Button.CURRENT_ACTIVATED: # this is touched and no button is the current touched button self.holdTime += 0.01 Button.CURRENT_ACTIVATED = self if (self.state == ButtonStates.UP and not self.Pressed() ): #contact made but not long enough to count as a press time.sleep( 0.1) # Wait a frame and see if we're still being touched if ( self.touchCondition() ): #A little bit of debounce on sound (but not light) in case we get some noise self.state = ButtonStates.CONTACT Button.LAST_CONTACTED = self cp.stop_tone() cp.start_tone(100 + (50 * (MODE * 9 + self.order))) elif (self.state == ButtonStates.CONTACT and self.Pressed()): self.state = ButtonStates.JUST_PRESSED cp.stop_tone() time.sleep(0.01) elif self.state == ButtonStates.JUST_PRESSED: self.state = ButtonStates.PRESSED cp.start_tone(100 + (50 * (MODE * 9 + self.order))) elif self.state == ButtonStates.PRESSED and self.Held(): cp.stop_tone() time.sleep(0.01) cp.start_tone(100 + (50 * (MODE * 9 + self.order))) self.state = ButtonStates.DOWN else: self.holdTime = 0.0 if self.state == ButtonStates.DOWN: self.state = ButtonStates.JUST_RELEASED_HOLD if not slide: cp.stop_tone() elif self.state == ButtonStates.JUST_PRESSED or self.state == ButtonStates.PRESSED: self.state = ButtonStates.JUST_RELEASED_PRESS if not slide: cp.stop_tone() elif self.state == ButtonStates.JUST_RELEASED_HOLD or self.state == ButtonStates.JUST_RELEASED_PRESS: self.state = ButtonStates.RELEASED elif self.state == ButtonStates.CONTACT: if not slide: cp.stop_tone() self.state = ButtonStates.UP else: self.state = ButtonStates.UP
else: cp.pixels[index] = (0, 0, 0) # off sound_duration.start() while (1): # typical "loop" for "keep doing stuff" in circuit/micro python # Repeating if blink_interval(): cp.red_led = not cp.red_led # blink if long_short_blink(): # on is longer than off blink_neo(0) if fancy_blink_interval(): # a pattern of 4 intervals, then repeat blink_neo(1) # Timer/One-shots # in this example, these happen only once per run if sound_duration(): # the even/odd trick: if sound_duration.i % 2: # not playing: cp.start_tone(262) else: # playing, so: cp.stop_tone()
if a1 and a2: tone = D elif a2 and a3: tone = F elif a4 and a5: tone = B elif a1: tone = C elif a2: tone = E elif a3: tone = G elif a4: tone = A elif a5: tone = C * 2 if a6: tone = tone * 2 tone = tone * octave if tone != lastTone: if tone > 0: cp.start_tone(tone) else: cp.stop_tone() lastTone = tone time.sleep(0.1)
if neopixel_values is not None: start = neopixel_values.start if start > NEOPIXEL_BUF_LENGTH: continue data = neopixel_values.data data_len = min(len(data), NEOPIXEL_BUF_LENGTH - start) neopixel_buf[start:start + data_len] = data[:data_len] if neopixel_values.write_now: neopixel_write.neopixel_write(neopixel_out, neopixel_buf) if now_msecs - temp_last_update >= temp_svc.measurement_period: temp_svc.temperature = cp.temperature temp_last_update = now_msecs tone = tone_svc.tone if tone is not None: freq, duration_msecs = tone if freq != 0: if duration_msecs != 0: # Note that this blocks. Alternatively we could # use now_msecs to time a tone in a non-blocking # way, but then the other updates might make the # tone interval less consistent. cp.play_tone(freq, duration_msecs / 1000) else: cp.stop_tone() cp.start_tone(freq) else: cp.stop_tone() last_tone = tone
def trigger_degree(degree): (key, octave) = degree_to_key_octave(degree) cp.start_tone(key_note_map[key] * 2**octave)
def trigger_note(key, octave_offset): cp.start_tone(key_note_map[key] * 2**octave_offset) cp.pixels.fill(BLACK) (pixel_ix, color_ix) = key_pixel_color_map[key] cp.pixels.fill(BLACK) cp.pixels[pixel_ix] = colors[color_ix]
#set up warning_interval = Every(0.8) #create the interval timer warning_color = (255, 0, 0) flash_duration = Every(0.1, 0) # note the trailing 0 tone_duration = Every(0.5, 0) # note the trailing 0 OFF = (0, 0, 0) #loop while True: (a_x, a_y, a_z) = cp.acceleration #print((0, a_z)) if a_z < -0.5: if warning_interval( ): #remember the () to check the time. an object without () is always True, because it exists. cp.start_tone(880) # start a tone, non-blocking! cp.pixels.fill(warning_color) # you have to "start" timers flash_duration.start() tone_duration.start() # when to stop the tone if tone_duration(): cp.stop_tone() # when to turn off if flash_duration(): cp.pixels.fill(OFF) #slow the loop only in one place
'''import statements - use 2 lines of code below''' from adafruit_circuitplayground import cp import time '''sets the brightness of the LEDs- use the code below''' cp.pixels.brightness = 0.2 #to do: create a while loop that runs when it is true while True: #to do: create an if statement for cp.button_a if cp.button_a: #to do: access the 1st pixel like example 3 and set a color to it cp.pixels[0] = (200,0,0) #to do: use cp.start_tone to tell the device how long to ring a tone. Insert number from 0-400 cp.start_tone(409) #to do: create an if statement for cp.button_b elif cp.button_b: #to do: access the 2nd pixel like example 3 and set a color to it cp.pixels[1] = (0,255,0) #to do: use cp.start_tone to tell the device how long to ring a tone. Insert number from 0-400 cp.start_tone(409) #to do: create an else statement else: #to do: access the 1nd pixel like example 3 and set the color to (0,0,0) cp.pixels[0] = (0,0,0) #to do: access the 2nd pixel like example 3 and set the color to (0,0,0) cp.pixels[1] = (0,0,0) #stop the tone - use 1 line of the code below cp.stop_tone()
# SPDX-License-Identifier: MIT """ Circuit Playground Bluefruit Light-Up Tone Piano Touch the each of the touchpads around the outside of the board to play a tone for each pad. Touch A6 and TX at the same time to play the final tone in the octave. A0 is not a touchpad. """ from adafruit_circuitplayground import cp cp.pixels.brightness = 0.3 while True: if cp.touch_A1: cp.pixels.fill((255, 0, 0)) cp.start_tone(262) elif cp.touch_A2: cp.pixels.fill((210, 45, 0)) cp.start_tone(294) elif cp.touch_A3: cp.pixels.fill((155, 155, 0)) cp.start_tone(330) elif cp.touch_A4: cp.pixels.fill((0, 255, 0)) cp.start_tone(349) elif cp.touch_A5: cp.pixels.fill((0, 255, 255)) cp.start_tone(392) elif cp.touch_A6 and not cp.touch_TX: cp.pixels.fill((0, 0, 255)) cp.start_tone(440)
def play_note(note): cp.stop_tone() if cp.switch == False: cp.start_tone(tones[note])
return [tone, length] # play the song [tone1, length1] = get_note(notes1[0]) [tone2, length2] = get_note(notes2[0]) last_tone1 = 0 last_tone2 = 0 n1 = 0 n2 = 0 while n1 < len(notes1) or len2 < len(notes2): if tone1 != last_tone1: if tone1 > 0: cp.start_tone(tone1) if tone2 != last_tone2: if tone2 > 0: start_speaker(tone2) time.sleep(duration) last_tone1 = tone1 last_tone2 = tone2 length1 = length1 - 1 length2 = length2 - 1 if length1 <= 0: cp.stop_tone() n1 = n1 + 1 [tone1, length1] = get_note(notes1[n1])
print("GOT FULL MESSAGE") print(buffer) # buffer is full, process message prefix = bytearray([buffer[0]]) if prefix == b'L': print("PARSING LIGHT COMMAND") pixel_id = int(buffer[1]) if pixel_id >= 0 and pixel_id < N_PIXELS: r = int(buffer[2]) g = int(buffer[3]) b = int(buffer[4]) cp.pixels[pixel_id] = (r, g, b) elif prefix == b'T': print("PARSING TONE COMMAND") cp.start_tone( int.from_bytes(bytearray([buffer[1], buffer[2]]), 'little')) elif prefix == b'S': print("PARSING STOP_TONE COMMAND") cp.stop_tone() # done processing message buffer = [] STATE = "IDLE" else: print("IGNORING BYTE") data = uart.read(1) print("done reading data") # Write sensor values if time.monotonic() - last_sensor_reading > 0.1:
# https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/start-and-stop-tone from adafruit_circuitplayground import cp # Note frequencies from https://pages.mtu.edu/~suits/notefreqs.html C4 = 261.63 D4 = 293.66 E4 = 329.63 F4 = 349.23 G4 = 392.00 A4 = 440.00 B4 = 493.88 while True: if cp.touch_A1: cp.start_tone(C4) elif cp.touch_A2: cp.start_tone(D4) elif cp.touch_A3: cp.start_tone(E4) elif cp.touch_A4: cp.start_tone(F4) elif cp.touch_A5: cp.start_tone(G4) elif cp.touch_A6: cp.start_tone(A4) elif cp.touch_TX: cp.start_tone(B4) else: cp.stop_tone()