def play_melody(melody): for (notename, eigths) in melody: length = eigths * 0.1 if notename: light_random_neopixel() cp.play_tone(note(notename), length) else: time.sleep(length)
def alarm(): while not cp.button_b: cp.play_tone(262, 1) pixels.fill((255, 0, 0)) time.sleep(0.1) pixels.fill((0, 0, 255)) time.sleep(0.1) pixels.fill((0, 0, 0))
def laskoFanInfrared(): pulseIn.pause() # pauses IR detection cp.red_led = True pulseOut.send(pulseArrayFan) # sends IR pulse cp.play_tone(750, 0.3) time.sleep(0.2) # wait so pulses don't run together pulseIn.clear() # clear detected pulses cp.red_led = False pulseIn.resume() # resumes IR detection
def show_location(a, v): pixel = 9 if a < 1000: pixel = int(a / 100) if a < 0: pixel = 0 color = red tone = red_tone if v > 0: color = white tone = white_tone if v <= final_velocity: color = blue tone = blue_tone cp.pixels.fill(0) cp.pixels[pixels[pixel]] = color if pixel != last_pixel: cp.play_tone(tone, 0.2) last_ppixel = pixel
#import libraries import time import board from adafruit_circuitplayground import cp from every import Every #set up tone_interval = Every( 5.0 ) #create the interval timer #loop while True: if tone_interval(): #remember the () to check the time. tone_interval without () is always True, because it exists. cp.play_tone( 440, 2.0 ) #play a tone at 440 Hz for 2 seconds #slow the loop only in one place time.sleep( 0.01 )
# set colors def set_color(bpm): i = int(bpm / 30) c = int((bpm / 10) % 3) cp.pixels.fill(0) cp.pixels[i] = colors[c] # main loop delay = get_delay(bpm) set_color(bpm) while True: if cp.button_a: bpm = set_bpm(bpm - 10) delay = get_delay(bpm) set_color(bpm) if cp.button_b: bpm = set_bpm(bpm + 10) delay = get_delay(bpm) set_color(bpm) if cp.switch == False: cp.pixels.brightness = 0.5 cp.play_tone(tone, 0.1) cp.pixels.brightness = 0 time.sleep(delay)
#import libraries import time import board from adafruit_circuitplayground import cp from every import Every #set up warning_interval = Every(0.8) #create the interval timer warning_color = (255, 0, 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.play_tone(880, 0.5) #play a tone cp.pixels.fill(warning_color) time.sleep(0.05) else: cp.pixels.fill(OFF) #slow the loop only in one place time.sleep(0.01)
# Copyright (c) 2020 Rick Leander All rights reserved # Buy the book at https://www.amazon.com/author/rleander# # # use accelerometer to play sounds from adafruit_circuitplayground import cp # notes and frequencies C = 261.63 D = 293.66 E = 329.63 F = 349.23 G = 392.00 A = 440.00 B = 493.88 notes = [ C / 2, D / 2, E / 2, F / 2, G / 2, A / 2, B / 2, C, D, E, F, G, A, B, C * 2, D * 2, E * 2, F * 2, G * 2, A * 2, B * 2, C * 3 ] # main loop while True: if cp.switch == False: [x, y, z] = cp.acceleration i = 11 - int(x) delay = 0.25 + (y / 50.0) cp.play_tone(notes[i], delay)
# Demo keyboard module # Turn the switch on to begin import time from adafruit_circuitplayground import cp # Super loop while True: if cp.switch: cp.pixels.fill((0, 0, 0)) if cp.touch_A1: cp.pixels[5] = (255, 0, 0) cp.pixels[6] = (200, 128, 0) cp.play_tone(395, 1) if cp.touch_A2: cp.pixels[7] = (100, 180, 0) cp.pixels[8] = (100, 180, 0) cp.play_tone(418, 1) if cp.touch_A3: cp.pixels[8] = (0, 255, 0) cp.pixels[9] = (0, 255, 0) cp.play_tone(469, 1) if cp.touch_A4: cp.pixels[0] = (0, 180, 128) cp.pixels[1] = (0, 180, 128) cp.play_tone(527, 1) if cp.touch_A5: cp.pixels[1] = (0, 60, 200) cp.pixels[2] = (0, 60, 200) cp.play_tone(558, 1)
# Circuit Playground Temperature # Reads the on-board temperature sensor and prints the value import time from adafruit_circuitplayground import cp while True: temp_c = cp.temperature temp_f = cp.temperature * 9 / 5 + 32 if temp_c > 35: print("Risk of wildfire in area") cp.play_tone(4000, 1) else: print("Temperature : %f C / %f F, No risk of Fire" % (temp_c, temp_f)) time.sleep(0.25)
import simpleio #loop while True: #in here I want to read the accelerometer and get a number that tells me how how much total acceleration there is right now. x, y, z = cp.acceleration sum_accel = abs(x) + abs(y) + abs(z) #print((sum_accel, 2)) #the range of sum_accel is about 0 to 150 accel_min = 0 accel_max = 150 #in here I want to make a random range of tone duration. short_duration = 0.1 long_duration = 0.6 tone_duration = random.uniform(short_duration, long_duration) #print( (0, tone_duration) ) #in here I want to make a random range of tone pitch. low_pitch = 300 high_pitch = 1000 #tone_pitch = random.uniform(low_pitch, high_pitch) #print( (tone_duration, tone_pitch) ) tone_pitch = simpleio.map_range( sum_accel, accel_min, accel_max, low_pitch, high_pitch ) print( (300, tone_pitch) ) cp.play_tone( tone_pitch, tone_duration ) #play a tone at 440 Hz for 2 seconds #slow the loop only in one place time.sleep( 0.1 )
def beep(): cp.red_led = True cp.play_tone(262, 0.05) cp.red_led = False
def play_sound(): cp.play_tone(1000, 5.0)
while True: if not active_connection: # There's no connection, so let's scan for one cp.pixels[0] = (60, 40, 0 ) # set CPB NeoPixel 0 to yellow while searching print("Scanning for Magic Light...") # Scan and filter for advertisements with ProvideServicesAdvertiesment type for advertisement in radio.start_scan(ProvideServicesAdvertisement): # Filter further for advertisements with MagicLightService if MagicLightService in advertisement.services: active_connection = radio.connect(advertisement) print("Connected to Magic Light") cp.pixels[0] = (0, 0, 255 ) # Set NeoPixel 0 to blue when connected # Play a happy tone cp.play_tone(440, 0.1) cp.play_tone(880, 0.1) print("Adjust slide potentiometers to mix RGB colors") try: bulb = active_connection[MagicLightService] except _bleio.ConnectionError: print("disconnected") continue break radio.stop_scan() # Now that we're connected, stop scanning while active_connection.connected: # Connected, now we can set attrs to change colors # Toggle slide switch to go to half or full brightness if cp.switch: cp.red_led = True dimmer = 0.5
# Python code for Make Music with the Circuit Playground Express # by Rick Leander # Copyright (c) 2020 Rick Leander All rights reserved # Buy the book at https://www.amazon.com/author/rleander# # play the C major scale from adafruit_circuitplayground import cp tempo = 0.5 cp.play_tone(261.63, tempo) cp.play_tone(293.66, tempo) cp.play_tone(329.63, tempo) cp.play_tone(349.23, tempo) cp.play_tone(392.00, tempo) cp.play_tone(440.00, tempo) cp.play_tone(493.88, tempo) cp.play_tone(523.26, tempo)
""" """ #turn on LED for n in range(0,10,1): cp.pixels.brightness = random.random() r=random.randint(0,255) g=random.randint(0,255) b=random.randint(0,255) cp.pixels[n] = (r, g, b) time.sleep(1) """ """ #play wavfile cp.play_file("jemaine-ohno.wav") """ """ #play tone for hal second cp.play_tone(100, 0.5) """ """ #turn on/off Red light while True: cp.red_led = True time.sleep(0.5) cp.red_led = False time.sleep(0.5) """ """ #detect shake while True: if cp.shake(shake_threshold=10):
from adafruit_circuitplayground import cp import time while True: time.sleep(0.1) x, y, z = cp.acceleration acceleration = (x**2 + y**2 + z**2)**0.5 print(acceleration) if acceleration > 20: for i in range(3): cp.pixels.fill((255, 0, 0)) cp.play_tone(500, 0.25) cp.pixels.fill((0, 0, 0)) cp.play_tone(250, 0.25)
from adafruit_circuitplayground import cp cp.play_tone(440, 1)
def arp(): cp.play_tone(PITCH_C * 2**0, 0.06) cp.play_tone(PITCH_G * 2**0, 0.06) cp.play_tone(PITCH_D * 2**1, 0.06) cp.play_tone(PITCH_A * 2**1, 0.06) cp.play_tone(PITCH_E * 2**2, 0.06) cp.play_tone(PITCH_B * 2**2, 0.06)
def play_chord(chord): for note in chord: cp.play_tone(note, tempo)
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
else: # this means CPX is in actual service mode # sentry mode is now enabled and timer value is increased to 30 sec timerVal = 30 if (monotonic - sentryTime) >= 0.5: # print("sentry check") sentry_check() sentryTime = time.monotonic() if (monotonic - tm) > timerVal: tm = time.monotonic() trashLevel = getTrashLevel() if not math.isnan(trashLevel): print("checking trash level: service = " + str(trashLevel)) LED = displayLights(trashLevel) light_time = time.monotonic() if cp.button_a: fullheight = getTrashLevel() red = fullheight * 0.2 yellow = fullheight * 0.4 cp.play_tone(292, 1) if LED and (monotonic - light_time) > 0.5 : # print("light time: " + str(monotonic - light_time) + " And LED = " + str(LED)) LED = lightsOFF() light_time = time.monotonic() time.sleep(0.05)
# Dark seems to be 200+ LOW_NOISE = 90 MEDIUM_NOISE = 40 HIGH_NOISE = 20 SCARED_NOISE = 5 # Super loop while True: if cp.switch: print(cp.light) if cp.light < SCARED_NOISE: print("Scared") cp.pixels.fill((0, 255, 0)) cp.play_tone(NOTE_C8, 1) cp.play_tone(NOTE_B7, 1) elif cp.light < HIGH_NOISE: print("High") cp.pixels.fill((0, 255, 0)) cp.play_tone(NOTE_C6, 1) cp.play_tone(NOTE_B5, 1) elif cp.light < MEDIUM_NOISE: print("Medium") cp.pixels.fill((0, 255, 0)) cp.play_tone(NOTE_C4, 1) cp.play_tone(NOTE_B3, 1) elif cp.light < LOW_NOISE: print("Low") cp.pixels.fill((0, 255, 0)) cp.play_tone(NOTE_C2, 1)
def show_mole(mole): cp.pixels.fill(0) cp.pixels[pixels[mole]] = colors[mole] cp.play_tone(tones[mole], note_time)
"""This example plays two tones for 1 second each. Note that the tones are not in a loop - this is to prevent them from playing indefinitely!""" from adafruit_circuitplayground import cp cp.play_tone(262, 1) cp.play_tone(294, 1)
import time import board import analogio import random # from progspace_room import Room from adafruit_circuitplayground import cp # room = Room(use_debug=True) while True: while True: # while room.connected(): time.sleep(2 + random.random() * 5) cp.play_tone(240 + random.randint(0, 60), random.random() * 0.4) cp.play_tone(240 + random.randint(0, 60), random.random() * 0.4) cp.play_tone(240 + random.randint(0, 60), random.random() * 0.4)
from adafruit_circuitplayground import cp import time while True: if cp.button_a: print("Button A pressed!") cp.red_led = False cp.pixels.fill(0xFF0000) cp.play_tone(1000, 0.5) if cp.button_b: print("Button B pressed!") cp.red_led = True cp.pixels.fill(0) time.sleep(0.1)
from adafruit_circuitplayground import cp while True: if cp.shake(50): print("Hard shake Detected") cp.pixels.fill((255, 0, 0)) cp.play_tone(440, 10.0)
def play_note(note): cp.pixels.fill(0) cp.pixels[pixels[note]] = colors[note] cp.play_tone(tones[note], note_time)
#import libraries import time import board from adafruit_circuitplayground import cp from every import Every #set up tone_interval = Every( 0.8 ) #create the interval timer #loop while True: if tone_interval(): #remember the () to check the time. tone_interval without () is always True, because it exists. cp.play_tone( 880, 0.5 ) #play a tone at 440 Hz for 2 seconds #slow the loop only in one place time.sleep( 0.01 )