Running this returns a flickering steam around 250, going at it's lowest to 230 and at it's highest to 270. Shouting into the mic makes it go between exactly 500 at the highest and exactly 0 at the lowest. How should I change these values to get what I want? I'm trying to make a robot that takes the frequency from the mic and uses it to move and turn with 4 motors. here's the code:
#include "arduinoFFT.h"
#define SAMPLES 128 //SAMPLES-pt FFT. Must be a base 2 number. Max 128 for Arduino Uno.
#define SAMPLING_FREQUENCY 2048 //Ts = Based on Nyquist, must be 2 times the highest expected frequency.
double vReal[SAMPLES]; //create vector of size SAMPLES to hold real values
double vImag[SAMPLES]; //create vector of size SAMPLES to hold imaginary values
ArduinoFFT<double> FFT = ArduinoFFT<double>(vReal, vImag, SAMPLES, SAMPLING_FREQUENCY);
unsigned int samplingPeriod;
unsigned long microSeconds;
int BL = 11;
int TL =12;
int BR = 9;
int TR = 8;
void setup()
{
Serial.begin(115200); //Baud rate for the Serial Monitor
samplingPeriod = round(1000000*(1.0/SAMPLING_FREQUENCY)); //Period in microseconds
pinMode(BL,OUTPUT);
pinMode(TL,OUTPUT);
pinMode(BR,OUTPUT);
pinMode(TR,OUTPUT);
}
void loop()
{
/*Sample SAMPLES times*/
for(int i=0; i<SAMPLES; i++)
{
microSeconds = micros(); //Returns the number of microseconds since the Arduino board began running the current script.
vReal[i] = analogRead(A0); //Reads the value from analog pin 0 (A0), quantize it and save it as a real term.
vImag[i] = 0; //Makes imaginary term 0 always
/*remaining wait time between samples if necessary*/
while(micros() < (microSeconds + samplingPeriod))
{
//do nothing
}
}
/*Perform FFT on samples*/
FFT.windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.complexToMagnitude(vReal, vImag, SAMPLES);
/*Find peak frequency and print peak*/
double peak = FFT.majorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
Serial.println(peak);
Serial.println(analogRead(A0));
if (peak<200){
digitalWrite(BL, LOW);
digitalWrite(TL, LOW);
digitalWrite(BR, LOW);
digitalWrite(TR, LOW);
}
if ((peak>230 and peak<245) || (peak>465 and peak<510)){
Serial.println("C");
digitalWrite(BL, HIGH);
digitalWrite(TL, HIGH);
digitalWrite(BR, HIGH);
digitalWrite(TR, HIGH);
}
if ((peak>250 and peak<280) || (peak>510 and peak<540)){
Serial.println("D");
digitalWrite(BL, LOW);
digitalWrite(TL, LOW);
digitalWrite(BR, HIGH);
digitalWrite(TR, HIGH);
}
if ((peak>285 and peak<310) || (peak>560 and peak<610)){
Serial.println("E");
digitalWrite(BL, HIGH);
digitalWrite(TL, HIGH);
digitalWrite(BR, HIGH);
digitalWrite(TR, HIGH);
}
if ((peak>310 and peak<330) || (peak>610 and peak<650)){
Serial.println("F");
digitalWrite(BL, HIGH);
digitalWrite(TL, HIGH);
digitalWrite(BR, LOW);
digitalWrite(TR, LOW);
}
if ((peak>345 and peak<360) || (peak>700 and peak<770)){
Serial.println("G");
digitalWrite(BL, HIGH);
digitalWrite(TL, HIGH);
digitalWrite(BR, HIGH);
digitalWrite(TR, HIGH);
}
if ((peak>380 and peak<410) || (peak>790 and peak<900)){
Serial.println("A");
digitalWrite(BL, HIGH);
digitalWrite(TL, HIGH);
digitalWrite(BR, LOW);
digitalWrite(TR, LOW);
}
if ((peak>200 and peak<230) || (peak>430 and peak<460)){
Serial.println("B");
digitalWrite(BL, LOW);
digitalWrite(TL, LOW);
digitalWrite(BR, HIGH);
digitalWrite(TR, HIGH);
}
}