/* Project name: Rewise GFG Steel Can Speaker Written by: Zuzanna Last Update on: March 2023 * ----- WIRING ------ * Microphone: * - - Arduino GND * + - using Arduino UNO: Arduino AREF and Arduino 3.3V (use breadboard to connect all the wires together) * or NANO: Arduino 3.3V * A0 - Arduino A0 * * Neopixel: * G - Arduino GND * V - Arduino 5V * DI - Arduino pin 6 * * Microphone value can be adjusted by rotating potentiometer (blue box on the microphone) * If lighting seems to flickering even when the music is off, increase value in the * */ #include #include #ifdef __AVR__ #include #endif #define N_PIXELS 15 // Number of pixels in strip #define MIC_PIN A0 // Microphone is attached to this analog pin #define LED_PIN 6// NeoPixel LED strip is connected to this pin // Change the value here to control the brightness (0 to 255). 0 turns it off, 255 is the brightest option. int fullBrightness = 150; int brightness = 0; int count = 0; float volume = 0; float last = 0; float maxVolume = 0; float minVolume = 1024; float avgVolume = 0; int currentPixel = 0; int noiseFilter = 2; bool musicOn = true; //MusicStepDown controls how fast the LEDs dim out int musicStepDown = fullBrightness/50 + 1; Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800); //fullPulse variables int paletteThreshold = 1500; int colourCount = 1; // Setup code - only runs once when the Arduino is turned on or the code is uploaded void setup() { analogReference(INTERNAL); analogRead(0); strip.setBrightness(fullBrightness); Serial.begin(9600); // initializing neopixel strip strip.begin(); strip.show(); // Turns leds pixel by pixel on to pink for(int i = 0 ; i < N_PIXELS; i++){ strip.setPixelColor(i, strip.Color(255, i*2, 30)); delay(20); strip.show(); } // Turns off all Neopixel LEDs strip.clear(); volume = analogRead(MIC_PIN); } //Loop code - constantly runs this part of code until Arduino is turned off void loop() { last = volume; volume = analogRead(MIC_PIN); musicOn = abs(last - volume) > noiseFilter;// change this value to lower (to make the microphone more sensitive) or higher(to make microphone less sensitive) Serial.println(volume); if (musicOn){ noiseFilter = 0; brightness = fullBrightness; avgVolume = (avgVolume + volume)/2.0; if (volume > maxVolume) maxVolume = volume; if (volume < minVolume) minVolume = volume; //Choose one of the lighting schemes by leaving one and commenting out the other ones like below. Comment out all them to keep the LEDs off PulseColours(); // PulseFull(); // QuaterPulse(); } else if(currentPixel != 0){ strip.setPixelColor(currentPixel, strip.Color(0,0,0)); currentPixel = currentPixel - 1; } else { noiseFilter = 2; brightness -= musicStepDown; strip.setBrightness(brightness); } strip.show(); count++; // keeps count to enable reset minVolume and MaxVolume if (count > 500){ minVolume = avgVolume; maxVolume = avgVolume; count = 0; } delay(10); } //Below are the functions for coloring Neopixel LEDs //Pulse Colors void PulseColours(){ if (musicOn){ int pulseSize = map(volume, minVolume, maxVolume, 0, N_PIXELS); currentPixel = pulseSize; for (int i = 0; i < N_PIXELS; i++){ if (i < pulseSize){ // strip.setPixelColor(i,Wheel(map(i, 0, N_PIXELS-1 ,30, 150))); strip.setPixelColor(i,Rainbow(map(i,0,strip.numPixels()-1,0,paletteThreshold))); //use this line to change to another colour scheme //don't forget to comment out the one above. } else{ strip.setPixelColor(i, strip.Color(0,0,0)); } } // This line adjusts brightness of the neopixel strip.setBrightness(fullBrightness * pow(volume / maxVolume, 2)); strip.show(); } } void PulseFull(){ if (musicOn){ int pulseSize = map(volume, minVolume, maxVolume, 0,N_PIXELS); if (pulseSize > N_PIXELS/2){ colourCount += (paletteThreshold/70)*pulseSize; } colourCount += 1; if (colourCount >= paletteThreshold){ colourCount = 0; } uint32_t col = Sulfur(colourCount); for (int i = 0; i < N_PIXELS; i++){ strip.setPixelColor(i,col); } Serial.println(fullBrightness * pow((volume / maxVolume), 2.5)); strip.setBrightness(fullBrightness * pow((volume / maxVolume), 2.5)); strip.show(); } } //Pulsing quarter lighting. void QuaterPulse(){ if (musicOn){ int division = 4; int equalParts = N_PIXELS/division; int pulseSize = map(volume, minVolume, maxVolume, 0, equalParts); int pixel = 0; for (int i = 0; i < equalParts; i++){ for (int j = 0; j < equalParts; j++){ pixel = i* equalParts + j; if (j < pulseSize){ strip.setPixelColor(pixel,Rainbow(map(pixel,0,strip.numPixels()-1,0,paletteThreshold))); //Choose your color scheme on this line! } else { strip.setPixelColor(pixel, strip.Color(0,0,0)); } } } strip.setBrightness(fullBrightness * pow(volume / maxVolume, 2)); strip.show(); } } void Circle(){ if (musicOn){ int pulseSize = map(volume, minVolume, maxVolume, 0, N_PIXELS); currentPixel = pulseSize; for (int i = 0; i < N_PIXELS; i++){ if (i < pulseSize){ //strip.setPixelColor(i,Wheel(map(i, 0, N_PIXELS-1 ,30, 150))); strip.setPixelColor(i,Ocean(map(i,0,strip.numPixels()-1,0,paletteThreshold))); //use this line to change to another colour scheme //don't forget to comment out the one above. } else{ strip.setPixelColor(i, strip.Color(0,0,0)); } } strip.setBrightness(fullBrightness * pow(volume / maxVolume, 2)); strip.show(); } } uint32_t Wheel(byte WheelPos) { if(WheelPos < 85) { return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if(WheelPos < 170) { WheelPos -= 85; return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } } uint32_t Rainbow(unsigned int i) { paletteThreshold = 1529; if (i > 1529) return Rainbow(i % 1530); if (i > 1274) return strip.Color(255, 0, 255 - (i % 255)); //violet -> red if (i > 1019) return strip.Color((i % 255), 0, 255); //blue -> violet if (i > 764) return strip.Color(0, 255 - (i % 255), 255); //aqua -> blue if (i > 509) return strip.Color(0, 255, (i % 255)); //green -> aqua if (i > 255) return strip.Color(255 - (i % 255), 255, 0); //yellow -> green return strip.Color(255, i, 0); //red -> yellow } uint32_t Sunset(unsigned int i) { paletteThreshold = 1019; if (i > 1019) return Sunset(i % 1020); if (i > 764) return strip.Color((i % 255), 0, 255 - (i % 255)); //blue -> red if (i > 509) return strip.Color(255 - (i % 255), 0, 255); //purple -> blue if (i > 255) return strip.Color(255, 128 - (i % 255) / 2, (i % 255)); //orange -> purple return strip.Color(255, i / 2, 0); //red -> orange } uint32_t Ocean(unsigned int i) { paletteThreshold = 764; if (i > 764) return Ocean(i % 765); if (i > 509) return strip.Color(0, i % 255, 255 - (i % 255)); //blue -> green if (i > 255) return strip.Color(0, 255 - (i % 255), 255); //aqua -> blue return strip.Color(0, 255, i); //green -> aqua } uint32_t PinaColada(unsigned int i) { paletteThreshold = 764; if (i > 764) return PinaColada(i % 765); if (i > 509) return strip.Color(255 - (i % 255) / 2, (i % 255) / 2, (i % 255) / 2); //red -> half white if (i > 255) return strip.Color(255, 255 - (i % 255), 0); //yellow -> red return strip.Color(128 + (i / 2), 128 + (i / 2), 128 - i / 2); //half white -> yellow } uint32_t Sulfur(unsigned int i) { paletteThreshold = 764; if (i > 764) return Sulfur(i % 765); if (i > 509) return strip.Color(i % 255, 255, 255 - (i % 255)); //aqua -> yellow if (i > 255) return strip.Color(0, 255, i % 255); //green -> aqua return strip.Color(255 - i, 255, 0); //yellow -> green } uint32_t NoGreen(unsigned int i) { paletteThreshold = 1274; if (i > 1274) return NoGreen(i % 1275); if (i > 1019) return strip.Color(255, 0, 255 - (i % 255)); //violet -> red if (i > 764) return strip.Color((i % 255), 0, 255); //blue -> violet if (i > 509) return strip.Color(0, 255 - (i % 255), 255); //aqua -> blue if (i > 255) return strip.Color(255 - (i % 255), 255, i % 255); //yellow -> aqua return strip.Color(255, i, 0); //red -> yellow }