1
#include "arduinoFFT.h"
float y, x = 0;
#define t PE_0
#define SAMPLES 1024             //Must be a power of 2
#define SAMPLING_FREQUENCY 1024 //Hz, must be less than 10000 due to ADC
arduinoFFT FFT = arduinoFFT();


unsigned int sampling_period_us;
unsigned long microseconds;

double  vReal[SAMPLES];
double vImag[SAMPLES];

void setup() {
  Serial.begin(115200);

  sampling_period_us = round(1000000 * (1.0 / SAMPLING_FREQUENCY));
}

void loop() {

  /*SAMPLING*/
  for (int i = 0; i < SAMPLES; i++)
  {
    microseconds = micros();    //Overflows after around 70 minutes!
    y=(analogRead(t))*(3.3/4095.0)-1.65;
    x=((1000.0*y)/201.0);
    vReal[i] = x;
    vImag[i] = 0;

    while (micros() < (microseconds + sampling_period_us)) {
    }
  }

  /*FFT*/
  FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HANN , FFT_FORWARD);
  FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
  FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
  double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);

  /*PRINT RESULTS*/
  //Serial.println(peak);     //Print out what frequency is the most dominant.

  for (int i = 0; i < (SAMPLES / 2); i++)
  {
    /*View all these three lines in serial terminal to see which frequencies has which amplitudes*/

    //Serial.println((i * 1.0 * SAMPLING_FREQUENCY) / SAMPLES, 1);
    //Serial.print(" ");
    Serial.println(vReal[i], 1);    //View only this line in serial plotter to visualize the bins
  }

  delay(1000);  //Repeat the process every second OR:
  //while (1);      //Run code once
Serial.println("//");
}
2
  • 1
    this is the line that you have to replace with I2C comm Serial.println(vReal[i], 1); .......... you have to start by writing code that sends just one Double between the two devices .... then merge the two codes
    – jsotola
    May 1, 2018 at 18:57
  • Are you thinking of using the Arduino as the master or the Pi as the master?
    – Majenko
    May 1, 2018 at 21:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.