Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Audio Library AudioInputAnalog conflict with ADC library?

  1. #1
    Senior Member Davidelvig's Avatar
    Join Date
    Aug 2015
    Location
    Wisconsin
    Posts
    232

    Audio Library AudioInputAnalog conflict with ADC library?

    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:
    Click image for larger version. 

Name:	Teensy Mic and Pot.jpg 
Views:	16 
Size:	120.5 KB 
ID:	14010

    ...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

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,584
    you cannot use ADC with AudioInputAnalog, see the teensy audio gui side bar for AudioInputAnalog, it states "analogRead() must not be used, because AudioInputAnalog is regularly accessing the ADC hardware"

    https://www.pjrc.com/teensy/gui/inde...dioInputAnalog

  3. #3
    Senior Member Davidelvig's Avatar
    Join Date
    Aug 2015
    Location
    Wisconsin
    Posts
    232
    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.

  4. #4
    Quote Originally Posted by Davidelvig View Post
    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...rence-question

  5. #5
    Senior Member Davidelvig's Avatar
    Join Date
    Aug 2015
    Location
    Wisconsin
    Posts
    232
    @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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •