def react_to_shake(): print("We are shaking!") set_right(0.0) set_left(0.0) attempts = 0 while True: if attempts == 3: return False if not cpx.shake(shake_threshold=SHAKE_THRESHOLD): return True # Back up set_right(-0.5) set_left(-0.5) time.sleep(1.0) # Try turning right set_right(0.25) set_left(LEFT_SPEED) # Turn for this amount of time time.sleep(random.choice([0.3, 0.4, 0.5])) attempts += 1
def tack(direction, duration): target_time = time.monotonic() + duration if direction == LEFT: set_left(0.25) set_right(RIGHT_SPEED) else: set_left(LEFT_SPEED) set_right(0.25) while time.monotonic() < target_time: if cpx.shake(shake_threshold=SHAKE_THRESHOLD): return react_to_shake() return True
# SPDX-FileCopyrightText: 2017 Kattni Rembor for Adafruit Industries # # SPDX-License-Identifier: MIT from adafruit_circuitplayground.express import cpx while True: if cpx.shake(shake_threshold=20): print("Shake detected!") cpx.red_led = True else: cpx.red_led = False
# tapping lights pixels[2] if cpx.tapped: cpx.pixels[2] = (0, 100, 0) # this captures just the button press, when button_b goes from false to true if cpx.button_b and not lastBtnB: btnBToggle = not btnBToggle lastBtnB = cpx.button_b # shaking turns off pixels[2] # btnBToggle determines how hard you need to shake if btnBToggle: cpx.pixels[7] = (0, 0, 100 ) # bright light indicates high shake threshold if cpx.shake(): cpx.pixels[2] = (0, 0, 0) else: cpx.pixels[7] = (0, 0, 10) # dim light indicates low shake threshold if cpx.shake(shake_threshold=10): cpx.pixels[2] = (0, 0, 0) # light between 0 and 2 lights, based on brightness lights = int(cpx.light / 10) if lights == 0: cpx.pixels[3] = (0, 0, 0) cpx.pixels[4] = (0, 0, 0) if lights == 1: cpx.pixels[3] = (0, 0, 0) cpx.pixels[4] = (10, 10, 0) if lights == 2:
total = roll(20) print("Total: %s" % total) display_roll_value(total) if total == 1: pixels.fill(RED) cpx.play_file("laugh.wav") if total == 20: cpx.play_file("fanfare.wav") rainbow_cycle(.001) # init the neopixels pixels = cpx.pixels pixels.fill((0, 0, 0)) pixels.show() while True: if cpx.shake(13): roll_to_hit() if cpx.button_a: print("Button A, OK!") rainbow_cycle(.00001) roll_to_hit() if cpx.button_b: print("Button B Yo!") init_pixels
# wrap around at 0 to original maximum value if cpx.button_b: if dice_brightness == 0: dice_brightness = DICE_MAXBRIGHTNESS else: dice_brightness -= 1 cpx.pixels.brightness = 1 / (2**(DICE_MAXBRIGHTNESS - dice_brightness)) ##dice_color = adjust_color(dice_color, ## 1 / (2**(DICE_MAXBRIGHTNESS - dice_brightness))) ##show_die(cpx.pixels, roll_number, dice_color) # wait for use to take finger off button while cpx.button_b: pass # Check for rolling if cpx.shake() or cpx.tapped: roll_start_time = time.monotonic() new_roll = True rolling = True # Rolling momentum # Keep rolling for a period of time even after shaking stops if new_roll: if time.monotonic() - roll_start_time > 1: rolling = False # Display status on NeoPixels if rolling: # Compute a random number from 1 to 6 roll_number = random.randrange(1, 6 + 1) # Make some noise and show the dice roll number
# This program uses cpx.shake() to detect shaking. # We're going to import these two modules in nearly every program # Sleep allows us to, well, sleep. The CPX will run a while loop WAY too fast without a pause of some sort. # And when we import cpx, we get all kinds of cool functionality from time import sleep from adafruit_circuitplayground.express import cpx phase = 1 # Hey, an exaple of some semi-forever loops. These while loops run until the phase changes while phase == 1: print("Phase 1") cpx.pixels.fill((10, 0, 0)) if cpx.shake(): phase = 2 sleep(.1) while phase == 2: print("Phase 2") cpx.pixels.fill((0, 10, 0)) if cpx.shake(): phase = 3 sleep(.1) print("done")
def led_flash(duration): cpx.pixels.fill((255, 255, 255)) cpx.pixels.show() time.sleep(duration) cpx.pixels.fill((0, 0, 0)) cpx.pixels.show() time.sleep(duration) # make a counter variable counter = 0 while True: # Listen for shakes if cpx.shake(shake_threshold=15 ): # adjust sensitivity - low number is more sensitive print("Shake detected!") # Let us know there was a shake counter = counter + 1 # Start a counter if counter == 2: # On second shake play_wav("awe-a") # play audio for _ in range(3): # loop x times party_flash(0.4) # neopixel flash elif counter == 3: # On third shake play_wav("awe-b") for _ in range(3): # loop x times party_flash(0.4) # neopixel flash elif counter == 4: # On fourth shake play_wav("awe-c") for _ in range(3): # loop x times party_flash(0.4) # neopixel flash elif counter == 5: # on fifth shake
pass selectItem(items[0]) time.sleep(0.1) activationTimer = 0 if cpx.touch_A2: while cpx.touch_A2: pass selectItem(items[1]) time.sleep(0.1) activationTimer = 0 if cpx.touch_A3: while cpx.touch_A3: pass selectItem(items[2]) time.sleep(0.1) activationTimer = 0 if cpx.touch_A4: while cpx.touch_A4: pass selectItem(items[3]) time.sleep(0.1) activationTimer = 0 if mode == Mode.FIXED: activationTimer = activationTimer + 1 if activationTimer > blinkFrequence * 10: inactiveCircuit() while not (cpx.shake() or cpx.touch_A1 or cpx.touch_A2 or cpx.touch_A3 or cpx.touch_A4 or cpx.touch_A5): pass activationTimer=0
from adafruit_circuitplayground.express import cpx import time while True: print('shake detected:', cpx.shake()) time.sleep(0.1)
# pixels.fill((0,0,0)) # pixels.show() ###another interesting feature if cpx.switch: cpx.pixels.brightness = .2 cpx.pixels[0] = (220, 0, 255) print("living the dream") time.sleep(.1) cpx.pixels[0] = (0, 0, 0) if cpx.tapped: print("Single tap detected!") if cpx.shake(10): print("Shake detected!") time.sleep(.001) if cpx.shake(): print("Shakey Shake detected!") time.sleep(.001) #while True: if touch1.value: print("A1 touched!") if touch2.value: print("A2 touched!") if touch3.value: print("A3 touched!") if touch4.value: print("A4 touched!")
from adafruit_circuitplayground.express import cpx import time while True: if cpx.shake(20): cpx.play_tone(900, 0.2) time.sleep(0.1)