I have some great code from fixed point | FreeIO
to do sound localization. There’s an updated link to the full sketch at the bottom of:
Updated link to code at
It’s very demanding for processing, so needs to be configured correctly. He developed it on a Leonardo board, but I need to run it on a Mega2560. I understand the signal processing stuff and most of the rest in the sketch, but I’m having trouble with configuring the Analog to Digital. He configures the A/D Control Status Registers as follows:
ADCSRA = ((1<< ADEN)| // 1 = ADC Enable
(0<< ADSC)| // ADC Start Conversion
(1<<ADATE)| // 1 = ADC Auto Trigger Enable
(0<< ADIF)| // ADC Interrupt Flag
(0<< ADIE)| // ADC Interrupt Enable
(0<<ADPS1)| // ADC Prescaler Selects 1 MHz.
ADCSRB = ((1<<ADHSM)| // High Speed mode select
(1<< MUX5)| // 1 (10100) ADC1<->ADC4 Gain = 200x.
(0<<ADTS2)| // Sets Auto Trigger source - TIMER0 OCA
This compiles fine for the Leonardo, but the ADHSM and ADTS3 are not known for the Mega 2560.
I looked at the datasheets, and it appears that the ADTS3 is 3 on the Leonardo but bit 3 is Mux5 on the Mega (I’m guessing I could leave it out). ADHSM to set high speed mode is 7 on Leonardo but apparently undefined on the Mega. Seems necessary to accomplish this somehow.
Can someone advise me how to set these 2 parameters, and if you see any other problem with leaving everything else as he has it–this is a blind spot for me, but this code is important for me to master.