Hello, I am making a music visualizer using a strip of neopixel LEDs and the arduinoFFT library. when trying to get the leds to respond to the microphone output, however, they just don't seem to work. I checked the lights and they work just fine with the program if I manually input the values, and the computer imputs the values they show up in the serial port just fine, but the lights don't respond. Any ideas on what the problem might be?
Here is the full code:
/*
Code by Ari Davidson
*/
#include "arduinoFFT.h"
arduinoFFT FFT = arduinoFFT();
#define CHANNEL A0
const uint16_t samples = 64; //This value MUST ALWAYS be a power of 2
const double samplingFrequency = 2093; //Hz, must be less than 10000 due to ADC
unsigned int sampling_period_us;
unsigned long microseconds;
/*
These are the input and output vectors
Input vectors receive computed results from FFT
*/
double vReal[samples];
double vImag[samples];
#define SCL_INDEX 0x00
#define SCL_TIME 0x01
#define SCL_FREQUENCY 0x02
#define SCL_PLOT 0x03
#include <Adafruit_NeoPixel.h>
#include<arduinoFFT.h>
#define LED_PIN 13
#define LED_COUNT 6//number of rows(sub strips)
int pixNum = 5; //number of substrips
Adafruit_NeoPixel strip(LED_COUNT * pixNum, LED_PIN, NEO_GRB + NEO_KHZ800);//initalize led strip
int avol = 0;
int ledNum = 0;
int stripNum = 1;
uint32_t orange = strip.Color(255, 100, 0);
uint32_t yellow = strip.Color(255, 255, 0);
uint32_t green = strip.Color(0, 255, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t purple = strip.Color(255, 0, 255);
void setup(){
strip.begin();
strip.show();
sampling_period_us = round(1000000*(1.0/samplingFrequency));
Serial.begin(115200);
while(!Serial);
Serial.println("Ready");
}
void ledOn(int ledNum, int stripNum){
if(ledNum == 1){
strip.setPixelColor(ledNum+stripNum,orange);
}
else{
if(ledNum == 2){
strip.setPixelColor(ledNum+stripNum,yellow);
}else{
if(ledNum == 3){
strip.setPixelColor(ledNum+stripNum,green);
}
else{
if(ledNum == 4){
strip.setPixelColor(ledNum+stripNum,blue);
}
else{
if(ledNum == 5){
strip.setPixelColor(ledNum+stripNum,purple);
}
else{
strip.setPixelColor(ledNum+stripNum,0,0,0);
}
}
}
}
}
}
void readMic()
{
/*SAMPLING*/
microseconds = micros();
for(int i=0; i<samples; i++)
{
vReal[i] = analogRead(CHANNEL);
vImag[i] = 0;
while(micros() - microseconds < sampling_period_us){
//empty loop
}
microseconds += sampling_period_us;
}
FFT.Compute(vReal, vImag, samples, FFT_FORWARD); /* Compute FFT */
int aval1 = abs(vReal[1]);
avol = 0;
if(100<aval1 && aval1<500){
avol = 1;
} if(499<aval1 && aval1<1000){
avol = 2;
} if(999<aval1 && aval1<1250){
avol = 3;
} if(1249<aval1 && aval1<1500){
avol = 4;
} if(1499<aval1 && aval1<2000){
avol = 5;
}
Serial.print(avol);
Serial.print(" ");
Serial.println(aval1);
}
void PrintVector(double *vData, uint16_t bufferSize, uint8_t scaleType)
{
for (uint16_t i = 0; i < bufferSize; i++)
{
double abscissa;
switch (scaleType)
{
case SCL_INDEX:
abscissa = (i * 1.0);
break;
case SCL_TIME:
abscissa = ((i * 1.0) / samplingFrequency);
break;
case SCL_FREQUENCY:
abscissa = ((i * 1.0 * samplingFrequency) / samples);
break;
}
Serial.print(abscissa, 6);
if(scaleType==SCL_FREQUENCY)
Serial.print("Hz");
Serial.print(" ");
Serial.println(vData[i], 4);
}
Serial.println();
}
void loop(){
readMic();
for(int i = 0; i <= avol; i++){
ledOn(i,0);
}
strip.show();
strip.clear();
delay(1000);
}