-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundReact.ino
161 lines (128 loc) · 2.51 KB
/
SoundReact.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Sound Reactive RGB LED Strip Sketch.
// SoundReact By Anton Grouchtchak(Teraskull).
// https://github.com/Teraskull/SoundReact
#define red 11
#define green 10
#define blue 9
#define microphone 7
#define L 13
int change = 0;
int minNum = 4000; // Generate random time to change colors, from 4000 to 15000
int maxNum = 15000;
int x = 0;
int y = 0; // TEMP values, so that the same color doesn't repeat 3 times in a row
int z = 0;
int initFirstRGB = 500;
int initRGB = 150; // Initialize RGB Strip with 3 red and 1 green flash
int initLastRGB = 1000;
bool soundstate; // Detect sound from microphone
void setup() {
//Serial.begin(9600);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(L, OUTPUT);
pinMode(microphone, INPUT);
digitalWrite(L, LOW); // Turn off built-in LED
initStrip();
}
void loop() {
chooseRandomColor();
changeRandomTime();
//Serial.println(change);
for (int i = 0; i < change; i++) {
detectSound();
if (soundstate == 1) {
colorShow();
}
else {
colorOff();
}
}
}
void initStrip() {
colorRed();
delay(initFirstRGB);
colorOff();
delay(initRGB);
colorRed();
delay(initRGB);
colorOff();
delay(initRGB);
colorRed();
delay(initRGB);
colorOff();
delay(initRGB);
colorGreen();
delay(initFirstRGB);
colorOff();
delay(initLastRGB);
}
void colorRed() {
analogWrite(red, 0);
analogWrite(green, 255);
analogWrite(blue, 255);
}
void colorGreen() {
analogWrite(red, 255);
analogWrite(green, 0);
analogWrite(blue, 255);
}
void colorBlue() {
analogWrite(red, 255);
analogWrite(green, 255);
analogWrite(blue, 0);
}
void colorYellow() {
analogWrite(red, 0);
analogWrite(green, 0);
analogWrite(blue, 255);
}
void colorCyan() {
analogWrite(red, 255);
analogWrite(green, 0);
analogWrite(blue, 0);
}
void colorPurple() {
analogWrite(red, 0);
analogWrite(green, 255);
analogWrite(blue, 0);
}
void colorOff() {
analogWrite(red, 255);
analogWrite(green, 255);
analogWrite(blue, 255);
}
int chooseRandomColor() {
while(y == x || z == x){
x = random(1, 7);
}
z = y;
y = x;
}
int changeRandomTime() {
change = random(minNum, maxNum);
}
bool detectSound() {
soundstate = digitalRead(microphone);
}
void colorShow() {
if (x == 1) {
colorRed();
}
if (x == 2) {
colorGreen();
}
if (x == 3) {
colorBlue();
}
if (x == 4) {
colorYellow();
}
if (x == 5) {
colorCyan();
}
if (x == 6) {
colorPurple();
}
}