-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_led.ino
124 lines (116 loc) · 3.39 KB
/
controller_led.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
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
120
121
122
123
124
// Define the const variables and the pins output of colors
const int red_control = 2;
const int blue_control = 4;
const int green_control = 3;
const int thresh = 300;
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(red_control, OUTPUT);
pinMode(blue_control, OUTPUT);
pinMode(green_control, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
//Variables to store the 0-255 color rgb and read
unsigned int sample;
int red_color = 0;
int blue_color = 0;
int green_color = 0;
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0; // Variable to take the Max tension signal in the sample window
unsigned int signalMin = 1024; // Variable to take the Min tension signal in the sample window
// collect data for 50 mS
// 506 MAX
while (millis() - startMillis < sampleWindow){
sample = analogRead(A0);
if (sample < 1024) { // toss out spurious readings
if (sample > signalMax){
signalMax = sample; // save just the max levels
}
else if (sample < signalMin){
signalMin = sample; // save just the min levels
}
}
}
//Calculate the Peak-to-Peak tension
peakToPeak = signalMax - signalMin;
//Convert the peak to peak into colors
pitchConv(peakToPeak,&red_color,&blue_color,&green_color);
//Output the colors values
analogWrite(red_control, red_color);
analogWrite(blue_control, blue_color);
analogWrite(green_control, green_color);
delay(30);
}
/**
* Converts the analog brightness reading into a percentage
* 100% brightness is 614.. about 3 volts on circuit
* The resulting percentage can simply be multiplied on the rgb values when setting our colors,
* for example black is (0,0,0) so when volume is off we get 0v and all colors are black (leds are off)
*/
double convBrightness(int b) {
double c = b /(double) thresh;
Serial.println(b);
Serial.println(c);
if( c < 0.2 ) {
c = 0;
}
else if(c > 1) {
c = 1.00;
}
return c;
}
/*
* This function sets the values into pointer of colors
*/
void setColor(int a,int ba,int c,int *r,int *b, int *g){
*(r) = a;
*(b) = ba;
*(g) = c;
}
/*
* Converts the tension into colors, takes the peak-to-peak and
* the pointers of colors variables
*/
void pitchConv(int p,int *r,int *b,int *g){
double bright = convBrightness(p);
// Map the colors according the peak-to-peak value
if(p <= 70) {
int cor_b = p*255.0/70.0;
setColor(255, 0, cor_b,r,b,g);
}
else if(p <= 100) {
int cor_r = p*255.0/100.0;
setColor(0, cor_r, 255,r,b,g);
}
else if(p <= 150) {
int cor_g = p*255.0/150.0;
setColor(255, cor_g, 0,r,b,g);
}
else if(p <= 200) {
int cor_r = p*255.0/200.0;
setColor(cor_r, 255, 100,r,b,g);
}
else if(p <= 230) {
int cor_b = p*(255.0/230.0);
setColor(255, 100, cor_b,r,b,g);
}
else if(p <= 270) {
int rb = 255-((p-200)*2);
setColor(rb, 255, rb,r,b,g);
}
else if(p <= 300 ) {
setColor(0, 255-(p-210), (p-210),r,b,g);
}
else {
setColor(255, 0, 0,r,b,g);
}
//Multiple the colors by the brightness value
*(r) = *(r)*bright;
*(b) = *(b)*bright;
*(g) = *(g)*bright;
}