Skip to content

Instantly share code, notes, and snippets.

@marynotari
Created April 2, 2019 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marynotari/362a40513eba73d7a12125ed3f413be5 to your computer and use it in GitHub Desktop.
Save marynotari/362a40513eba73d7a12125ed3f413be5 to your computer and use it in GitHub Desktop.
#include <Adafruit_NeoPixel.h>
#define N_PIXELS 5 // Number of pixels in strand
#define LED_PIN1 6 // 1 = RIGHT
#define LED_PIN2 12 // 2 = LEFT
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(N_PIXELS, LED_PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(N_PIXELS, LED_PIN2, NEO_GRB + NEO_KHZ800);
// Adapted from https://learn.adafruit.com/ursulas-seashell-necklace/the-code
#define MIC_PIN A9 // Analog port for microphone
#define DC_OFFSET 0 // DC offset in mic signal - if unusure, leave 0
#define NOISE 100 // Noise/hum/interference in mic signal and increased value until it went quiet
#define SAMPLES 60 // Length of buffer for dynamic level adjustment
#define TOP (N_PIXELS + 2) // Allow dot to go slightly off scale
#define PEAK_FALL 10 // Rate of peak falling dot
byte
peak = 0, // Used for falling dot
dotCount = 0, // Frame counter for delaying dot-falling speed
volCount = 0; // Frame counter for storing past volume data
int
vol[SAMPLES], // Collection of prior volume samples
lvl = 10, // Current "dampened" audio level
minLvlAvg = 0, // For dynamic adjustment of graph low & high
maxLvlAvg = 512;
void setup() {
// start the LEDs at 'off' and start the volume array at '0' in case there is anything left over from previous run
memset(vol, 0, sizeof(vol)); // See http://www.cplusplus.com/reference/cstring/memset/
strip1.begin();
strip2.begin();
}
void loop() {
// Declare variables for doing the mic math
uint8_t i; // uint8_t = unsigned byte (remains 8 bits on any system)
uint16_t minLvl, maxLvl; // uint16_t = unsigned 2 byte integer (remains 16 bits on any system)
int n, height;
// Rather than using a Mic Library, this example uses raw math (that I do not follow):
n = analogRead(MIC_PIN); // Raw reading from mic
n = abs(n - 512 - DC_OFFSET); // Center on zero (
n = (n <= NOISE) ? 0 : (n - NOISE); // Remove noise/hum (see https://www.tutorialspoint.com/cprogramming/c_operators.htm)
lvl = ((lvl * 7) + n) >> 3; // "Dampened" reading else looks twitchy
// Calculate bar height based on dynamic min/max levels (fixed point):
height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);
// Constrain the lights to the top and bottom of the LED strip
if(height < 0L) height = 0; // Clip output
else if(height > TOP) height = TOP;
if(height > peak) peak = height; // Keep 'peak' at top
// Color pixels based on rainbow gradient - Wheel() cycles through all the colors
for(i = 0; i < N_PIXELS; i++) {
if(i >= height) {
strip1.setPixelColor(i, 0, 0, 0);
strip2.setPixelColor(i, 0, 0, 0);
}
// Change the last two values to change the colors that the pixels cycle thru
else {
strip1.setPixelColor(i, Wheel(map(i, 0, strip1.numPixels()-1, 30, 150)));
strip2.setPixelColor(i, Wheel(map(i, 0, strip2.numPixels()-1, 30, 150)));
}
}
// Draw peak dot
if(peak > 0 && peak <= N_PIXELS-1) {
strip1.setPixelColor(peak,Wheel(map(peak,0,strip1.numPixels()-1,30,150)));
strip2.setPixelColor(peak,Wheel(map(peak,0,strip2.numPixels()-1,30,150)));
}
strip1.show();
strip2.show();
}
// Define Wheel()
// - Input a value 0 to 255 to get a color value.
// - The colors are a transition r - g - b - back to r.
// - WheelPos = the value of whereever the LED color is on the 0-255 scale
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip1.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
// return strip2.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip1.Color(255 - WheelPos * 3, 0, WheelPos * 3);
// return strip2.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip1.Color(0, WheelPos * 3, 255 - WheelPos * 3);
// return strip2.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
} // Wheel()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment