Skip to content

Presentation code #1

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 1 commit into from
May 3, 2018
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions cpx_accel_neopixels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from adafruit_circuitplayground.express import cpx
import time

# Main loop gets x, y and z axis acceleration, prints the values, and turns on
# red, green and blue, at levels related to the x, y and z values.
while True:
if cpx.switch:
print("Slide switch off!")
cpx.pixels.fill((0, 0, 0))
continue
else:
R = 0
G = 0
B = 0
x, y, z = cpx.acceleration
print(x, y, z)
if x:
R = R + abs(int(x))
if y:
G = G + abs(int(y))
if z:
B = B + abs(int(z))
cpx.pixels.fill((R, G, B))
time.sleep(0.2)
7 changes: 7 additions & 0 deletions cpx_accelerometer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import time
from adafruit_circuitplayground.express import cpx

while True:
x, y, z = cpx.acceleration
print(x, y, z)
time.sleep(0.5)
8 changes: 8 additions & 0 deletions cpx_blinky.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import time
from adafruit_circuitplayground.express import cpx

while True:
cpx.red_led = True
time.sleep(0.5)
cpx.red_led = False
time.sleep(0.5)
5 changes: 5 additions & 0 deletions cpx_button_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from adafruit_circuitplayground.express import cpx

while True:
if cpx.button_a:
cpx.red_led = True
7 changes: 7 additions & 0 deletions cpx_button_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from adafruit_circuitplayground.express import cpx

while True:
if cpx.button_b:
cpx.red_led = True
else:
cpx.red_led = False
6 changes: 6 additions & 0 deletions cpx_light_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import time
from adafruit_circuitplayground.express import cpx

while True:
print("Lux:", cpx.light)
time.sleep(1)
6 changes: 6 additions & 0 deletions cpx_neopixel_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from adafruit_circuitplayground.express import cpx

cpx.pixels.brightness = 0.3

while True:
cpx.pixels[1] = (0, 255, 0)
4 changes: 4 additions & 0 deletions cpx_neopixels_fill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from adafruit_circuitplayground.express import cpx

while True:
cpx.pixels.fill((50, 0, 0))
4 changes: 4 additions & 0 deletions cpx_play_tone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from adafruit_circuitplayground.express import cpx

while True:
cpx.play_tone(262, 1)
5 changes: 5 additions & 0 deletions cpx_shake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from adafruit_circuitplayground.express import cpx

while True:
if cpx.shake():
print("Shake detected!")
69 changes: 69 additions & 0 deletions cpx_sound_meter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import array
import audiobusio
import board
import math
import neopixel

CURVE = 2
SCALE_EXPONENT = math.pow(10, CURVE * -0.1)

PEAK_COLOR = (80, 0, 255)
NUM_PIXELS = 10
NUM_SAMPLES = 160


def constrain(value, floor, ceiling):
return max(floor, min(value, ceiling))


def log_scale(input_value, input_min, input_max, output_min, output_max):
normalized_input_value = (input_value - input_min) / (input_max - input_min)
return output_min + math.pow(normalized_input_value, SCALE_EXPONENT) * (output_max - output_min)


def normalized_rms(values):
minbuf = int(mean(values))
return math.sqrt(sum(float(sample - minbuf) * (sample - minbuf) for sample in values) / len(values))


def mean(values):
return sum(values) / len(values)


def volume_color(i):
return i * (255 // NUM_PIXELS), 50, 0


pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1, auto_write=False)
pixels.fill(0)
pixels.show()

mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, frequency=16000, bit_depth=16)
samples = array.array('H', [0] * NUM_SAMPLES)
mic.record(samples, len(samples))
input_floor = normalized_rms(samples) + 10

# Lower number means more sensitive - more LEDs will light up with less sound.
sensitivity = 500
input_ceiling = input_floor + sensitivity

peak = 0
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
print(magnitude)

c = log_scale(constrain(magnitude, input_floor, input_ceiling),
input_floor, input_ceiling, 0, NUM_PIXELS)

pixels.fill(0)
for i in range(NUM_PIXELS):
if i < c:
pixels[i] = volume_color(i)
if c >= peak:
peak = min(c, NUM_PIXELS - 1)
elif peak > 0:
peak = peak - 1
if peak > 0:
pixels[int(peak)] = PEAK_COLOR
pixels.show()
9 changes: 9 additions & 0 deletions cpx_start_stop_tone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from adafruit_circuitplayground.express import cpx

while True:
if cpx.button_a:
cpx.start_tone(262)
elif cpx.button_b:
cpx.start_tone(294)
else:
cpx.stop_tone()
6 changes: 6 additions & 0 deletions cpx_temperture_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import time
from adafruit_circuitplayground.express import cpx

while True:
print("Temperature C:", cpx.temperature)
time.sleep(1)
7 changes: 7 additions & 0 deletions cpx_touch_a1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import time
from adafruit_circuitplayground.express import cpx

while True:
if cpx.touch_A1:
print("Touched A1!")
time.sleep(0.1)
19 changes: 19 additions & 0 deletions cpx_touch_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import time
from adafruit_circuitplayground.express import cpx

while True:
if cpx.touch_A1:
print("Touched A1!")
if cpx.touch_A2:
print("Touched A2!")
if cpx.touch_A3:
print("Touched A3!")
if cpx.touch_A4:
print("Touched A4!")
if cpx.touch_A5:
print("Touched A5!")
if cpx.touch_A6:
print("Touched A6!")
if cpx.touch_A7:
print("Touched A7!")
time.sleep(0.1)
28 changes: 28 additions & 0 deletions cpx_touch_fill_rainbow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import time
from adafruit_circuitplayground.express import cpx

cpx.pixels.brightness = 0.3

while True:
if cpx.touch_A1:
print("Touched A1!")
cpx.pixels.fill((255, 0, 0))
if cpx.touch_A2:
print("Touched A2!")
cpx.pixels.fill((210, 45, 0))
if cpx.touch_A3:
print("Touched A3!")
cpx.pixels.fill((155, 100, 0))
if cpx.touch_A4:
print("Touched A4!")
cpx.pixels.fill((0, 255, 0))
if cpx.touch_A5:
print("Touched A5!")
cpx.pixels.fill((0, 135, 125))
if cpx.touch_A6:
print("Touched A6!")
cpx.pixels.fill((0, 0, 255))
if cpx.touch_A7:
print("Touched A7!")
cpx.pixels.fill((100, 0, 155))
time.sleep(0.1)
28 changes: 28 additions & 0 deletions cpx_touch_pixel_rainbow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import time
from adafruit_circuitplayground.express import cpx

cpx.pixels.brightness = 0.3

while True:
if cpx.touch_A1:
print("Touched A1!")
cpx.pixels[6] = (255, 0, 0)
if cpx.touch_A2:
print("Touched A2!")
cpx.pixels[8] = (210, 45, 0)
if cpx.touch_A3:
print("Touched A3!")
cpx.pixels[9] = (155, 100, 0)
if cpx.touch_A4:
print("Touched A4!")
cpx.pixels[0] = (0, 255, 0)
if cpx.touch_A5:
print("Touched A5!")
cpx.pixels[1] = (0, 135, 125)
if cpx.touch_A6:
print("Touched A6!")
cpx.pixels[3] = (0, 0, 255)
if cpx.touch_A7:
print("Touched A7!")
cpx.pixels[4] = (100, 0, 155)
time.sleep(0.1)