-
Notifications
You must be signed in to change notification settings - Fork 30
/
vu8.ino
86 lines (75 loc) · 2.34 KB
/
vu8.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* VU: Hue cycling, three bars (shatter, mono)
*/
void vu8() {
const uint8_t DRAW_MAX = 30;
const uint8_t SEGMENTS = 3;
static int origin = 0;
static uint8_t scroll_color = 0;
static int last_intensity = 0;
static int intensity_max = 0;
static int origin_at_flip = 0;
static CHSV draw[DRAW_MAX];
static bool growing = false;
static bool fall_from_left = true;
int intensity = auxReading(0);
//// Update Origin ////
// detect peak change and save origin at curve vertex
if (growing && intensity < last_intensity) {
growing = false;
intensity_max = last_intensity;
fall_from_left = !fall_from_left;
origin_at_flip = origin;
} else if (intensity > last_intensity) {
growing = true;
origin_at_flip = origin;
}
last_intensity = intensity;
// adjust origin if falling
if (!growing) {
if (fall_from_left) {
origin = origin_at_flip + ((intensity_max - intensity) / 2);
} else {
origin = origin_at_flip - ((intensity_max - intensity) / 2);
}
// correct for origin out of bounds
if (origin < 0) {
origin = DRAW_MAX - abs(origin);
} else if (origin > DRAW_MAX - 1) {
origin = origin - DRAW_MAX - 1;
}
}
//// Assign draw values ///
// draw amplitue as 1/2 intensity both directions from origin
int min_lit = origin - (intensity / 2);
int max_lit = origin + (intensity / 2);
if (min_lit < 0) {
min_lit = min_lit + DRAW_MAX;
}
if (max_lit >= DRAW_MAX) {
max_lit = max_lit - DRAW_MAX;
}
for (int i=0; i < DRAW_MAX; i++) {
// if i is within origin +/- 1/2 intensity
if (
(min_lit < max_lit && min_lit < i && i < max_lit) // range is within bounds and i is within range
|| (min_lit > max_lit && (i > min_lit || i < max_lit)) // range wraps out of bounds and i is within that wrap
) {
draw[i] = CHSV(scroll_color,255,255);
} else {
draw[i] = CHSV(scroll_color,0,0);
}
}
//// Write Segmented ////
int seg_len = N_PIXELS / SEGMENTS;
for (int s = 0; s < SEGMENTS; s++) {
for (int i = 0; i < seg_len; i++) {
ledsLeft[i + (s*seg_len)] = draw[map(i, 0, seg_len, 0, DRAW_MAX)];
}
}
FastLED.show();
averageReadings(0);
EVERY_N_MILLISECONDS(20) {
scroll_color = ++scroll_color % 255;
}
}