My question is this, I’d like to read the volume from the audio output signal and use this to feed an analog LED (or set of LEDs) as a volume indicator. I don’t see a function for this in the API, would I just use the solder pads on the shield and put them into the analog in and read that signal?
Forgive me if I’m missing something obvious. Thanks in advance.
Good question. The volume of the signal is determined by its amplitude. The reading from the ADC is a value from [0-4095] and a value in the middle of this range (2048) represents no sound. That is, the amplitude of an AC audio signal is higher the farther away from 2048. So 0 and 4095 are the loudest. The volume, then, is proportional to the distance of the ADC reading from 2048.
If you want to drive LEDs to show the volume, I would keep track of the current ADC reading in a global volatile variable, then control LEDs from withing the loop() function. Don’t do any calculation or LED control from within the ISR, as this needs to run as fast as possible.
Declare a global variable:
volatile unsigned int currentADC;
In the ISR, set this value whenever you read from the ADC. For example:
signal = AudioHacker.readADC();
currentADC = signal; // store this for access in loop()
Now in loop, calculate the distance of the current reading from silence:
unsigned int volume = abs(2048 - currentADC);
// now use this value in the range of [0, 2048] to control LEDs.
Hope that helps. The key is to do all the hard work (calculate abs, LED control) in loop, not in the ISR.