diff --git a/Teensy/teensy_process_sound/Animations.h b/Teensy/teensy_process_sound/Animations.h index d0efc85..334f711 100644 --- a/Teensy/teensy_process_sound/Animations.h +++ b/Teensy/teensy_process_sound/Animations.h @@ -1,7 +1,12 @@ #ifndef ANIMATIONS_H #define ANIMATIONS_H +#define DEFAULT_ANIMATION_SPEED 12000 +#define MIN_SPEED_OFFSET 0 +#define MAX_SPEED_OFFSET 30 + uint8_t hueShift = 0; +static int8_t animationSpeed = 0; uint8_t getHue(uint8_t hue) { EVERY_N_MILLIS(300) { @@ -17,11 +22,14 @@ void shiftArrayRight(CRGB leds[], int moveBy, int startIndex) { } } +elapsedMicros elapsed; + void moveStrips(CRGB leds[], int startIndex) { - EVERY_N_MILLIS(MOVE_PIXEL_EVERY_X_MS) { + if (elapsed >= DEFAULT_ANIMATION_SPEED + animationSpeed * 500) { for (int i = 0; i < NUM_STRIPS; i++) { shiftArrayRight(&leds[i*NUM_LEDS_PER_STRIP], 1, startIndex); } + elapsed = 0; } } diff --git a/Teensy/teensy_process_sound/Globals.h b/Teensy/teensy_process_sound/Globals.h index 97a505d..a9dc06d 100644 --- a/Teensy/teensy_process_sound/Globals.h +++ b/Teensy/teensy_process_sound/Globals.h @@ -1,6 +1,12 @@ #ifndef GLOBALS_H #define GLOBALS_H -#define NUM_BINS 140 +#define NUM_BINS 140 + +#define KY040_CLK 10 +#define KY040_DT 0 + +#define CONTROL_PIN 1 +#define BRIGHTNESS_PIN A8 #endif diff --git a/Teensy/teensy_process_sound/teensy_process_sound.ino b/Teensy/teensy_process_sound/teensy_process_sound.ino index 5438314..b82f4ce 100644 --- a/Teensy/teensy_process_sound/teensy_process_sound.ino +++ b/Teensy/teensy_process_sound/teensy_process_sound.ino @@ -1,6 +1,5 @@ #define NUM_STRIPS 8 -#define NUM_LEDS_PER_STRIP 75 -#define MOVE_PIXEL_EVERY_X_MS 5 +#define NUM_LEDS_PER_STRIP 210 #define USE_OCTOWS2811 #include @@ -10,6 +9,7 @@ #include #include #include +#include #include "Globals.h" #include "SoundFilters.h" #include "Animations.h" @@ -29,6 +29,13 @@ float bands[6]; CRGB leds[NUM_STRIPS * NUM_LEDS_PER_STRIP]; +Bounce controlButton = Bounce(CONTROL_PIN, 10); // 10 ms debounce + +void printArrayToSerial(float soundArray[], int arrayLength); +void printLedsToSerial(CRGB leds[]); +void setBrightnessFromPot(); +void setSpeedFromKY040(); +void setupKY040(); void setup() { LEDS.addLeds(leds, NUM_LEDS_PER_STRIP); @@ -50,14 +57,13 @@ void setup() { memset(deltas, 0, sizeof(deltas)); FastLED.setBrightness(200); -} -void printArrayToSerial(float soundArray[], int arrayLength); -void printLedsToSerial(CRGB leds[]); -void setBrightnessFromPot(); + setupKY040(); +} void loop() { setBrightnessFromPot(); + setSpeedFromKY040(); int i; @@ -67,7 +73,6 @@ void loop() { } processSound(soundArray, deltas); createBands(deltas, bands); - bandsAnimation(bands, leds); // printArrayToSerial(soundArray, NUM_BINS); // printArrayToSerial(deltas, NUM_BINS); @@ -80,14 +85,61 @@ void loop() { // Serial.println(FastLED.getFPS()); } + bandsAnimation(bands, leds); FastLED.show(); } +void setupKY040() { + pinMode(KY040_CLK, INPUT); + pinMode(KY040_DT, INPUT); + + pinMode(CONTROL_PIN, INPUT_PULLUP); +} + +void setSpeedFromKY040() { + static uint8_t pinCLKlast = digitalRead(KY040_CLK); + uint8_t currentVal; + EVERY_N_MILLIS(5) { + currentVal = digitalRead(KY040_CLK); + + if (currentVal != pinCLKlast) { + if (digitalRead(KY040_DT) == currentVal) { + animationSpeed += 1; + } else { + animationSpeed -= 1; + } + } + + if (animationSpeed > MAX_SPEED_OFFSET) { + animationSpeed = MAX_SPEED_OFFSET; + } else if (animationSpeed < MIN_SPEED_OFFSET) { + animationSpeed = MIN_SPEED_OFFSET; + } + pinCLKlast = currentVal; + Serial.println(animationSpeed); + } +} + void setBrightnessFromPot() { static float potValue = 0; - potValue = 0.99 * potValue + 0.01 * analogRead(A8); + static bool controlConnected = false; + + potValue = 0.99 * potValue + 0.01 * analogRead(BRIGHTNESS_PIN); int brightnessVal = potValue*255/1023; -// Serial.println(brightnessVal); + if (brightnessVal <= 5) { + brightnessVal = 0; + } + + if (controlButton.update()) { + if (controlButton.fallingEdge()) { + controlConnected = !controlConnected; + } + } + + if (!controlConnected) { + brightnessVal = 255; + } + FastLED.setBrightness(brightnessVal); }