Skip to content

Commit 3eb42a3

Browse files
author
Nimrod Gutman
committed
Merge pull request #1 from ngutman/control_speed
Control speed and brightness
2 parents 0e3cbf7 + 2355c2c commit 3eb42a3

3 files changed

Lines changed: 77 additions & 11 deletions

File tree

Teensy/teensy_process_sound/Animations.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#ifndef ANIMATIONS_H
22
#define ANIMATIONS_H
33

4+
#define DEFAULT_ANIMATION_SPEED 12000
5+
#define MIN_SPEED_OFFSET 0
6+
#define MAX_SPEED_OFFSET 30
7+
48
uint8_t hueShift = 0;
9+
static int8_t animationSpeed = 0;
510

611
uint8_t getHue(uint8_t hue) {
712
EVERY_N_MILLIS(300) {
@@ -17,11 +22,14 @@ void shiftArrayRight(CRGB leds[], int moveBy, int startIndex) {
1722
}
1823
}
1924

25+
elapsedMicros elapsed;
26+
2027
void moveStrips(CRGB leds[], int startIndex) {
21-
EVERY_N_MILLIS(MOVE_PIXEL_EVERY_X_MS) {
28+
if (elapsed >= DEFAULT_ANIMATION_SPEED + animationSpeed * 500) {
2229
for (int i = 0; i < NUM_STRIPS; i++) {
2330
shiftArrayRight(&leds[i*NUM_LEDS_PER_STRIP], 1, startIndex);
2431
}
32+
elapsed = 0;
2533
}
2634
}
2735

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#ifndef GLOBALS_H
22
#define GLOBALS_H
33

4-
#define NUM_BINS 140
4+
#define NUM_BINS 140
5+
6+
#define KY040_CLK 10
7+
#define KY040_DT 0
8+
9+
#define CONTROL_PIN 1
10+
#define BRIGHTNESS_PIN A8
511

612
#endif

Teensy/teensy_process_sound/teensy_process_sound.ino

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#define NUM_STRIPS 8
2-
#define NUM_LEDS_PER_STRIP 75
3-
#define MOVE_PIXEL_EVERY_X_MS 5
2+
#define NUM_LEDS_PER_STRIP 210
43

54
#define USE_OCTOWS2811
65
#include <OctoWS2811.h>
@@ -10,6 +9,7 @@
109
#include <SPI.h>
1110
#include <SD.h>
1211
#include <SerialFlash.h>
12+
#include <Bounce.h>
1313
#include "Globals.h"
1414
#include "SoundFilters.h"
1515
#include "Animations.h"
@@ -29,6 +29,13 @@ float bands[6];
2929

3030
CRGB leds[NUM_STRIPS * NUM_LEDS_PER_STRIP];
3131

32+
Bounce controlButton = Bounce(CONTROL_PIN, 10); // 10 ms debounce
33+
34+
void printArrayToSerial(float soundArray[], int arrayLength);
35+
void printLedsToSerial(CRGB leds[]);
36+
void setBrightnessFromPot();
37+
void setSpeedFromKY040();
38+
void setupKY040();
3239

3340
void setup() {
3441
LEDS.addLeds<OCTOWS2811>(leds, NUM_LEDS_PER_STRIP);
@@ -50,14 +57,13 @@ void setup() {
5057
memset(deltas, 0, sizeof(deltas));
5158

5259
FastLED.setBrightness(200);
53-
}
5460

55-
void printArrayToSerial(float soundArray[], int arrayLength);
56-
void printLedsToSerial(CRGB leds[]);
57-
void setBrightnessFromPot();
61+
setupKY040();
62+
}
5863

5964
void loop() {
6065
setBrightnessFromPot();
66+
setSpeedFromKY040();
6167

6268
int i;
6369

@@ -67,7 +73,6 @@ void loop() {
6773
}
6874
processSound(soundArray, deltas);
6975
createBands(deltas, bands);
70-
bandsAnimation(bands, leds);
7176

7277
// printArrayToSerial(soundArray, NUM_BINS);
7378
// printArrayToSerial(deltas, NUM_BINS);
@@ -80,14 +85,61 @@ void loop() {
8085
// Serial.println(FastLED.getFPS());
8186
}
8287

88+
bandsAnimation(bands, leds);
8389
FastLED.show();
8490
}
8591

92+
void setupKY040() {
93+
pinMode(KY040_CLK, INPUT);
94+
pinMode(KY040_DT, INPUT);
95+
96+
pinMode(CONTROL_PIN, INPUT_PULLUP);
97+
}
98+
99+
void setSpeedFromKY040() {
100+
static uint8_t pinCLKlast = digitalRead(KY040_CLK);
101+
uint8_t currentVal;
102+
EVERY_N_MILLIS(5) {
103+
currentVal = digitalRead(KY040_CLK);
104+
105+
if (currentVal != pinCLKlast) {
106+
if (digitalRead(KY040_DT) == currentVal) {
107+
animationSpeed += 1;
108+
} else {
109+
animationSpeed -= 1;
110+
}
111+
}
112+
113+
if (animationSpeed > MAX_SPEED_OFFSET) {
114+
animationSpeed = MAX_SPEED_OFFSET;
115+
} else if (animationSpeed < MIN_SPEED_OFFSET) {
116+
animationSpeed = MIN_SPEED_OFFSET;
117+
}
118+
pinCLKlast = currentVal;
119+
Serial.println(animationSpeed);
120+
}
121+
}
122+
86123
void setBrightnessFromPot() {
87124
static float potValue = 0;
88-
potValue = 0.99 * potValue + 0.01 * analogRead(A8);
125+
static bool controlConnected = false;
126+
127+
potValue = 0.99 * potValue + 0.01 * analogRead(BRIGHTNESS_PIN);
89128
int brightnessVal = potValue*255/1023;
90-
// Serial.println(brightnessVal);
129+
if (brightnessVal <= 5) {
130+
brightnessVal = 0;
131+
}
132+
133+
if (controlButton.update()) {
134+
if (controlButton.fallingEdge()) {
135+
controlConnected = !controlConnected;
136+
}
137+
}
138+
139+
if (!controlConnected) {
140+
brightnessVal = 255;
141+
}
142+
91143
FastLED.setBrightness(brightnessVal);
92144
}
93145

0 commit comments

Comments
 (0)