compiling error

Hi
I'm trying to replace this line

vReal[i] = analogRead(0);

by

amplitude[i] = analogRead(PA7);

without declaration of amplitude I have this error

'amplitude' was not declared in this scope

when I declared it,

int amplitude;

I have this error

invalid types 'int[int]' for array subscript

What is the solution ?

Make amplitude an array

I did this

int amplitude [i];

and error is,

ftt02:9:17: error: array bound is not an integer constant before ']' token

int amplitude ;

  • ^*
    In function 'void loop()':
    ftt02:39:5: error: 'amplitude' was not declared in this scope
    _ amplitude = analogRead(0);_
    * ^~~~~~~~~*
    exit status 1
    array bound is not an integer constant before ']' token

Vik321:
I did this

int amplitude [i];

and error is,
ftt02:9:17: error: array bound is not an integer constant before ']' token

int amplitude ;

  • ^*
    [/quote] You have to use a compile-time constant for your array size. The variable 'i' is not a constant.
    > Vik321:
    > ```
    *> In function 'void loop()':

ftt02:39:5: error: 'amplitude' was not declared in this scope

amplitude[i] = analogRead(0);

^*

> ```
Because the declaration of 'amplitude' failed, the array is not defined.

Thanks for suggestions, I also ask google, so far no luck.
The program is compiling without errors in original version , "vReal = analogRead(0);"
```
*#include "arduinoFFT.h"
#include <math.h>
#define SAMPLES 128            //Must be a power of 2
#define SAMPLING_FREQUENCY 5000 //Hz, must be less than 10000 due to ADC
const double signalFrequency = 1000;

//this part is not used for "vReal[i] = analogRead(0);" , and is compiling without errors
///////////////////////
int i;
int amplitude [i];
/////////////////////

arduinoFFT FFT = arduinoFFT();

unsigned int sampling_period_us;
unsigned long microseconds;

double vReal[SAMPLES];
double vImag[SAMPLES];
double cycles = (((SAMPLES - 1) * signalFrequency) / SAMPLING_FREQUENCY);

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!

///////////////////////////////////////
    //vReal[i] = analogRead(0);
    // vImag[i] = 0;
    amplitude[i] = analogRead(0);
//////////////////////////////////////

vReal[i] = int8_t((amplitude * (sin((i * (twoPi * cycles)) / SAMPLES))) / 2.0);
    vImag[i] = int8_t((amplitude * (cos((i * (twoPi * cycles)) / SAMPLES))) / 2.0);

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

/FFT/
  FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, 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.print((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
  }

}*
```

Vik321:

int i;

int amplitude [i];

Global variables like your i here are initialized to 0 by default. So you are declaring an array named amplitude with 0 elements. That's not going to be terribly useful.

Please spend some time studying this:

You already declared arrays like here

double vReal[SAMPLES];

Just do the same

double amplitude[SAMPLES];

BTW does this line compile?

double cycles = (((SAMPLES - 1) * signalFrequency) / SAMPLING_FREQUENCY) ;

Only the declaration of the variable should be there and the calculation of its value should be in the setup

Thanks lesept.
Your suggestion is working and your question = not.
Here is the error.

ftt02:42:34: error: invalid operands of types 'double [128]' and 'double' to binary 'operator*'

     vReal[i] = int8_t((amplitude * (sin((i * (twoPi * cycles)) / SAMPLES))) / 2.0);

                        ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ftt02:43:34: error: invalid operands of types 'double [128]' and 'double' to binary 'operator*'

     vImag[i] = int8_t((amplitude * (cos((i * (twoPi * cycles)) / SAMPLES))) / 2.0);

                        ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

exit status 1
invalid operands of types 'double [128]' and 'double' to binary 'operator*'

I should put all program from beginning = sorry.

You can't multiply an array. Use a simple variable like this:

  /*SAMPLING*/
  for (int i = 0; i < SAMPLES; i++)
  {
    microseconds = micros();    //Overflows after around 70 minutes!

    int amplitude = analogRead(0);
    vReal[i] = int8_t((amplitude * (sin((i * (twoPi * cycles)) / SAMPLES))) / 2.0);
    vImag[i] = int8_t((amplitude * (cos((i * (twoPi * cycles)) / SAMPLES))) / 2.0);

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

As JohnWasser says, you may not need an array for the amplitude if all you need further is real and imaginary parts. In this case do not delare the amplitude array.

Otherwise do

vImag[i] = int8_t((amplitude[i] * (cos((i * (twoPi * cycles)) / SAMPLES))) / 2.0);

Thanks
@johnwasser = no errors
@lesept = I will check that also when I hook up loop antenna for testing.
Yes antenna, I trying to build a direction finder.
There is one more future which I need to add to program, a button.
When button is pressed on serial monitor I should have 0 deg reading, when antenna is rotating the number will change, for that I will use atan(); function.
will this work?
if pin is high(button pressed) atan() = 0:

The results
@johnwasser

#include "arduinoFFT.h"
#include <math.h>
#define SAMPLES 128             //Must be a power of 2
#define SAMPLING_FREQUENCY 5000 //Hz, must be less than 10000 due to ADC
const double signalFrequency = 1000;

arduinoFFT FFT = arduinoFFT();

unsigned int sampling_period_us;
unsigned long microseconds;

//double amplitude[SAMPLES];
double vReal[SAMPLES];
double vImag[SAMPLES];
double cycles = (((SAMPLES - 1) * signalFrequency) / SAMPLING_FREQUENCY);

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

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

void loop() {

  /*SAMPLING*/

 /////////////////////////////////////// 
    /*SAMPLING*/
  for (int i = 0; i < SAMPLES; i++)
  {
    microseconds = micros();    //Overflows after around 70 minutes!

    int amplitude = analogRead(0);
    vReal[i] = int8_t((amplitude * (sin((i * (twoPi * cycles)) / SAMPLES))) / 2.0);
    vImag[i] = int8_t((amplitude * (cos((i * (twoPi * cycles)) / SAMPLES))) / 2.0);

    while (micros() - microseconds < sampling_period_us)
    {
    }
  }
///////////////////////////////////////////////
  /*FFT*/
  FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, 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.print((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
  }


}

noise, no respond to input signal

sine.jpg

original

org.jpg

@lesept

#include "arduinoFFT.h"
#include <math.h>
#define SAMPLES 128             //Must be a power of 2
#define SAMPLING_FREQUENCY 5000 //Hz, must be less than 10000 due to ADC
const double signalFrequency = 1000;


//this part is not used for "vReal[i] = analogRead(0);" , and is compiling without errors
///////////////////////
//int amplitude = 35;
/////////////////////


arduinoFFT FFT = arduinoFFT();

unsigned int sampling_period_us;
unsigned long microseconds;

//double amplitude[SAMPLES];
double vReal[SAMPLES];
double vImag[SAMPLES];
double cycles = (((SAMPLES - 1) * signalFrequency) / SAMPLING_FREQUENCY);

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

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

void loop() {

  /*SAMPLING*/

  ///////////////////////////////////////
  /*SAMPLING*/
  for (int i = 0; i < SAMPLES; i++)
  {
    microseconds = micros();    //Overflows after around 70 minutes!

    int amplitude = analogRead(0);
//lesept
    vImag[i] = int8_t((amplitude[i] * (cos((i * (twoPi * cycles)) / SAMPLES))) / 2.0);
    vReal[i] = int8_t((amplitude[i] * (sin((i * (twoPi * cycles)) / SAMPLES))) / 2.0);

//johnwasser, pic sine
   // vReal[i] = int8_t((amplitude * (sin((i * (twoPi * cycles)) / SAMPLES))) / 2.0);
  // vImag[i] = int8_t((amplitude * (cos((i * (twoPi * cycles)) / SAMPLES))) / 2.0);

    while (micros() - microseconds < sampling_period_us)
    {
    }
  }
  ///////////////////////////////////////////////
  /*FFT*/
  FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, 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.print((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
  }
}

The error
fft05:42:35: error: invalid types 'int[int]' for array subscript

vImag = int8_t((amplitude * (cos((i * (twoPi * cycles)) / SAMPLES))) / 2.0);
* ^*
fft05:43:35: error: invalid types 'int[int]' for array subscript
vReal = int8_t((amplitude * (sin((i * (twoPi * cycles)) / SAMPLES))) / 2.0);
* ^*
exit status 1
invalid types 'int[int]' for array subscript

int amplitude = analogRead(0);
//lesept
    vImag[i] = int8_t((amplitude[i]

amplitude is not an array

original

//
/*
This code has three “print modes”:

Line 41 can print to terminal which frequency is most dominant.
Lines 47-49 can print to terminal the values of each bin.
Line 49 will print can print to plotter the visualization of all the bins.
*/
//https://www.norwegiancreations.com/2017/08/what-is-fft-and-how-can-you-implement-it-on-an-arduino/
//https://github.com/kosme/arduinoFFT/blob/master/Examples/FFT_04/FFT_04.ino
#include "arduinoFFT.h"
 
#define SAMPLES 128             //Must be a power of 2
#define SAMPLING_FREQUENCY 1000 //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!
     
       vReal[i] = analogRead(0);
        
        vImag[i] = 0;
     
        while(micros() < (microseconds + sampling_period_us)){
        }
    }
 
    /*FFT*/
    FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, 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.print((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
}

in original

 vReal[i] = analogRead(0);

So what?

so why

amplitude[i] = analogRead(0);

is causing the problems

Vik321:
so why

amplitude[i] = analogRead(0);

is causing the problems

amplitude is not an array.
It doesn't take a subscript.

Ok, here are the questions you need to ask yourself

  • What is it you measure with your sensor, using analogRead ? Amplitude or real part?
  • What do you want to display to the user or make calculations on?

If you can answer these questions, then you can write your code.

If you want to display or calculate something from the real and imaginary parts, such as the FFT, then you need to store those values in arrays and only those values. In this case the amplitude is just an intermediate variable that you use once and don't need to store for later use.

If you need to calculate or display all the amplitude values, then you have to store them in an array.

To declare an array, so as you already did

double whatever[SAMPLES];

And to store or use the i-th value of this array, use whatever [ i ]

All the variables you don't store in arrays, you just declare them as global variables or local when you need to use them

I hope this is clear, because I am not a native English speaker (at all)