Audio Library AudioInputAnalog conflict with ADC library?

Status
Not open for further replies.

Davidelvig

Well-known member
I recall encountering this before, but can't find the thread.
Latest Arduino (1.8.5) and Teensyduino (1.4.2)
Mac OSX
Teensy 3.2
1.2V output mic (Neutronned)
simple blue pot
external power in this case (USB power cut)
This code:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

#include <ADC.h>
// This app, as written compiles and runs
//     - showing FFT bins from an analog mic (AudioInputAnalog)
//     - and variable input from an analog POT using ADC (0-65530)
// After swapping the order of the following two lines
//     - FFT persistently shows unavailable,
//     - and ADC.read() returns 0-1023
ADC *adc = new ADC();
AudioInputAnalog         Mic;

AudioAnalyzeFFT1024      FFT;
AudioConnection          patchCord3(Mic, FFT);

#define MAXINT16 65530
#define ADC_PIN A3

void setup() {
    AudioMemory(50);  
    pinMode(ADC_PIN, INPUT);  
}

void loop() {
   printFFT();
   printADC();
   delay(50);
}

void printFFT() {
   if (FFT.available()) {
       Serial.print("FFT: ");
       for (int i = 0; i < 60; i++) {
           float n = FFT.read(i);
           if (n >= 0.02) {             // arbitrary minimal value
               Serial.print("^^");
           } else {
               Serial.print("  ");
           }
        }
        Serial.println();
    } else {
        Serial.println("FFT not available");
    }
}

void printADC()
{ 
    int a = adc->analogRead(ADC_PIN, ADC_1);
    int m = map(a, 0, MAXINT16, 0, 128);
    Serial.printf("ADC: %i\t", a);
    for (int x = 0; x < 128; x++) {
        if (x <= m)
            Serial.print("a");
        else
            Serial.print(" ");
    }
    Serial.println();
}

...works as expected with this simple setup:
Teensy Mic and Pot.jpg

...until the adc and AudioInputAnalog declarations are swapped in the code.

Is there are order-of-declaration that I need to watch for in these two libraries, or am I missing something else?



Thanks
Dave
 
Thanks, @manitou. I'm familiar with Audio library's competition with standard analogRead() methods.

My sketch uses @pedvide's ADC library included with Teensyduino(#include <ADC.h>), which allows selecting which of the Teensy's two ADCs to use kin the call to analogRead().

I've assumed (and it appears) that the Audio library's AudioInputAnalog uses ADC_0 by default.
I'm specifying ADC_1 for using with the ADC library.

Again, it works well and consistently when declared in the order in this code, and fails in a consistent way when the declarations are swapped.
 
Thanks, @manitou. I'm familiar with Audio library's competition with standard analogRead() methods.

My sketch uses @pedvide's ADC library included with Teensyduino(#include <ADC.h>), which allows selecting which of the Teensy's two ADCs to use kin the call to analogRead().

I've assumed (and it appears) that the Audio library's AudioInputAnalog uses ADC_0 by default.
I'm specifying ADC_1 for using with the ADC library.

Again, it works well and consistently when declared in the order in this code, and fails in a consistent way when the declarations are swapped.

Yes, the order is important. The ADC object MUST be initialised before the audio object. I wrap them in a struct so the intialisation order is clear. https://github.com/cutlasses/TeensyBoardTestV2

The ADC library doesn't play nicely with the audio library if you want to change the voltage reference of ADC_1 (but not ADC_0). See this thread https://forum.pjrc.com/threads/50533-Voltage-reference-question
 
@unicornpower,
Thanks for this!
Your first link confirmed my findings (which was really helpful)
... and I had just found the second problem (unchangeable 1.2V on ADC_1), which your second link fixes.

I'm back in business
 
Status
Not open for further replies.
Back
Top