Code Help!?!?!?

Hello everyone, I'm currently work on an audio project where a microphone detects a sound in HZ then plays the same sound through a speaker repeatedly, but I can not seem to get my code right. Any help is appreciated thanks. :grin:

Heres the code...

Note: The arduinoFFT.h library needs to be added to the Arduino IDE before compiling and uploading this script/sketch to an Arduino.

/*
#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.

arduinoFFT FFT = arduinoFFT();

unsigned int samplingPeriod;
unsigned long microSeconds;

double vReal[SAMPLES]; //create vector of size SAMPLES to hold real values
double vImag[SAMPLES]; //create vector of size SAMPLES to hold imaginary values

void setup()
{
Serial.begin(115200); //Baud rate for the Serial Monitor
samplingPeriod = round(1000000*(1.0/SAMPLING_FREQUENCY)); //Period in microseconds
pinMode(9, 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(0); //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);     //Print out the most dominant frequency.
tone(peak);
reset

}

Hers the code in better format. :smiley:

/*
#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.

arduinoFFT FFT = arduinoFFT();

unsigned int samplingPeriod;
unsigned long microSeconds;

double vReal[SAMPLES]; //create vector of size SAMPLES to hold real values
double vImag[SAMPLES]; //create vector of size SAMPLES to hold imaginary values

void setup()
{
Serial.begin(115200); //Baud rate for the Serial Monitor
samplingPeriod = round(1000000*(1.0/SAMPLING_FREQUENCY)); //Period in microseconds
pinMode(9, 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(0); //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);     //Print out the most dominant frequency.
tone(peak);
reset

}

A Fourier Transform is not a good way to detect a single frequency. It will just tell you how much of the signal power is in each (rather wide) band of frequencies.

I would try one of the many "frequency meter" sketches and look for a frequency that is fairly stable over a period of time.

I will look into that thanks so much for the reply!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.