error: expected unqualified-id before 'if'

Hello people
I am trying to execute a simple code using if statement but when I try to verify the code I get the following error:
expected unqualified-id before ‘if’

I checked my code many times but unfortunately, I cannot detect the error!
please, can anyone help me to detect it?

#include <DMAChannel.h>
#include "pdb.h"
DMAChannel dma(false);
//max_amplitude is 3.3v, 7bit(128)
float amp;
static volatile uint16_t sinetable[128];

if (amp==3.3){

sinetable[]= {
2048,2149,2250,2350,2450,2549,2646,2742,
2837,2929,3020,3108,3193,3275,3355,3431,
3504,3574,3639,3701,3759,3812,3861,3906,
3946,3982,4013,4039,4060,4076,4087,4094,
4095,4091,4082,4069,4050,4026,3998,3965,
3927,3884,3837,3786,3730,3671,3607,3539,
3468,3394,3316,3235,3151,3064,2975,2883,
2790,2695,2598,2500,2400,2300,2199,2098,
1997,1896,1795,1695,1595,1497,1400,1305,
1212,1120,1031,944,860,779,701,627,
556,488,424,365,309,258,211,168,
130,97,69,45,26,13,4,0,
1,8,19,35,56,82,113,149,
189,234,283,336,394,456,521,591,
664,740,820,902,987,1075,1166,1258,
1353,1449,1546,1645,1745,1845,1946,2048,
};}

else if (Amplitude==3.290)
{static volatile uint16_t sinetable[] = {
  2042,2142,2242,2342,2440,2538,2635,2730,
2823,2915,3005,3092,3176,3258,3337,3413,
3486,3555,3620,3682,3740,3793,3843,3888,
3929,3965,3996,4023,4045,4062,4074,4082,
4084,4082,4074,4062,4045,4023,3996,3965,
3929,3888,3843,3793,3740,3682,3620,3555,
3486,3413,3337,3258,3176,3092,3005,2915,
2823,2730,2635,2538,2440,2342,2242,2142,
2042,1942,1842,1742,1644,1546,1449,1354,
1261,1169,1079,992,908,826,747,671,
598,529,464,402,344,291,241,196,
155,119,88,61,39,22,10,2,
0,2,10,22,39,61,88,119,
155,196,241,291,344,402,464,529,
598,671,747,826,908,992,1079,1169,
1261,1354,1449,1546,1644,1742,1842,1942,};
}

else 
{static volatile uint16_t sinetable[] = 
  {2036,2135,2235,2334,2433,2530,2626,2721,
2814,2906,2995,3082,3166,3248,3327,3402,
3475,3544,3609,3670,3728,3781,3831,3876,
3916,3952,3983,4010,4032,4049,4061,4069,
4071,4069,4061,4049,4032,4010,3983,3952,
3916,3876,3831,3781,3728,3670,3609,3544,
3475,3402,3327,3248,3166,3082,2995,2906,
2814,2721,2626,2530,2433,2334,2235,2135,
2036,1936,1836,1737,1638,1541,1445,1350,
1257,1165,1076,989,905,823,744,669,
596,527,462,401,343,290,240,195,
155,119,88,61,39,22,10,2,
0,2,10,22,39,61,88,119,
155,195,240,290,343,401,462,527,
596,669,744,823,905,989,1076,1165,
1257,1350,1445,1541,1638,1737,1836,1936,};}
void setup() {
  dma.begin(true); // allocate the DMA channel first
  
  SIM_SCGC2 |= SIM_SCGC2_DAC0; // enable DAC clock
  DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // enable the DAC module, 3.3V reference
  // slowly ramp up to DC voltage, approx 1/4 second
  for (int16_t i=0; i<2048; i+=8) {
    *(int16_t *)&(DAC0_DAT0L) = i;
    delay(1);
  }
  
  // set the programmable delay block to trigger DMA requests
  SIM_SCGC6 |= SIM_SCGC6_PDB; // enable PDB clock
  PDB0_IDLY = 0; // interrupt delay register
  PDB0_MOD = PDB_PERIOD; // modulus register, sets period
  PDB0_SC = PDB_CONFIG | PDB_SC_LDOK; // load registers from buffers
  PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG; // reset and restart
  PDB0_CH0C1 = 0x0101; // channel n control register?
  
  dma.sourceBuffer(sinetable, sizeof(sinetable));
  dma.destination(*(volatile uint16_t *)&(DAC0_DAT0L));
  dma.triggerAtHardwareEvent(DMAMUX_SOURCE_PDB);
  dma.enable();
}

void loop() {
  // do nothing here
}

You cannot have executable code outside of a function.

BulldogLowell: You cannot have executable code outside of a function.

thanks for replying but what did you mean by "code outside of a function."

if (amp==3.3){

For example

If I understand What you mean, the code should be like this:

#include <DMAChannel.h>
#include "pdb.h"
float Amplitude=3.3;
DMAChannel dma(false);
//max_amplitude is 3.3v, 7bit(128)
if (Amplitude==3.3)
{static volatile uint16_t sinetable[] = {
2048,2149,2250,2350,2450,2549,2646,2742,
2837,2929,3020,3108,3193,3275,3355,3431,
3504,3574,3639,3701,3759,3812,3861,3906,
3946,3982,4013,4039,4060,4076,4087,4094,
4095,4091,4082,4069,4050,4026,3998,3965,
3927,3884,3837,3786,3730,3671,3607,3539,
3468,3394,3316,3235,3151,3064,2975,2883,
2790,2695,2598,2500,2400,2300,2199,2098,
1997,1896,1795,1695,1595,1497,1400,1305,
1212,1120,1031,944,860,779,701,627,
556,488,424,365,309,258,211,168,
130,97,69,45,26,13,4,0,
1,8,19,35,56,82,113,149,
189,234,283,336,394,456,521,591,
664,740,820,902,987,1075,1166,1258,
1353,1449,1546,1645,1745,1845,1946,2048,
};
}
else if (Amplitude==3.290)
{static volatile uint16_t sinetable[] = {
  2042,2142,2242,2342,2440,2538,2635,2730,
2823,2915,3005,3092,3176,3258,3337,3413,
3486,3555,3620,3682,3740,3793,3843,3888,
3929,3965,3996,4023,4045,4062,4074,4082,
4084,4082,4074,4062,4045,4023,3996,3965,
3929,3888,3843,3793,3740,3682,3620,3555,
3486,3413,3337,3258,3176,3092,3005,2915,
2823,2730,2635,2538,2440,2342,2242,2142,
2042,1942,1842,1742,1644,1546,1449,1354,
1261,1169,1079,992,908,826,747,671,
598,529,464,402,344,291,241,196,
155,119,88,61,39,22,10,2,
0,2,10,22,39,61,88,119,
155,196,241,291,344,402,464,529,
598,671,747,826,908,992,1079,1169,
1261,1354,1449,1546,1644,1742,1842,1942,};}

else {static volatile uint16_t sinetable[] = 
  {2036,2135,2235,2334,2433,2530,2626,2721,
2814,2906,2995,3082,3166,3248,3327,3402,
3475,3544,3609,3670,3728,3781,3831,3876,
3916,3952,3983,4010,4032,4049,4061,4069,
4071,4069,4061,4049,4032,4010,3983,3952,
3916,3876,3831,3781,3728,3670,3609,3544,
3475,3402,3327,3248,3166,3082,2995,2906,
2814,2721,2626,2530,2433,2334,2235,2135,
2036,1936,1836,1737,1638,1541,1445,1350,
1257,1165,1076,989,905,823,744,669,
596,527,462,401,343,290,240,195,
155,119,88,61,39,22,10,2,
0,2,10,22,39,61,88,119,
155,195,240,290,343,401,462,527,
596,669,744,823,905,989,1076,1165,
1257,1350,1445,1541,1638,1737,1836,1936,};}


void setup() {
  dma.begin(true); // allocate the DMA channel first
  
  SIM_SCGC2 |= SIM_SCGC2_DAC0; // enable DAC clock
  DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // enable the DAC module, 3.3V reference
  // slowly ramp up to DC voltage, approx 1/4 second
  for (int16_t i=0; i<2048; i+=8) {
    *(int16_t *)&(DAC0_DAT0L) = i;
    delay(1);
  }
  
  // set the programmable delay block to trigger DMA requests
  SIM_SCGC6 |= SIM_SCGC6_PDB; // enable PDB clock
  PDB0_IDLY = 0; // interrupt delay register
  PDB0_MOD = PDB_PERIOD; // modulus register, sets period
  PDB0_SC = PDB_CONFIG | PDB_SC_LDOK; // load registers from buffers
  PDB0_SC = PDB_CONFIG | PDB_SC_SWTRIG; // reset and restart
  PDB0_CH0C1 = 0x0101; // channel n control register?
  
  dma.sourceBuffer(sinetable, sizeof(sinetable));
  dma.destination(*(volatile uint16_t *)&(DAC0_DAT0L));
  dma.triggerAtHardwareEvent(DMAMUX_SOURCE_PDB);
  dma.enable();
}

void loop() {
  // do nothing here
}

If I understand What you mean, the code should be like this:

No.

The if statement MUST be between the { that starts a function body, like setup(), and the } that ends the function body.

Comparing floats exactly is not a good idea. The base 10 representation, 3.3, may not correspond to a unique binary pattern, so the value in the variable could be off by as little as 0.0000001 and not match. You might expect that 3.3000001 is the close enough to 3.3000000, but the compiler will disagree with you (and be right).

PaulS:
No.

The if statement MUST be between the { that starts a function body, like setup(), and the } that ends the function body.

Comparing floats exactly is not a good idea. The base 10 representation, 3.3, may not correspond to a unique binary pattern, so the value in the variable could be off by as little as 0.0000001 and not match. You might expect that 3.3000001 is the close enough to 3.3000000, but the compiler will disagree with you (and be right).

32 bits?

3.3f = 3.2999999523162841796875

The link is useful to understand and although there are an infinite amount of real numbers between the limits of a 32bit float, there are of course only a finite number which may be represented in 32bit binary. That is 4,261,412,866 possibilities including the two zeroes; interestingly, quite a few more than a 32 bit signed integer.

The loss of precision happens where you are between any of those unique representations. Just play with the digits less than 10-6 and see what happens to the binary.

Can you explain what it is that you are trying to accomplish? Because the code you have now is just pure nonsense.

Take this

float Amplitude=3.3;
DMAChannel dma(false);
//max_amplitude is 3.3v, 7bit(128)
if (Amplitude==3.3)

This bit is all out at global scope, not inside any function. So the if is illegal there. But even if it was legal, how would you expect this to work? This is at global scope, happening before anything runs when all the variables are being initialized. You just initialized Amplitude to the value of 3.3. Why in the world would you need any of the if statements? What value other than 3.3 do you think it could possibly have at this point in time? How could it ever equal anything else at this point?

Furthermore, once inside the body of that if statement you define an array called sinetable but this array is in the scope of only the if statement and there are no other lines in there. So what is the point of an array that only exists long enough to get initial values and then immediately ceases to exist? Even if the if statement was legal there, what is it that you think this code would do other than absolutely nothing?

Ok, so you want to initialise a table to one of three different sets of values.

There are two ways to do this.

== METHOD 1 ==

If your amplitude might change at run time, you will need three separate tables, and a variable that switches between them.

int tab1[] = {1,2,3,4};
int tab2[] = {3,4,5,6};
int tab3[] = {5,6,7,8};
int *tab = tab2;

void some_code() {
  for(int i = 0; i<4; i++)
  Serial.println(tab[i]);
}

void some_other_code() {
  if(we_need_to_switch_to_tab3) {
    tab = tab3;
  }
}

== METHOD 2 ==

If you don’t need to switch between tables at run-time, and you just want one set of values compiled into the code but want to be able to switch them out when you build the sketch, then you can use preprocessor directives

// AMP_choice should be 1, 2 or 3
#define AMP_choice 1

#if AMP_choice == 1
  const float AMP = 3.3;
  int tab[] = {1,2,3,4};
#elif AMP_choice == 2
  const float AMP = 3.290;
  int tab[] = {3,4,5,6};
#else
  const float AMP = 3.12345;
  int tab[] = {5,6,7,8};
#endif

The preprocessor acts before the compiler sees the code (hence "pre"processor) - the unwanted parts are removed

PaulMurrayCbr: There are [at least] two ways to do this.

you could also use:

  • a hardware switch
  • simple arrays