Skip to content

Commit

Permalink
Merge pull request #1 from ngutman/control_speed
Browse files Browse the repository at this point in the history
Control speed and brightness
  • Loading branch information
Nimrod Gutman committed May 31, 2016
2 parents 0e3cbf7 + 2355c2c commit 3eb42a3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
10 changes: 9 additions & 1 deletion Teensy/teensy_process_sound/Animations.h
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
}
}

Expand Down
8 changes: 7 additions & 1 deletion Teensy/teensy_process_sound/Globals.h
Original file line number Diff line number Diff line change
@@ -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
70 changes: 61 additions & 9 deletions Teensy/teensy_process_sound/teensy_process_sound.ino
Original file line number Diff line number Diff line change
@@ -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 <OctoWS2811.h>
Expand All @@ -10,6 +9,7 @@
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>
#include "Globals.h"
#include "SoundFilters.h"
#include "Animations.h"
Expand All @@ -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<OCTOWS2811>(leds, NUM_LEDS_PER_STRIP);
Expand All @@ -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;

Expand All @@ -67,7 +73,6 @@ void loop() {
}
processSound(soundArray, deltas);
createBands(deltas, bands);
bandsAnimation(bands, leds);

// printArrayToSerial(soundArray, NUM_BINS);
// printArrayToSerial(deltas, NUM_BINS);
Expand All @@ -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);
}

Expand Down

0 comments on commit 3eb42a3

Please sign in to comment.