-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_AUX_Reactive_LEDs.ino
More file actions
119 lines (103 loc) · 3.42 KB
/
Arduino_AUX_Reactive_LEDs.ino
File metadata and controls
119 lines (103 loc) · 3.42 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <FastLED.h>
#define IN A7 // input analog pin, the one to which the auc is connected
#define DATA_PIN 2
#define CLOCK_PIN 13
#define NUM_LEDS 30 // number of leds to command on the strip
#define AMPLIFY 1 // the input will be input^AMPLIFY
int THRSH = 15;
int prev_listen = 0;// will hold the result of the previous call to listen()
int new_listen = 0;// will hold the new result of listen()
CRGB leds[NUM_LEDS];
CRGB color_template[] = {0xFF00D0/*purple*/,0x0000FF/*blue*/,0xFF1001/*red - orange*/,0xFFA000/*peach - orange*/,0x10FF00/*warm green*/,0x00FFFF/*cyan*/};
int to_cycle = sizeof(color_template)/sizeof(color_template[0]); // length of array
int col_flag = 0; // will be in range [0, to_cycle - 1]
CRGB cur_color;
void setup() {
FastLED.addLeds<WS2811/*the nodel of led chip*/, DATA_PIN, BRG/*RGB order*/>(leds, NUM_LEDS);
pinMode(IN, INPUT); // set the input pin as input
Serial.begin(9600); // start serial so we can later print values for tests
}
void set_color(int i){// redundant - set leds [0,i-1] to cur_color
for(int j = 0; j < i + 1; j++){
leds[j] = cur_color;
FastLED.show();
}
}
void make_dance_single(){ // redundant - does make dance but without wave
if(col_flag == 0){
cur_color = CRGB::Red;
}
else if(col_flag == 1){
cur_color = CRGB::Blue;
}
else{
cur_color = CRGB::Green;
}
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = cur_color;
FastLED.show();
if(i > 0);
}
}
void make_dance(){ // when called, it will make a wave of color while listening to the input between eaech led and then, only if it wasn't triggered will turn off the leds
cur_color = color_template[col_flag];
for(int i = 0; i < NUM_LEDS - 1; i++){
leds[i] = cur_color;
leds[i+1] = CRGB::Black;
FastLED.show();
new_listen = listen();
if(new_listen - prev_listen > THRSH && i > 20){ // if it is mare than 20 leds into the wave and the new listen is over the threshold it will send a wave of a different color over the existing wave
prev_listen = new_listen;
col_flag = (col_flag + 1) % to_cycle; // this is how we make sure we don't go out of bounds
make_dance();
return;
}
prev_listen = new_listen;
}
for(int i = 0; i < NUM_LEDS; i++){ // turn off the leds with a wave
leds[i] = CRGB::Black;
//FastLED.show();
//delay(3);
}
FastLED.show(); // new approarch
col_flag = (col_flag + 1) % to_cycle; // switch color for next time
}
int listen(){ // will return the mean of for_how_long reads of the aux input tp the power of AMPLIFY
int vals_mean = 0;
int for_how_long = 80;
for(int i = 0; i < for_how_long; i++){
vals_mean = vals_mean + (analogRead(IN)/ for_how_long);// / for_how_long);
}
Serial.println(pow(vals_mean,AMPLIFY));
return pow(vals_mean,AMPLIFY);
}
void turn_off(){
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGB::Black;
FastLED.show();
}
}
void shiftColorOfAll(){
col_flag = (col_flag + 1) % to_cycle;
cur_color = color_template[col_flag];
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = cur_color;
}
FastLED.show();
}
void loop() {
new_listen = listen();
if(new_listen - prev_listen > THRSH){
prev_listen = new_listen;
make_dance();
}
else if(leds[0] != CRGB( 0, 0, 0)){// if the leds didnt turn of last run
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGB::Black;
FastLED.show();
}
}
else{
prev_listen = new_listen;
}
}