diff --git a/Head_Tilt_Ears/code.py b/Head_Tilt_Ears/code.py index 6bcdcab36..ffc771353 100644 --- a/Head_Tilt_Ears/code.py +++ b/Head_Tilt_Ears/code.py @@ -16,15 +16,20 @@ import time import busio import board import adafruit_lis3dh -import simpleio +import pulseio +from adafruit_motor import servo + # Setup accelerometer i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) sensor = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19) # Setup servos -left_ear = simpleio.Servo(board.A1) -right_ear = simpleio.Servo(board.A2) +left_pwm = pulseio.PWMOut(board.A1, frequency=50) +right_pwm = pulseio.PWMOut(board.A2, frequency=50) + +left_ear = servo.Servo(left_pwm) +right_ear = servo.Servo(right_pwm) #initialize things left_ear.angle = 0 diff --git a/NeoPixel_Cyber_Falls_Wig/NeoPixel_Cyber_Falls_Wig.ino b/NeoPixel_Cyber_Falls_Wig/NeoPixel_Cyber_Falls_Wig.ino new file mode 100644 index 000000000..13d96b3d8 --- /dev/null +++ b/NeoPixel_Cyber_Falls_Wig/NeoPixel_Cyber_Falls_Wig.ino @@ -0,0 +1,148 @@ +// 'Cyber falls' sketch, adapted from code for Firewalker sneakers. +// Creates a fiery rain-like effect on multiple NeoPixel strips. +// Requires Adafruit Trinket and NeoPixel strips. Strip length is +// inherently limited by Trinket RAM and processing power; this is +// written for five 15-pixel strands, which are paired up per pin +// for ten 15-pixel strips total. + +#include +#include + +uint8_t gamma[] PROGMEM = { // Gamma correction table for LED brightness + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, + 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, + 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, + 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, + 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, + 25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36, + 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50, + 51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68, + 69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89, + 90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114, + 115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142, + 144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175, + 177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213, + 215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 }; + +#define STRIPLEN 15 // Length of LED strips +#define MAXDROPS 5 // Max concurrent 'raindrops' +#define N_STRIPS 5 // Connect strips to pins 0 to (N_STRIPS-1) + +Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIPLEN, 0); + +// Everything's declared volatile because state changes inside +// an interrupt routine. +volatile struct { + uint8_t strip; + int16_t pos; + uint8_t speed; + uint16_t brightness; +} drop[MAXDROPS]; +volatile uint8_t + nDrops = 0, // Number of 'active' raindrops + prevStrip = 255; // Last selected strip +volatile uint16_t + countdown = 0; // Time to next raindrop + +void setup() { + + // Set up Timer/Counter1 for ~30 Hz interrupt +#if F_CPU == 16000000L + clock_prescale_set(clock_div_1); + TCCR1 = _BV(PWM1A) |_BV(CS13) | _BV(CS12); // 1:2048 prescale +#else + TCCR1 = _BV(PWM1A) |_BV(CS13) | _BV(CS11) | _BV(CS10); // 1:1024 prescale +#endif + + // Turn strips off ASAP (must follow clock_prescale_set) + strip.begin(); + for(uint8_t s=0; s= 32)) continue; // Out of range + if(mx1 > 24) { // Rising edge of wave; ramp up fast (2 px) + m = ((long)drop[d].brightness * (long)(32 - mx1)) >> 4; + } else { // Falling edge of wave; fade slow (6 px) + m = ((long)drop[d].brightness * (long)mx1) / 24; + } + mag[p] += m; // Accumulate result in magnitude buffer + } + } + } + + // Remap magnitude table to RGB for strand + for(p=0; p= (STRIPLEN * 4)) { // Off end? + // Remove drop from list (move last one to this place) + memcpy((void *)&drop[d], (void *)&drop[nDrops-1], sizeof(drop[0])); + nDrops--; + } + } +} \ No newline at end of file diff --git a/NeoPixel_Cyber_Falls_Wig/NeoPixel_Cyber_Falls_Wig.py b/NeoPixel_Cyber_Falls_Wig/NeoPixel_Cyber_Falls_Wig.py new file mode 100644 index 000000000..6e754a538 --- /dev/null +++ b/NeoPixel_Cyber_Falls_Wig/NeoPixel_Cyber_Falls_Wig.py @@ -0,0 +1,57 @@ +# 'Cyber falls' sketch +# Creates a fiery rain-like effect on multiple NeoPixel strips. +# Requires Adafruit Trinket and NeoPixel strips. Strip length is +# inherently limited by Trinket RAM and processing power; this is +# written for five 15-pixel strands, which are paired up per pin +# for ten 15-pixel strips total. + +import time +import board +import neopixel +import adafruit_fancyled.adafruit_fancyled as fancy + +num_leds = 15 # number of LEDs per strip +saturation = 255 # 0-255, 0 is pure white, 255 is fully saturated color +blend = True # color blending between palette indices +brightness = 0.5 # half brightness the range is 0.0 - 1.0 +concurrent = 3 # number of LEDs on at a time +on_time = 0.04 # 0.04 seconds == 40 milliseconds + +# NeoPixel objects using all five Trinket M0 GPIO pins 0-4 +drop0 = neopixel.NeoPixel(board.D0, num_leds) +drop1 = neopixel.NeoPixel(board.D1, num_leds) +drop2 = neopixel.NeoPixel(board.D2, num_leds) +drop3 = neopixel.NeoPixel(board.D3, num_leds) +drop4 = neopixel.NeoPixel(board.D4, num_leds) + +# list of neopixel strips +drop_list = [drop0, drop1, drop2, drop3, drop4] + +def led_drops(strip): + + # FancyLED allows for mixing colors with palettes + palette = [fancy.CRGB(200, 255, 200), # lighter (more white) green + fancy.CRGB(0, 255, 0)] # full green + + for i in range(num_leds): + # FancyLED can handle the gamma adjustment, brightness and RGB settings + color = fancy.palette_lookup(palette, i / num_leds) + color = fancy.gamma_adjust(color, brightness=brightness) + strip[i] = color.pack() + + # turn off the LEDs as we go for raindrop effect + if i >= concurrent: + strip[i - concurrent] = (0,0,0) + + if i >= num_leds - 1: + for j in range(concurrent,-1,-1): + strip[i-j] = (0,0,0) + time.sleep(on_time) + + time.sleep(on_time) + +while True: + + # loop through each neopixel strip in our list + for drops in drop_list: + led_drops(drops) diff --git a/NeoPixel_Cyber_Falls_Wig/README.md b/NeoPixel_Cyber_Falls_Wig/README.md new file mode 100644 index 000000000..517b0d5ee --- /dev/null +++ b/NeoPixel_Cyber_Falls_Wig/README.md @@ -0,0 +1,4 @@ +# NeoPixel Cyber Falls Wig + +Code to accompany this tutorial: +https://learn.adafruit.com/neopixel-cyber-falls diff --git a/Sound_Reactive_Ears/code.py b/Sound_Reactive_Ears/code.py index 61de3e861..16265117e 100644 --- a/Sound_Reactive_Ears/code.py +++ b/Sound_Reactive_Ears/code.py @@ -17,7 +17,8 @@ import math import array import board import audiobusio -import simpleio +import pulseio +from adafruit_motor import servo from adafruit_circuitplayground.express import cpx # Exponential scaling factor. @@ -30,9 +31,11 @@ NUM_SAMPLES = 90 # the trigger threshhold THRESHOLD = 6 +left_pwm = pulseio.PWMOut(board.A1, frequency=50) +right_pwm = pulseio.PWMOut(board.A2, frequency=50) -left_ear = simpleio.Servo(board.A1) -right_ear = simpleio.Servo(board.A2) +left_ear = servo.Servo(left_pwm) +right_ear = servo.Servo(right_pwm) cpx.pixels.fill((0, 0, 0)) left_ear.angle = 0 diff --git a/Trinket_Gemma_Servo_Control/Trinket_Gemma_Servo_Control.py b/Trinket_Gemma_Servo_Control/Trinket_Gemma_Servo_Control.py index 522c095d6..1284e0337 100644 --- a/Trinket_Gemma_Servo_Control/Trinket_Gemma_Servo_Control.py +++ b/Trinket_Gemma_Servo_Control/Trinket_Gemma_Servo_Control.py @@ -1,15 +1,19 @@ +# Trinket Gemma Servo Control +# for Adafruit M0 boards + import board -import simpleio +import pulseio +from adafruit_motor import servo from analogio import AnalogIn # servo pin for the M0 boards: -servo = simpleio.Servo(board.A2) +pwm = pulseio.PWMOut(board.A2, duty_cycle=2 ** 15, frequency=50) +my_servo = servo.Servo(pwm) angle = 0 # potentiometer trimpot = AnalogIn(board.A1) # pot pin for servo control - def remap_range(value, left_min, left_max, right_min, right_max): # this remaps a value from original (left) range to new (right) range # Figure out how 'wide' each range is @@ -25,5 +29,4 @@ def remap_range(value, left_min, left_max, right_min, right_max): while True: angle = remap_range(trimpot.value, 0, 65535, 0, 180) - servo.angle = angle - # time.sleep(0.05) + my_servo.angle = angle