Can't get ADC working with audio callback

Hi,

I’m very new to Daisy, I have just received my Seed yesterday.

Basically, I’m trying to modify the oscillator frequency using a potentiometer. But the read of ADC always returns the same value whatever the pot position…

I have tested the Knob example from Web programmer, and it works.

If i comment the line seed.StartAudio(AudioCallback); it works. (PS I have tested with empty AudioCallback, same result, doesn’t work)

I’m using

  • libDaisy@c5c1026 like in DaisyExample repo
  • DaisySP@5a6b618 like in DaisyExample repo
  • STM32 Cube IDE

Do you have experienced such issue ?

EDIT : I also have the issue if read the ADC in the AudioCallback

Here is my code :

int main(void)
{
    // initialize seed hardware and oscillator daisysp module
    float sample_rate;
    seed.Configure();
    seed.Init();
    sample_rate = seed.AudioSampleRate();

    // Oscillators init
    for (int i = 0; i < OSC_COUNT; i++)
    {
        freqs[i] = BASE_FREQ + i*FREQ_INC_FACTOR;
        oscs[i].Init(sample_rate);
        oscs[i].SetWaveform(Oscillator::WAVE_RAMP);
        oscs[i].SetFreq(freqs[i]);
        oscs[i].SetAmp(0.5);
    }

    // ADC init
    AdcChannelConfig adcConfig;
    adcConfig.InitSingle(seed.GetPin(21));
    seed.adc.Init(&adcConfig, 1);
    seed.adc.Start();

    // start callback
    seed.StartAudio(AudioCallback);

    volatile bool ledState = false;

    while(1)
    {
        freqModifier = seed.adc.GetFloat(0);

        seed.SetLed(ledState);
        ledState = !ledState;
        dsy_system_delay(100);
    }
}

I’ve managed to get something similar running on the petal, maybe this will help. If you could give me your full source code it might be more helpful.

It’s a bit silly, but sometimes I find rebuilding libdaisy magically fixes problems. Sometimes I’ll pull a new version and forget to rebuild or something. Just a suggestion! Let me know if you have any other ideas.

Here’s what I got working.

#include "daisysp.h"
#include "daisy_seed.h"

using namespace daisysp;
using namespace daisy;

#define OSC_COUNT 3
#define FREQ_INC_FACTOR 100.f

static DaisySeed  seed;
static Oscillator oscs[OSC_COUNT];

void setFreqs(float mod){
	for (int i = 0; i < OSC_COUNT; i++)
    {
        oscs[i].SetFreq(mod + FREQ_INC_FACTOR * i);
    }
}

static void AudioCallback(float *in, float *out, size_t size)
{
    float sig;
	setFreqs(seed.adc.GetFloat(0) * 500);
    for(size_t i = 0; i < size; i += 2)
    {
		sig = 0.f;	
		for (int j = 0; j < OSC_COUNT; j++)
		{
			sig += oscs[j].Process();
		}
		sig /= (float)OSC_COUNT;
        out[i] = out[i + 1] = sig;
    }
}

int main(void)
{
    // initialize seed hardware and oscillator daisysp module
    float sample_rate;
    seed.Configure();
    seed.Init();
    sample_rate = seed.AudioSampleRate();

    for (int i = 0; i < OSC_COUNT; i++)
    {
        oscs[i].Init(sample_rate);
        oscs[i].SetWaveform(Oscillator::WAVE_SIN);
        oscs[i].SetAmp(0.5);
    }
	setFreqs(200.f);

    // ADC init
    AdcChannelConfig adcConfig;
    adcConfig.InitSingle(seed.GetPin(19));
    seed.adc.Init(&adcConfig, 1);
    seed.adc.Start();

    // start callback
    seed.StartAudio(AudioCallback);

    volatile bool ledState = false;

    while(1)
    {		
        seed.SetLed(ledState);
        ledState = !ledState;
        dsy_system_delay(100);
    }
}

This is also being tracked/discussed on github in Issue #277

That’s amasing how this is quite the same code I did :slight_smile:

Here is the full code (note that the read value from ADC is not yet used, but I “read” it using SWD debug probe). I’ll try yours

//-------------------------------------------------
// INCLUDES
//-------------------------------------------------
#include "daisysp.h"
#include "daisy_seed.h"

using namespace daisysp;
using namespace daisy;

//-------------------------------------------------
// DEFINITIONS
//-------------------------------------------------

#define OSC_COUNT           4           ///< Number of oscillators
#define BASE_FREQ           440.0       ///< Base frequency for oscillators
#define FREQ_INC_FACTOR     1.5         ///< Base frequency for oscillators
#define FREQ_MODIFIER_PIN   21

//-------------------------------------------------
// VARIABLES
//-------------------------------------------------
static DaisySeed    seed;               ///< The DaisySeed instance
static Oscillator   oscs[OSC_COUNT];    ///< Oscillators array
static float        freqs[OSC_COUNT];   ///< Oscillators frequencies
static float        freqModifier = 1.0;

//-------------------------------------------------
// CALLBACKS
//-------------------------------------------------

static void AudioCallback(float *in, float *out, size_t size)
{
    float sig;
    freqModifier = seed.adc.GetFloat(0);

    for(size_t i = 0; i < size; i += 2)
    {
        sig = 0;
        for (size_t j = 0; j < OSC_COUNT; j++)
        {
            sig += oscs[j].Process();
        }
        out[i] = sig;// left out
        out[i + 1] = sig; // right out
    }
}

//-------------------------------------------------
// FUNCTIONS
//-------------------------------------------------

int main(void)
{
    // initialize seed hardware and oscillator daisysp module
    float sampleRate;
    seed.Configure();
    seed.Init();
    sampleRate = seed.AudioSampleRate();

    // Oscillators init
    for (int i = 0; i < OSC_COUNT; i++)
    {
        freqs[i] = BASE_FREQ + i*FREQ_INC_FACTOR;
        oscs[i].Init(sampleRate);
        oscs[i].SetWaveform(Oscillator::WAVE_RAMP);
        oscs[i].SetFreq(freqs[i]);
        oscs[i].SetAmp(0.5);
    }

    // ADC init
    AdcChannelConfig adcConfig;
    adcConfig.InitSingle(seed.GetPin(FREQ_MODIFIER_PIN));
    seed.adc.Init(&adcConfig, 1);
    seed.adc.Start();

    // start callback
    seed.StartAudio(AudioCallback);

    while (1);
}

[SOLVED] I this was a connection issue. Wrong Dupont cable so DGND and AGND was not connected to each other…