The microphone triggers a 35-pixel long NeoPixel strip.
I am using the same wiring as the Drum Project: https://learn.adafruit.com/gemma-powere ... ive-drums/
My issue is that everything works perfectly while I am connected to USB, but when I switch to battery power, the light's either flash at random, or continually stay lit.
I have tested multiple fully charged batteries from 105mAh - 2200 mAh all with the same result.
My suspicion is that it has something to do with the power being too 'dirty' for the microphone... perhaps it's too sensitive when working via battery? I've tested this with the Drum project code as well as my own code below (same results).
Any ideas on how to fix this anomaly?
Code: Select all
/****************************************
Scrolling Sound Meter Sketch for the
Adafruit Microphone Amplifier
****************************************/
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#define PIN 0
// Include the Matrix code for display
Adafruit_NeoPixel strip = Adafruit_NeoPixel(35, PIN, NEO_GRB + NEO_KHZ800);
const int maxScale = 100;
const int lowestZone = 30;
const int redZone = 90;
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;
void setup()
{
strip.begin();
strip.show(); // Initialize all pixels to 'off'
strip.setBrightness(80); //adjust brightness here
}
void loop()
{
unsigned long startMillis = millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(1);
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // save just the max levels
}
else if (sample < signalMin)
{
signalMin = sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin;
// map 1v p-p level to a 0% - 100% scale
int displayPeak = map(peakToPeak, 0, 1023, 0, maxScale);
// draw the new sample
{
if (lowestZone >= displayPeak) // blank these pixels
{
restingState();
}
else if (displayPeak < redZone) // Bright Blue!
{
setStripColor(strip.Color(0, 0, 180));
}
else // Rainbows are awesome!
{
// setStripColor(strip.Color(255, 0, 0));
rainbowCycle_New(0, strip.numPixels());
}
}
strip.show(); // write the changes we just made to the display
//delay(60);
}
void restingState()
{
static int b = 0;
static bool fadeIn = true;
if (b >= 25)
fadeIn = false;
if (b <= 0)
fadeIn = true;
if (fadeIn) b++;
else b--;
setStripColor(strip.Color(0, 0, b));
}
void setStripColor(uint32_t c) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
// strip.show();
// delay(wait);
}
}
void rainbowCycle_New(int start, int finish) {
static uint16_t i = 0;
if(i>=finish)
i = start;
for(uint16_t j=0; j<256*5; j++)
{
if(i< finish)
{
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + random(0, 254)) & 255));
i++;
}
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}