I have my code here sending multiple pulses down a neopixel strip and im using one of adafruits electret microphones to add some audio responsiveness.
It was working fine but somehow now im getting screwy results from the nexopixel strip..
When I call a function to sample the microphone im using then last few pixels go funny..
Its even worse if I use the serial print function anywhere as nearly all the strip goes funky then..
What am I doing wrong?
Video of it with just last pixel going funny
Video of same code with serial print enabled
Many Thanks in advance :)
EDIT** its not noise from my PC as ive tried it o both battery and plug in power
EDIT2** both bits of code work fine on there own...but together something goes wrong..ive tested the strip it works fine with the all other sketches..
Code: Select all
//NeoPixels Function by adafruit
//Mic Sample Function By Adafruit
#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800);
int holdTime =00; ///delay to help with debugging
/////////////////////////////////////////////////////////////////
const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
int micAnalogPin = 0; // mic is connected to analog 1
unsigned int micReading; // the analog reading from the microphone// unsiggned means cannot be negative
//////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
/////////////////////////////////////Pulse Color///////////////////////////////////////////
int cR = 0, cG = 0, cB = 255;
/////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
MovePixelNew();// call pulse function
sampleMic();// sample mic
}
////////////////////////////////////////////////////////////////////////////////////////////
void MovePixelNew(){
uint32_t ledArray[30];// set up the array length as 30
const int Elements = 30;// setup the strip length as 30
SendArray(ledArray, Elements);// calls the function to send the pixels to the strip
////////////////////////////////////////////////////////////////////////////////////////////
for (;;) {
for(int k=0; k<10; k++) {// this bit sets the color of the gap between pulses
for(int i = Elements; i >= 1; i--) {
ledArray[i] = ledArray[i-1];
}
//REM this line to make it solid band of rainbow
ledArray[0] = strip.Color(0, 0, 0); // this sets the color of the gap
///////////////////////////////////////////////////////////////////
delay(20);// need to remove this and use Millis at some point
///////////////////////////////////////////////////////////////////
SendArray(ledArray, Elements);
}/// last colon of second for loop
////////////////////////////Random Pulse Generation for testing////////////////////////////////////////
int pos = GenerateRandom(10);
if(pos > 5){
//////////////////////////////////////////////////////////////////////////////////////////
ledArray[0] = strip.Color(cR, cG, cB);// this is the length of the pulse 3 pixel pulse atm
ledArray[1] = strip.Color(cR, cG, cB);
ledArray[2] = strip.Color(cR, cG, cB);
}/////////last colon of generation
break;
}
}
/////////////////////////////////////////Random Seed Generator////////////////////////////////////////////////////
int GenerateRandom(int maxVal) {
randomSeed(analogRead(0));
return random(0, maxVal+4);
}
//////////////////////////////////////////////send array to strip//////////////////////////////////////////////////
void SendArray(uint32_t ledArray[], int Elements){
for(int j=0; j<Elements; j++) {
strip.setPixelColor(j, ledArray[j]);
}
strip.show();
}
///////////////////////////sample Mic function//////////////////////////////////////////////////////
void sampleMic(){
//Begin Mic Sampleing
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < sampleWindow)
{
micReading = analogRead(micAnalogPin);
if (micReading < 1024) // toss out spurious readings
{
if (micReading > signalMax)
{
signalMax = micReading; // save just the max levels
}
else if (micReading < signalMin)
{
signalMin = micReading; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
Serial.println(volts); // This is the offending line????????????
}