Strange issue with FHT library for sound-reactive LED project I have been playing with the FHT library and have run into an interesting issue with fht_mag_octave() disrupting the output of my ADC actions.  Running on a MEGA ADK.

The idea is that I'm using FHT to read in the microphone input into A0, but also occasionally checking A2 pot to adjust the volume/brightness of the of the overall FastLED strip.

Here is the code in question...  it's barebones.  Just the standard FHT loop and then, every 100 loops, it checks A2 for the brightness setting.

#include &lt;FastLED.h&gt;

#define LED_PIN     6
#define BRIGHTNESS  200
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 16
CRGB* const leds(NUM_LEDS);

#define LOG_OUT 1 // use the log output function
#define OCTAVE 1 //   // Group buckets into octaves  (use the log output function LOG_OUT 1)
#define OCT_NORM 0 // Don't normalise octave intensities by number of bins
#define FHT_N 128 // set to 128 point fht
#include &lt;FHT.h&gt; // 

void setup() {
  Serial.begin(9600);
  delay(1000);
  FastLED.addLeds&lt;NEOPIXEL, LED_PIN&gt;(leds, NUM_LEDS);
  FastLED.setBrightness (BRIGHTNESS);


  // TIMSK0 = 0; // turn off timer0 for lower jitter (commented this out b/c I think this interferes with FastLED)
  ADCSRA = 0xe5; // set the adc to free running mode (0b11100101), prescalar=32 for higher ADC clock freq
  ADMUX = 0x40 | (0 &amp; 0x07); // use adc0 (0b1000000)
  DIDR0 = 0x01; // turn off the digital input for adc0

}

int counter = 0;

void loop() {

  while (1) { // reduces jitter

    cli();  // UDRE interrupt slows this way down on arduino1.0


    for (int i = 0 ; i &lt; FHT_N ; i++) { // save 128 samples
      while (!(ADCSRA &amp; 0x10)); // wait for adc to be ready  in binary 0010000 (look for ADC Interrupt flag to be set to 1)
      ADCSRA = 0xf5; // restart adc (in binary 11110101...http://www.robotplatform.com/knowledge/ADC/adc_tutorial_2.html)
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int k = (j &lt;&lt; 8) | m; // form into an int
      k -= 0x0200; // form into a signed int
      k &lt;&lt;= 6; // form into a 16b signed int
      fht_input[i] = k; // put real data into bins

    }

    fht_window(); // window the data for better frequency response
    fht_reorder(); // reorder the data before doing the fht
    fht_run(); // process the data in the fht
    fht_mag_octave();
 //   fht_mag_log();

    // every 100th loop, adjust the volume accourding to the value on A2 (Pot)
    if (counter &gt; 10) {

      ADMUX = 0x40 | (2 &amp; 0x07); // set admux to look at Analogpin A2 

      while (!(ADCSRA &amp; 0x10)); // wait for adc to be ready
      ADCSRA = 0xf5; // restart adc 
      byte m = ADCL; // fetch adc data
      byte j = ADCH;
      int brightness = (j &lt;&lt; 8) | m; // form into an int
      
//      brightness = map(brightness, 0, 1023, 0, 255);
      Serial.print("b: ");
      Serial.println(brightness);

      FastLED.setBrightness (brightness);

      ADMUX = 0x40 | (0 &amp; 0x07); // set admux back to look at A0 analog pin (to read the microphone input
      counter = 0;
    }

    counter++;
    sei();

  }
}


Here's the output as shown (basically, it's outputting nonsense, non-int values):

b: ⸮
b: ⸮
b: 
b: 
b: ⸮
b: ⸮
b: 
b: ⸮
b: ⸮
b: 
b: ⸮
b: ⸮
b: ⸮
b: 
b: ⸮
b: ⸮
b: 

b: ⸮
b: ⸮
b: 


If I comment OUT the fht_mag_octave line and uncomment fht_mag_log(), here is the output (which looks right as I turn the Pot pin):

b: 0
b: 0
b: 0
b: 0
b: 0
b: 9
b: 35
b: 38
b: 48
b: 85
b: 143
b: 171
b: 188
b: 204
b: 224
b: 245
b: 280
b: 332


The issue is that the A2 value "brightness" does not read properly if I use fht_mag_octave().  If I comment out only the fht_mag_octave() line, it works perfectly.  I can also use fht_mag_log instead, and it works fine.

This makes me think that there is something that fht_mag_octave() does something that interferes with the ADC registers.

Any ideas?
Thanks to @TonyStewart for suggesting that I just do a successive A2 read.

I modified the code by just repeating the A2 read.  b1 is the first read, and b2 is the 2nd read.

  while (!(ADCSRA &amp; 0x10)); // wait for adc to be ready
  ADCSRA = 0xf5; // restart adc 
  byte m = ADCL; // fetch adc data
  byte j = ADCH;
  int brightness = (j &lt;&lt; 8) | m; // form into an int

  Serial.print("b1: ");
  Serial.print(brightness);

  while (!(ADCSRA &amp; 0x10)); // wait for adc to be ready
  ADCSRA = 0xf5; // restart adc 
   m = ADCL; // fetch adc data
   j = ADCH;
   brightness = (j &lt;&lt; 8) | m; // form into an int

  Serial.print("   b2: ");
  Serial.println(brightness);


And the readout looks good.  B1 is now legible, but incorrect/low.  B2 looks accurate and scales all the way to 1024 as I wanted.


b1: 0   b2: 688
b1: 0   b2: 739
b1: 0   b2: 816
b1: 20   b2: 915
b1: 118   b2: 1016
b1: 152   b2: 1023
b1: 172   b2: 1023
b1: 165   b2: 1023
b1: 180   b2: 1023
b1: 188   b2: 1022
b1: 184   b2: 931
b1: 127   b2: 713
b1: 0   b2: 615
b1: 0   b2: 532
b1: 0   b2: 436
b1: 0   b2: 327
b1: 0   b2: 304
b1: 0   b2: 452


I'm still curious about the reasoning behind this...but I'm glad that there is a solution, and not one that is too painful.