Pitch perfect audio on Arduino Micro

oh wow! thanks a lot! now it works perfectly even at 20kHz, and I learned a lot in the process.
this is the working code:

/*** DEFINES */
     #define SND 3 /* PORTD0 */
     #define LED 13
     #define POT_A A4 /* ADC1 */
     
     #define PRESCALER 1 /* 1 8 64 256 1024 */
     #define SAMPLERATE 20000
     #define TIMER_INTERVAL (uint16_t)((1000.f/(float)SAMPLERATE) / (1000.f/((float)F_CPU/(float)PRESCALER))) /* in 1/(F_CPU/PRESCALER) units */
     
     // defines for setting and clearing register bits
     #ifndef cbi
          #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) // ClearBIt -> 0
     #endif
     #ifndef sbi
          #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) // SetBIt -> 1
     #endif
/* DEFINES end. */

/*** GLOBALS */
     uint8_t ledStatus = 0;
     uint32_t ledAcc = 0;
     
     uint8_t sndStatus = 0;
     uint32_t sndAcc = 0;
     #define FIXFREQ 1000.0
     
     uint32_t potAcc = 0;
     uint16_t pota16;
/* GLOBALS end. */

void setup(){
     pinMode(LED, OUTPUT);
     pinMode(SND, OUTPUT);
     pinMode(POT_A, INPUT);

	TCNT1 = 0; // clear timer counter
	TCCR1A = 0b11000000; // select the compare match A to set flag on match
	TCCR1B = 0b00001000; /* CTC1 is set, so the timer will reset on compare match */
	OCR1A  = TIMER_INTERVAL; // set output compare value
	TIMSK1 = 0b00000010; // enable timer compare match A interrupt
     
     #if PRESCALER == 1
          TCCR1B |= 0b00000001;
     #elif PRESCALER == 8
          TCCR1B |= 0b00000010;
     #elif PRESCALER == 64
          TCCR1B |= 0b00000011;
     #elif PRESCALER == 256
          TCCR1B |= 0b00000100;
     #elif PRESCALER == 1024
          TCCR1B |= 0b00000101;
     #endif

     // // set ADC prescale to 128, slows ADC samplerate, lightweight
     // sbi(ADCSRA,ADPS2) ;
     // sbi(ADCSRA,ADPS1) ;
     // sbi(ADCSRA,ADPS0) ;
     
     // free running adc
     cbi(ADCSRB, ADTS3);
     cbi(ADCSRB, ADTS2);
     cbi(ADCSRB, ADTS1);
     cbi(ADCSRB, ADTS0);
     
     sbi(ADCSRA, ADEN); /* ADC enable */
     sbi(ADCSRA, ADIE); /* ADC completed interrupt enable */
     sbi(ADCSRA, ADSC); // kickstart conversion for the ADC

	sei(); // enable interrupts
     
     // Serial.begin(9600);
}

void loop(){
}

ISR(ADC_vect){
     // sei(); /* enable global interrupt flag */
     uint8_t low = ADCL;
     uint8_t high = ADCH;
     pota16 = (high << 8) | low;
}

ISR(TIMER1_COMPA_vect){
     sndAcc++;
     if(sndAcc <= (uint32_t)(((float)SAMPLERATE / FIXFREQ) / 2.f) ){
          PORTD |= 0b00000001; // digitalWrite(SND, 1);
     }
     else{
          PORTD &= 0b11111110; // digitalWrite(SND, 0);
     }
     if(sndAcc >= (uint32_t)((float)SAMPLERATE / FIXFREQ) ){
          sndAcc = 0;
     }
     
     if(ledAcc == (uint32_t)((float)SAMPLERATE / 2.f) ){ /* flipping led's state 2times per second */
          ledAcc = 0;
          ledStatus = !ledStatus;
          digitalWrite(LED, ledStatus);
          // Serial.println(pota16);
     }
     else{
          ledAcc++;
     }
     
     if(potAcc == (uint32_t)((float)SAMPLERATE / 30.f) ){ /* reading pot value 30 times per second */
          potAcc = 0;
          ADMUX = 0b11000001; /* Internal 2.56V Voltage Reference, MUX set to read from ADC1(pin A4) */
          sbi(ADCSRA, ADSC); /* start conversion */
     }
     else{
          potAcc++;
     }
}

I also added a Serial.print() to check if I was actually reading changes from the potentiometer and yeah the serialprint itself does cause clicks, so I just commented it out :slight_smile:

Adding sei() inside the ADComplete ISR doesn't seem to make a difference, I think that's because when the TIMER1_COMPA interrupt code finished running it automatically resets the global interrupt flag, and being faster than the ADC ISR, it resumes normal execution before it.
Things might change if I add code to the TIMER1_COMPA ISR, I guess, but I'm not really sure this assumption is correct overall.