Components and supplies
DEVMO Microphone Sensor High Sensitivity Sound Detection Module
WS2812b LED strip
Arduino UNO
Ultrasonic Sensor - HC-SR04 (Generic)
RGB Diffused Common Cathode
Photo resistor
Tools and machines
Soldering iron (generic)
Project description
Code
Code
c_cpp
1#include <FastLED.h> 2#include <Wire.h> 3#include <NewPing.h> 4#include <Vector.h> 5#define LED_PIN 8 6#define NUM_LEDS 50 //50 7#define trigPin 3 8#define echoPin 2 9#define OUT_RED 5 10#define OUT_GREEN 6 11#define MIC A0 12#define LIGHT A1 13#define MAX_DISTANCE 80 14#define SENSITIVITY 2 15 16const int sampleWindow = 15; // Sample window width in mS (50 mS = 20Hz) 17unsigned int sample; 18int hue, acthue, actvalue, value, 19 soundstate, light, actlight, maxvalue, minvalue, bright, diffsum, distanceCm; 20float soundDifference; 21CRGB leds[NUM_LEDS]; 22char state, color, bmode; 23unsigned long aAllow, aTimer, modeallow, modetimer, loopcount, bAllow, bTimer; 24int top = 0; 25int singleLed = 0; 26long duration; 27int storage_array[10]; 28Vector<int> micdata(storage_array); 29 30int diff(int a, int b) { //retirns the difference between two numbers 31 if (a > b) { 32 return a - b; 33 } 34 else { 35 return b - a; 36 } 37} 38 39int measureDistance() { 40 digitalWrite(trigPin, LOW); 41 delayMicroseconds(2); 42 digitalWrite(trigPin, HIGH); 43 delayMicroseconds(10); 44 digitalWrite(trigPin, LOW); 45 duration = pulseIn(echoPin, HIGH); 46 int vissza; 47 vissza = (duration * 0.0340 / 2); 48 if (vissza < MAX_DISTANCE) { 49 return vissza; 50 } 51 else return 0; 52} 53 54void do_A() { //distance measure mode 55 if ((distanceCm < 20 && distanceCm > 18) && color == 'V') { 56 value = 0; 57 FastLED.clear(); 58 return; 59 } 60 61 if (distanceCm < 60 && distanceCm > 20) { //between 20 and 60 cm 62 if (color == 'H') { //changing hue (color) based on the measured distance 63 hue = map(distanceCm, 20, 60, 0, 255); 64 } 65 else if (color == 'V') { //changing value (brightness) based on the measured distance 66 value = map(distanceCm, 20, 60, 0, 255); 67 if (value > 242) { 68 value = 255; 69 } 70 } 71 if (value > actvalue) { //these are here so the value hueValueChanges smoothly 72 actvalue++; 73 } 74 if (value < actvalue) { 75 actvalue--; 76 } 77 if (hue > acthue) { 78 acthue++; 79 } 80 if (hue < acthue) { 81 acthue--; 82 } 83 84 for (int i = 0; i < NUM_LEDS; i++) { //giving value to the leds 85 leds[i] = CHSV(acthue, 255, actvalue); 86 } 87 } 88} 89 90void do_B() { //michrophone mode 91 unsigned long startMillis = millis(); 92 unsigned int peakToPeak = 0; 93 unsigned int signalMax = 0; 94 unsigned int signalMin = 1024; 95 96 // collect data for 50 mS 97 while (millis() - startMillis < sampleWindow) 98 { 99 sample = analogRead(A0); 100 if (sample < 1024) // toss out spurious readings 101 { 102 if (sample > signalMax) 103 { 104 signalMax = sample; // save just the max levels 105 } 106 else if (sample < signalMin) 107 { 108 signalMin = sample; // save just the min levels 109 } 110 } 111 } 112 soundDifference = signalMax - signalMin; // max - min = peak-peak amplitude 113 soundDifference = soundDifference * SENSITIVITY; 114 115 if (top < soundDifference) { 116 top = soundDifference; 117 } 118 119 if (singleLed < top) { 120 singleLed = soundDifference; 121 } 122 if (top > soundDifference) { 123 top--; 124 } 125 if (singleLed > soundDifference && loopcount % 2 == 0) { 126 singleLed--; 127 } 128 if (top > NUM_LEDS) { 129 top = NUM_LEDS; 130 singleLed = NUM_LEDS; 131 } 132 if (top < 2) { 133 top = 0; 134 } 135 if (singleLed < 4) { 136 singleLed = 0; 137 } 138 139 for (int i = 0; i < NUM_LEDS; i++) { 140 if (bmode == 'H') { //in this mode it bounces up and down 141 if (i < top) { 142 leds[i] = CHSV(i * (255 / NUM_LEDS), 255, map(i, 0, NUM_LEDS, 0, 255) + 1); // 143 } 144 else { 145 leds[i] = CRGB(0, 0, 0); 146 } 147 if (singleLed != 0) { 148 leds[singleLed] = CHSV(map(singleLed, 0, NUM_LEDS, 0, 255), 255, 255); 149 } 150 } 151 if (bmode == 'V') { //in this mode all the leds are on, only the brightness changes 152 leds[i] = CHSV(map(i, 0, NUM_LEDS, 0, 255), 255, map(top, 0, NUM_LEDS, 0, 255)); //(i*5)+1 //itt mindegyik világit, a fényerőt allítja 153 } 154 } 155} 156 157void do_C() { //light sensor mode 158 if (light > maxvalue) { 159 maxvalue = light; 160 } 161 if (light < minvalue) { 162 minvalue = light; 163 } 164 if (actlight < light) { 165 actlight++; 166 } 167 if (actlight > light) { 168 actlight--; 169 } 170 171 bright = map(light, minvalue, maxvalue, 0, 255); 172 for (int i = 0; i < NUM_LEDS; i++) { 173 leds[i] = CHSV(48, 255, 255 - bright); //without the "255 - " we can reverse it 174 } 175} 176 177void hueValueChange(char& valami) { 178 if (valami == 'H') { 179 valami = 'V'; 180 } 181 else if (valami == 'V') { 182 valami = 'H'; 183 } 184} 185 186void Changemodes() { 187 if (state == 'A') { 188 state = 'B'; 189 } 190 else if (state == 'B') { 191 state = 'C'; 192 } 193 else if (state == 'C') { 194 state = 'A'; 195 pinMode(trigPin, OUTPUT); 196 pinMode(echoPin, INPUT); 197 for (int i = 0; i < NUM_LEDS; i++) { 198 leds[i] = CHSV(hue, 255, value); 199 } 200 } 201 FastLED.show(); 202} 203 204NewPing sonar(trigPin, echoPin, 40); 205void setup() { 206 //mode declaration 207 FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); 208 pinMode(trigPin, OUTPUT); 209 pinMode(echoPin, INPUT); 210 pinMode(MIC, INPUT); 211 pinMode(LIGHT, INPUT); 212 pinMode(OUT_RED, OUTPUT); 213 pinMode(OUT_GREEN, OUTPUT); 214 //starting mode 215 state = 'A'; 216 color = 'H'; 217 bmode = 'H'; 218 219 light = analogRead(LIGHT); 220 actlight = light; 221 222 //lightsensor 223 maxvalue = 170; 224 minvalue = 35; 225 226 //LED check 227 leds[0] = CRGB(0, 255, 0); 228 FastLED.show(); 229 delay(100); 230 leds[0] = CRGB(0, 0, 0); 231 FastLED.show(); 232 233 //LED starting values 234 hue = 255; 235 acthue = 255; 236 value = 255; 237 actvalue = 255; 238 modeallow = 0; 239 aAllow = 0; 240 modetimer = 0; 241 aTimer = 0; 242 bAllow = 0; 243 bTimer = 0; 244 //debug 245 Serial.begin(115200); 246} 247bool zold = false; 248void loop() { 249 loopcount++; 250 if (state == 'B') { 251 if (loopcount % 10 == 1) { //here only checks every 10th loop, so when reacting to music, it doesnt have to wait so much 252 distanceCm = measureDistance(); 253 } 254 } 255 else { 256 distanceCm = measureDistance(); 257 } 258 259 if (distanceCm < 10 && distanceCm > 1) { 260 if (modeallow < millis()) { 261 digitalWrite(OUT_GREEN, HIGH); 262 digitalWrite(OUT_RED, LOW); 263 zold = true; 264 } 265 else { 266 digitalWrite(OUT_RED, HIGH); 267 } 268 } 269 else { 270 modetimer = millis() + 1500; 271 } 272 if (modetimer < millis() && modeallow < millis()) { 273 Changemodes(); 274 modeallow = millis() + 4000; 275 modetimer = millis() + 5500; 276 } 277 digitalWrite(OUT_RED, LOW); 278 if (distanceCm > 10 || distanceCm == 0) { 279 digitalWrite(OUT_GREEN, LOW); 280 zold = false; 281 } 282 283 if (state == 'A') { 284 if (distanceCm < 80 && distanceCm > 65) { 285 if (aAllow < millis()) { 286 digitalWrite(OUT_GREEN, HIGH); 287 digitalWrite(OUT_RED, LOW); 288 } 289 else { 290 digitalWrite(OUT_RED, HIGH); 291 } 292 } 293 else { 294 aTimer = millis() + 1500; 295 } 296 if (aTimer < millis() && aAllow < millis()) { 297 hueValueChange(color); 298 aAllow = millis() + 4000; 299 aTimer = millis() + 5500; 300 } 301 digitalWrite(OUT_RED, LOW); 302 if ((distanceCm > 80 || distanceCm < 65) && zold == false) { 303 digitalWrite(OUT_GREEN, LOW); 304 } 305 do_A(); 306 } 307 308 if (state == 'B') { 309 if (distanceCm < 80 && distanceCm > 20) { 310 311 if (bAllow < millis()) { 312 digitalWrite(OUT_GREEN, HIGH); 313 digitalWrite(OUT_RED, LOW); 314 } 315 else { 316 digitalWrite(OUT_RED, HIGH); 317 } 318 } 319 320 else { 321 bTimer = millis() + 1500; 322 } 323 if (bTimer < millis() && bAllow < millis()) { 324 hueValueChange(bmode); 325 bAllow = millis() + 4000; //after changing modes, it disables for 4 seconds. 326 bTimer = millis() + 5500; 327 328 } 329 digitalWrite(OUT_RED, LOW); 330 if ((distanceCm > 35 || distanceCm < 30) && zold == false) { 331 digitalWrite(OUT_GREEN, LOW); 332 } 333 do_B(); 334 } 335 336 if (state == 'C') { 337 light = analogRead(LIGHT); 338 do_C(); 339 } 340 FastLED.show(); 341}
Code
c_cpp
1#include <FastLED.h> 2#include <Wire.h> 3#include <NewPing.h> 4#include <Vector.h> 5#define LED_PIN 8 6#define NUM_LEDS 50 //50 7#define trigPin 3 8#define echoPin 2 9#define OUT_RED 5 10#define OUT_GREEN 6 11#define MIC A0 12#define LIGHT A1 13#define MAX_DISTANCE 80 14#define SENSITIVITY 2 15 16const int sampleWindow = 15; // Sample window width in mS (50 mS = 20Hz) 17unsigned int sample; 18int hue, acthue, actvalue, value, 19 soundstate, light, actlight, maxvalue, minvalue, bright, diffsum, distanceCm; 20float soundDifference; 21CRGB leds[NUM_LEDS]; 22char state, color, bmode; 23unsigned long aAllow, aTimer, modeallow, modetimer, loopcount, bAllow, bTimer; 24int top = 0; 25int singleLed = 0; 26long duration; 27int storage_array[10]; 28Vector<int> micdata(storage_array); 29 30int diff(int a, int b) { //retirns the difference between two numbers 31 if (a > b) { 32 return a - b; 33 } 34 else { 35 return b - a; 36 } 37} 38 39int measureDistance() { 40 digitalWrite(trigPin, LOW); 41 delayMicroseconds(2); 42 digitalWrite(trigPin, HIGH); 43 delayMicroseconds(10); 44 digitalWrite(trigPin, LOW); 45 duration = pulseIn(echoPin, HIGH); 46 int vissza; 47 vissza = (duration * 0.0340 / 2); 48 if (vissza < MAX_DISTANCE) { 49 return vissza; 50 } 51 else return 0; 52} 53 54void do_A() { //distance measure mode 55 if ((distanceCm < 20 && distanceCm > 18) && color == 'V') { 56 value = 0; 57 FastLED.clear(); 58 return; 59 } 60 61 if (distanceCm < 60 && distanceCm > 20) { //between 20 and 60 cm 62 if (color == 'H') { //changing hue (color) based on the measured distance 63 hue = map(distanceCm, 20, 60, 0, 255); 64 } 65 else if (color == 'V') { //changing value (brightness) based on the measured distance 66 value = map(distanceCm, 20, 60, 0, 255); 67 if (value > 242) { 68 value = 255; 69 } 70 } 71 if (value > actvalue) { //these are here so the value hueValueChanges smoothly 72 actvalue++; 73 } 74 if (value < actvalue) { 75 actvalue--; 76 } 77 if (hue > acthue) { 78 acthue++; 79 } 80 if (hue < acthue) { 81 acthue--; 82 } 83 84 for (int i = 0; i < NUM_LEDS; i++) { //giving value to the leds 85 leds[i] = CHSV(acthue, 255, actvalue); 86 } 87 } 88} 89 90void do_B() { //michrophone mode 91 unsigned long startMillis = millis(); 92 unsigned int peakToPeak = 0; 93 unsigned int signalMax = 0; 94 unsigned int signalMin = 1024; 95 96 // collect data for 50 mS 97 while (millis() - startMillis < sampleWindow) 98 { 99 sample = analogRead(A0); 100 if (sample < 1024) // toss out spurious readings 101 { 102 if (sample > signalMax) 103 { 104 signalMax = sample; // save just the max levels 105 } 106 else if (sample < signalMin) 107 { 108 signalMin = sample; // save just the min levels 109 } 110 } 111 } 112 soundDifference = signalMax - signalMin; // max - min = peak-peak amplitude 113 soundDifference = soundDifference * SENSITIVITY; 114 115 if (top < soundDifference) { 116 top = soundDifference; 117 } 118 119 if (singleLed < top) { 120 singleLed = soundDifference; 121 } 122 if (top > soundDifference) { 123 top--; 124 } 125 if (singleLed > soundDifference && loopcount % 2 == 0) { 126 singleLed--; 127 } 128 if (top > NUM_LEDS) { 129 top = NUM_LEDS; 130 singleLed = NUM_LEDS; 131 } 132 if (top < 2) { 133 top = 0; 134 } 135 if (singleLed < 4) { 136 singleLed = 0; 137 } 138 139 for (int i = 0; i < NUM_LEDS; i++) { 140 if (bmode == 'H') { //in this mode it bounces up and down 141 if (i < top) { 142 leds[i] = CHSV(i * (255 / NUM_LEDS), 255, map(i, 0, NUM_LEDS, 0, 255) + 1); // 143 } 144 else { 145 leds[i] = CRGB(0, 0, 0); 146 } 147 if (singleLed != 0) { 148 leds[singleLed] = CHSV(map(singleLed, 0, NUM_LEDS, 0, 255), 255, 255); 149 } 150 } 151 if (bmode == 'V') { //in this mode all the leds are on, only the brightness changes 152 leds[i] = CHSV(map(i, 0, NUM_LEDS, 0, 255), 255, map(top, 0, NUM_LEDS, 0, 255)); //(i*5)+1 //itt mindegyik világit, a fényerőt allítja 153 } 154 } 155} 156 157void do_C() { //light sensor mode 158 if (light > maxvalue) { 159 maxvalue = light; 160 } 161 if (light < minvalue) { 162 minvalue = light; 163 } 164 if (actlight < light) { 165 actlight++; 166 } 167 if (actlight > light) { 168 actlight--; 169 } 170 171 bright = map(light, minvalue, maxvalue, 0, 255); 172 for (int i = 0; i < NUM_LEDS; i++) { 173 leds[i] = CHSV(48, 255, 255 - bright); //without the "255 - " we can reverse it 174 } 175} 176 177void hueValueChange(char& valami) { 178 if (valami == 'H') { 179 valami = 'V'; 180 } 181 else if (valami == 'V') { 182 valami = 'H'; 183 } 184} 185 186void Changemodes() { 187 if (state == 'A') { 188 state = 'B'; 189 } 190 else if (state == 'B') { 191 state = 'C'; 192 } 193 else if (state == 'C') { 194 state = 'A'; 195 pinMode(trigPin, OUTPUT); 196 pinMode(echoPin, INPUT); 197 for (int i = 0; i < NUM_LEDS; i++) { 198 leds[i] = CHSV(hue, 255, value); 199 } 200 } 201 FastLED.show(); 202} 203 204NewPing sonar(trigPin, echoPin, 40); 205void setup() { 206 //mode declaration 207 FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); 208 pinMode(trigPin, OUTPUT); 209 pinMode(echoPin, INPUT); 210 pinMode(MIC, INPUT); 211 pinMode(LIGHT, INPUT); 212 pinMode(OUT_RED, OUTPUT); 213 pinMode(OUT_GREEN, OUTPUT); 214 //starting mode 215 state = 'A'; 216 color = 'H'; 217 bmode = 'H'; 218 219 light = analogRead(LIGHT); 220 actlight = light; 221 222 //lightsensor 223 maxvalue = 170; 224 minvalue = 35; 225 226 //LED check 227 leds[0] = CRGB(0, 255, 0); 228 FastLED.show(); 229 delay(100); 230 leds[0] = CRGB(0, 0, 0); 231 FastLED.show(); 232 233 //LED starting values 234 hue = 255; 235 acthue = 255; 236 value = 255; 237 actvalue = 255; 238 modeallow = 0; 239 aAllow = 0; 240 modetimer = 0; 241 aTimer = 0; 242 bAllow = 0; 243 bTimer = 0; 244 //debug 245 Serial.begin(115200); 246} 247bool zold = false; 248void loop() { 249 loopcount++; 250 if (state == 'B') { 251 if (loopcount % 10 == 1) { //here only checks every 10th loop, so when reacting to music, it doesnt have to wait so much 252 distanceCm = measureDistance(); 253 } 254 } 255 else { 256 distanceCm = measureDistance(); 257 } 258 259 if (distanceCm < 10 && distanceCm > 1) { 260 if (modeallow < millis()) { 261 digitalWrite(OUT_GREEN, HIGH); 262 digitalWrite(OUT_RED, LOW); 263 zold = true; 264 } 265 else { 266 digitalWrite(OUT_RED, HIGH); 267 } 268 } 269 else { 270 modetimer = millis() + 1500; 271 } 272 if (modetimer < millis() && modeallow < millis()) { 273 Changemodes(); 274 modeallow = millis() + 4000; 275 modetimer = millis() + 5500; 276 } 277 digitalWrite(OUT_RED, LOW); 278 if (distanceCm > 10 || distanceCm == 0) { 279 digitalWrite(OUT_GREEN, LOW); 280 zold = false; 281 } 282 283 if (state == 'A') { 284 if (distanceCm < 80 && distanceCm > 65) { 285 if (aAllow < millis()) { 286 digitalWrite(OUT_GREEN, HIGH); 287 digitalWrite(OUT_RED, LOW); 288 } 289 else { 290 digitalWrite(OUT_RED, HIGH); 291 } 292 } 293 else { 294 aTimer = millis() + 1500; 295 } 296 if (aTimer < millis() && aAllow < millis()) { 297 hueValueChange(color); 298 aAllow = millis() + 4000; 299 aTimer = millis() + 5500; 300 } 301 digitalWrite(OUT_RED, LOW); 302 if ((distanceCm > 80 || distanceCm < 65) && zold == false) { 303 digitalWrite(OUT_GREEN, LOW); 304 } 305 do_A(); 306 } 307 308 if (state == 'B') { 309 if (distanceCm < 80 && distanceCm > 20) { 310 311 if (bAllow < millis()) { 312 digitalWrite(OUT_GREEN, HIGH); 313 digitalWrite(OUT_RED, LOW); 314 } 315 else { 316 digitalWrite(OUT_RED, HIGH); 317 } 318 } 319 320 else { 321 bTimer = millis() + 1500; 322 } 323 if (bTimer < millis() && bAllow < millis()) { 324 hueValueChange(bmode); 325 bAllow = millis() + 4000; //after changing modes, it disables for 4 seconds. 326 bTimer = millis() + 5500; 327 328 } 329 digitalWrite(OUT_RED, LOW); 330 if ((distanceCm > 35 || distanceCm < 30) && zold == false) { 331 digitalWrite(OUT_GREEN, LOW); 332 } 333 do_B(); 334 } 335 336 if (state == 'C') { 337 light = analogRead(LIGHT); 338 do_C(); 339 } 340 FastLED.show(); 341}
Downloadable files
Circuit diagram
Circuit diagram
Circuit diagram
Circuit diagram
Documentation
Container
Not necessary, but I used it to put the components in it.
Container
Container
Not necessary, but I used it to put the components in it.
Container
Lid for the container
A top part for the container, so when hung on the wall, dust doesn't get in.
Lid for the container
Comments
Only logged in users can leave comments