Arduino Due doesn't do correctly what's inside if() {}

Hi,
I’m programming an Arduino Due board for a physics project and I’ve got trouble with programing it.
Here is my code:

int wave [] = {2048, 3770,3909,2336,498,84,1475,3393,4073,2891,934,0,949,2908,4076,3379,1458,79,510,2354};

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
analogWriteResolution (12);
}

void loop() {
int temperature=analogRead(A0);
Serial.println(temperature);  // put your main code here, to run repeatedly:
delay(2000);
if(temperature>80){
  for(int i = 0; i<20;i++){
      
    analogWriteResolution(12);
    analogWrite(DAC1, wave [i]);
    delayMicroseconds(86);}

}
}

I want my board to send a sine wave when temperature goes over 80, but i can see on the serial port when it does, I can’t hear anything from my speaker except a brief sound that comes regularly every (aproximately) 2 second but it has nothing to do with what i want.

When i only write the code inside the if()

int wave [] = {2048, 3770,3909,2336,498,84,1475,3393,4073,2891,934,0,949,2908,4076,3379,1458,79,510,2354};

void setup() {
  // put your setup code here, to run once:
analogWriteResolution (12);
//Serial.begin(9600)
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int i = 0; i<20;i++){
      if(wave [i]>4095) {
         wave[i]=4095;
     }
    analogWriteResolution(12);
    analogWrite(DAC1, wave [i]);
    delayMicroseconds(86);
  }
}

I obtain a very clear sine wave (with the same setup) of 1700Hz and i can have any other frequency.

So, i’d like my arduino to do exactly the same but only if temperature goes over 80. :slight_smile:

My setup:
-1xthermistor
-1xspeaker
-1xamplifier
-1xarduino due board

If you could help me it would be really nice !

Thanks for your help !

It's the "delay(2000);" that is getting you. You output ONE cycle of you sine wave, and then go to the next cycle of loop() and wait 2 seconds. In your second example you output one cycle, loop() around, and immediately output the next cycle, so you hear a continuous tone.