Last active
March 20, 2019 14:52
-
-
Save JoshLmao/5a63f8b098ba5c9bcaff15700e018cff to your computer and use it in GitHub Desktop.
Reactive Sound Cube π‘
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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