1// Pins2const int micPin = A0;3const int redPin = 3;4const int greenPin = 5;5const int bluePin = 6;6 7// Sound calibration (adjust!)8const int noiseFloor = 520; // silence level9const int maxSound = 750; // loud sound10 11// Smoothing12float smoothLevel = 0;13const float smoothing = 0.06;14 15void setup() {16 pinMode(redPin, OUTPUT);17 pinMode(greenPin, OUTPUT);18 pinMode(bluePin, OUTPUT);19}20 21void loop() {22 int micValue = analogRead(micPin);23 24 // Remove noise floor25 int soundLevel = abs(micValue - noiseFloor);26 soundLevel = constrain(soundLevel, 0, maxSound - noiseFloor);27 28 // Smooth animation29 smoothLevel = smoothLevel * (1 - smoothing) + soundLevel * smoothing;30 31 // Normalize 0–25532 int level = map(smoothLevel, 0, maxSound - noiseFloor, 0, 255);33 level = constrain(level, 0, 255);34 35 // Color mapping36 int r, g, b;37 38 if (level < 85) {39 // Blue → Green40 r = 0;41 g = map(level, 0, 85, 0, 255);42 b = map(level, 0, 85, 255, 0);43 } else if (level < 170) {44 // Green → Yellow45 r = map(level, 85, 170, 0, 255);46 g = 255;47 b = 0;48 } else {49 // Yellow → Red50 r = 255;51 g = map(level, 170, 255, 255, 0);52 b = 0;53 }54 55 analogWrite(redPin, r);56 analogWrite(greenPin, g);57 analogWrite(bluePin, b);58 59 delay(5);60}
Social
Find me online
Follow the latest releases, builds, and behind-the-scenes moments.