FFT stm32f103 missing values

Hi

I run this code but serial monitor does not print values.


#include "arduinoFFT.h"

/*
These values can be changed in order to evaluate the functions
*/
#define CHANNEL PA0
const uint16_t samples = 64;           //This value MUST ALWAYS be a power of 2
const double samplingFrequency = 100;  //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];

/* Create FFT object */
ArduinoFFT<double> FFT = ArduinoFFT<double>(vReal, vImag, samples, samplingFrequency);

#define SCL_INDEX 0x00
#define SCL_TIME 0x01
#define SCL_FREQUENCY 0x02
#define SCL_PLOT 0x03

void setup() {
  sampling_period_us = round(1000000 * (1.0 / samplingFrequency));
  Serial.begin(115200);
  while (!Serial)
    ;
  Serial.println("Ready");
}

void loop() {
  /*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;
  }
  /* Print the results of the sampling according to time */
  Serial.print(" Data = ");
  PrintVector(vReal, samples, SCL_TIME);
  FFT.windowing(FFTWindow::Hamming, FFTDirection::Forward); /* Weigh data */
  Serial.print(" Weighed data = ");
  PrintVector(vReal, samples, SCL_TIME);
  FFT.compute(FFTDirection::Forward); /* Compute FFT */
  Serial.print(" Computed Real values = ");
  PrintVector(vReal, samples, SCL_INDEX);
  Serial.print(" Computed Imaginary values = ");
  PrintVector(vImag, samples, SCL_INDEX);
  FFT.complexToMagnitude(); /* Compute magnitudes */
  Serial.print(" Computed magnitudes = ");
  PrintVector(vReal, (samples >> 1), SCL_FREQUENCY);
    Serial.print(" Hz = ");
  double x = FFT.majorPeak();
  Serial.println(x, 6);  //Print out what frequency is the most dominant.
                         // while(1); /* Run Once */
                         // delay(2000); /* Repeat after delay */
  Serial.println();
}

void PrintVector(double *vData, uint16_t bufferSize, uint8_t scaleType) {
  for (uint16_t i = 0; i < bufferSize; i++) {
    double abscissa;
    /* Print abscissa value */
    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("Hz2 = ");
    // Serial.print(" ");
   //Serial.println(vData[i], 4);
  }
 // Serial.print();
}
Data =  Weighed data =  Computed Real values =  Computed Imaginary values =  Computed magnitudes =  Hz = 1.591245


Do you see "Ready" in the serial monitor?

for 0.5 sec

Then what. Are we going to do 20 questions or are you going to follow the instructions in the pinned post re 'How to get the most from the forum'?

The PrintVector code has no print statements, they are all commented out.

when I comment, the values ​​show up, but switching them is annoying, I need to get rid of it.

Un-comment the delay(); ... your are "printlineing" the daata off the monitor.

  // delay(2000); /* Repeat after delay */
  Serial.println();

Here is a rudimentary, non-delay, timer...

unsigned long timer, timeout = 2000; // 2000ms is your timer

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

void loop() {
  if (millis() - timer > timeout) {
    timer = millis();
    Serial.print(".");  // the thing you want to do, including call a function
  }
}

Sorry, that makes no sense. You complain there is no data and I point out that is correct as you have removed via // the relevant print statements. I am done.

get rid of it. = What I mean is removing this part of the code and getting real-time readings that won't repeat after a delay

/* Print abscissa value */
    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;

I feel I still do not understand, but maybe this is what you seek... "won't repeat"... are you trying to print a value one time, and never again? That could be solved with either (1) print value during setup() or (2) setting and testing a "do not repeat" flag... example here...

const char *text = "Print one time from loop() using flag";
bool donotreprint = 0; // clear flag

void setup() {
  Serial.begin(115200);
  Serial.println("Print one time from setup()");
}

void loop() {
  printthis(text);
}

void printthis(char *sometext) {
  if (donotreprint == 0) {
    donotreprint = 1; // set "do not re-print" flag
    Serial.print(text);
  }
}

Or maybe... you want "if the number is 7, do not repeat 7, only repeat when NOT 7" would take storing and comparing an old value to the current value...

int oldrnd;

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

void loop() {
  int rnd = random(5);
  if (rnd != oldrnd) {
    oldrnd = rnd; // store old value
    Serial.print(rnd); // print when different from previous
  }
  delay(1000);
}

something like this one, something like this one, but this one do not respond to PA0 input.

#include "arduinoFFT.h"
  
#define SAMPLES 512               //Must be a power of 2
#define SAMPLING_FREQUENCY 50000  //Hz
#define REFRESH_RATE 10           //Hz
#define ARDUINO_IDE_PLOTTER_SIZE 500
  
//arduinoFFT FFT = arduinoFFT();
  
unsigned long sampling_period_us;
unsigned long useconds_sampling;
 
unsigned long refresh_period_us;
unsigned long useconds_refresh;
//==============================================  
double vReal[SAMPLES];
double vImag[SAMPLES];

 ArduinoFFT<double> FFT = ArduinoFFT<double>(vReal, vImag, SAMPLES, SAMPLING_FREQUENCY);
uint8_t analogpin = PA0;
  
void setup() {
  Serial.begin(115200);
 
  sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));
  refresh_period_us = round(1000000*(1.0/REFRESH_RATE));
 
  pinMode(analogpin, INPUT);
}
  
void loop() {
  useconds_refresh = micros();
   
  /*SAMPLING*/
  for(int i=0; i<SAMPLES; i++)
  {
    useconds_sampling = micros();
  
    vReal[i] = analogRead(analogpin);
    vImag[i] = 0;
  
    while(micros() < (useconds_sampling + sampling_period_us)){
      //wait...
    }
  }  
 
  /*FFT*/
  FFT.windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
  FFT.compute(vReal, vImag, SAMPLES, FFT_FORWARD);
  FFT.complexToMagnitude(vReal, vImag, SAMPLES);
 
  /*PRINT RESULTS*/
  for(int i=2; i<(SAMPLES/2); i++){
    //Serial.println(vReal[i], 1);
    Serial.print(" real = ");Serial.println(vReal[i], 1);
   
   // Serial.print(" real = ");Serial.println(vReal[5], 1);

  }
  for(int i=0; i<(ARDUINO_IDE_PLOTTER_SIZE - (SAMPLES/2)); i++){
   // Serial.println(0);
  }
 
  while(micros() < (useconds_refresh + refresh_period_us)){
    //wait...
  }
}


Either you are not reading PA0 or PA0 is not producing. What do you expect on PA0 and what do you see on PA0?

regardless PA0 is shorted to ground or connected to 3.3V there is no changes on serial monitor, just some noise

= 152.0
 real = 226.8
 real = 311.4
 real = 225.9
 real = 89.1
 real = 212.4
 real = 235.3
 real = 191.1
 real = 116.3
 real = 89.7
 real = 113.3
 real = 122.4
 real = 154.5
 real = 163.2
 real = 196.3
 real = 304.9
 real = 185.9

Don't be that way.

How are you using PA0?

on breadboard wire jumper to ground or floating

Why are you giving me an option? Describe exactly how you are using these.

This is round three of "guess what I'm doing." Stop it.

To see respond PA0 I connect it to ground = expecting zeros on serial monitor when 10cm wire on end is connected to PA0 and second end is not connected I expecting some noise on serial monitor.
Whot I have is a small noise regaldless PA0 connection .

I understand this. What do you want to do with the noise?

I am looking for noise changes depends to jumper connection but there are not present.

I going to change stm32.