Go Down

Topic: compiling error (Read 548 times) previous topic - next topic

Vik321

Hi
I'm trying to replace this line
Code: [Select]
vReal[i] = analogRead(0);
by
Code: [Select]
amplitude[i] = analogRead(PA7);
without declaration  of amplitude I have this error
Code: [Select]
'amplitude' was not declared in this scope

when I declared it,
Code: [Select]
int amplitude;
 I have this error
Code: [Select]
invalid types 'int[int]' for array subscript

What is the solution ?

AWOL

Make amplitude an array
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Vik321

#2
Mar 24, 2019, 01:22 am Last Edit: Mar 24, 2019, 01:23 am by Vik321
I did  this
Code: [Select]
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


johnwasser

I did  this
Code: [Select]
int amplitude [i];

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

 int amplitude ;

                 ^
You have to use a compile-time constant for your array size.  The variable 'i' is not a constant.

Code: [Select]
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.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Vik321

Thanks for suggestions, I also ask google, so far no luck.
The program is compiling without errors in original version ,  "vReal = analogRead(0);"

Code: [Select]
#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
  }


}

pert

Code: [Select]
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:
https://www.arduino.cc/reference/en/language/variables/data-types/array/

lesept

You already declared arrays like here
Code: [Select]
double vReal[SAMPLES];
Just do the same
Code: [Select]
double amplitude[SAMPLES];
BTW does this line compile? 
Code: [Select]
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
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Vik321

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

Code: [Select]
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.

johnwasser

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

Code: [Select]
  /*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)
    {
    }
  }
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

lesept

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
Code: [Select]
vImag[i] = int8_t((amplitude[i] * (cos((i * (twoPi * cycles)) / SAMPLES))) / 2.0);
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Vik321


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:

Vik321

#11
Mar 24, 2019, 04:46 pm Last Edit: Mar 24, 2019, 04:52 pm by Vik321
The results
@johnwasser

Code: [Select]
#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

Vik321

original

Vik321

@lesept

Code: [Select]
#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

AWOL

Code: [Select]
int amplitude = analogRead(0);
//lesept
    vImag[i] = int8_t((amplitude[i]
amplitude is not an array
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up