Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reactive Sound Cube 💡
/* Original by Najad on Hackster.io
* https://www.hackster.io/najad/reactive-sound-color-changing-cube-fdf705
*
* Altered by JoshLmao
* https://joshlmao.com
*/
#include "FastLED.h"
#define NUM_LEDS 10
#define DATA_PIN 5
struct S_RGB {
int red;
int green;
int blue;
};
// Amount to delay each cycle by
const int DELAY_MS = 100;
// Set the analog pin to be used for the microphone
const int MIC_PIN = A5;
// Store the last random color
S_RGB m_rgb;
// All LEDs we want to take control of
CRGB m_leds[NUM_LEDS];
// Choose if debug info should be shown through the serial port and on board light
bool m_showDebug = false;
// Returns a random RGB color
S_RGB randColors()
{
S_RGB color;
color.red = random(255);
color.green = random(255);
color.blue = random(255);
}
void setup()
{
// Init serialization for debug info
Serial.begin(9600);
// Configure FastLED to work with our LEDs
FastLED.addLeds<WS2811, DATA_PIN, RGB>(m_leds, NUM_LEDS);
// Set their brightness
LEDS.setBrightness(200);
// Set the first state of the LEDs
for(int i = 0; i < NUM_LEDS; i++)
{
m_leds[i] = CRGB(200, 200, 200);
LEDS.show();
}
}
void loop() {
// Get the current raw value and rescale to be within our custom constraints
int micValue = analogRead(MIC_PIN);
micValue = map(micValue, 0, 1023, 0, 100);
if(m_showDebug) {
Serial.println("Value = " + String(micValue));
}
// Avoid listening to small amounts of noise
int noiseThreshold = 25;
if(micValue > noiseThreshold)
{
if (m_showDebug) {
Serial.println("Value is above threshold");
}
// Iterate over all LEDs with a random color
for(int i = 0; i < NUM_LEDS; i++)
{
// Grabs a random RGB color
m_rgb = randColors();
// Set the led's to the color and update them
m_leds[i] = CRGB(m_rgb.red, m_rgb.green, m_rgb.blue);
LEDS.show();
if (m_showDebug) {
Serial.println("Color = r '" + String(m_rgb.red) + "' g '" + String(m_rgb.green) + "' b '" + String(m_rgb.blue) + "'");
}
}
}
delay(DELAY_MS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.