ADC library, Teensy 4.1: setSamplingSpeed() errors

Miq58

Member
I am starting to use the ADC library (https://github.com/pedvide/ADC) on the Teensy 4.1. The basics seem to work, but when I tried to change the sampling speed by

Code:
 adc.setSamplingSpeed(ADC_SAMPLING_SPEED::LOW_SPEED);

I get thrown the following error:

Code:
src\main.cpp: In function 'void setup()':
src\main.cpp:40:54: error: call to 'ADC::setSamplingSpeed' declared with attribute error: Use adc->adcX->setSamplingSpeed instead
   adc.setSamplingSpeed(ADC_SAMPLING_SPEED::LOW_SPEED); // change the sampling speed
                                                      ^
*** [.pio\build\teensy41\src\main.cpp.o] Error 1

But if I use the call as advised in the error message, I instead get:

Code:
src\main.cpp: In function 'void setup()':
src\main.cpp:40:7: error: 'class ADC' has no member named 'adcX'
   adc.adcX->setSamplingSpeed(ADC_SAMPLING_SPEED::LOW_SPEED); // change the sampling speed
       ^
*** [.pio\build\teensy41\src\main.cpp.o] Error 1

What am I doing wrong here?

The complete sketch is below.

Code:
#define HAS_USBMIDI TRUE

#include <Arduino.h>
#include <ADC.h>

#define ANALOGPIN A9
ADC adc; // adc object

const uint32_t SAMPLES(2000);
int v[SAMPLES];
int p[SAMPLES];
int n[SAMPLES];
float slope[SAMPLES];
const uint8_t DAMPER(5);
struct Damp {
  int v;
  int v2;
  int vx;
}; 
Damp d[DAMPER];
constexpr int sumx = DAMPER * (DAMPER - 1) / 2;
constexpr int sumx2 = DAMPER * (DAMPER - 1) * (2 * DAMPER - 1) / 6;
constexpr int denom = DAMPER * sumx2 - sumx * sumx;
int mid = 0;
const float SLOPLIM(8);
const int AVGLIM(5);
const uint16_t SHIFTVAL(0);
const uint16_t SAMPLEINTERVAL(5);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("--OK--");

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  adc.adcX->setSamplingSpeed(ADC_SAMPLING_SPEED::LOW_SPEED); // change the sampling speed

  for (uint8_t i = 0; i < DAMPER; ++i) {
    d[i].v = adc.analogRead(ANALOGPIN) >> SHIFTVAL;
    d[i].v2 = d[i].v * d[i].v;
    d[i].vx = d[i].v * i;
    mid += d[i].v;
  }
  mid /= DAMPER;
}

void loop() {
  static uint32_t atic = millis();   // pad sample timer
  static uint32_t ticks = 0;         // counter for samples stored
  static int noteval = 0;            // 0: no note, !=0: note
  static int lastATval = 0;
  static int lastAVG = 0;
  static uint8_t AVGcnt = 0;

  // Check pad(s) every 10ms
  if (millis() - atic >= SAMPLEINTERVAL) {
    // Output stage not yet reached?
    if (ticks < SAMPLES) {
      // Yes, regular sampling cycle

      // Define variables to calculate slope
      int sumv = 0;    // sum of values
      int sumv2 = 0;   // sum of squared values
      int sumxv = 0;   // sum of x * value
      int avgv = 0;    // average of values

      // Loop over slope calculation points (1..DAMPER)
      for (uint8_t i = 0; i < DAMPER - 1; ++i) {
        // Shift old values one to the left
        d[i].v = d[i+1].v;
        d[i].v2 = d[i+1].v2;
        d[i].vx = d[i].v * i;
        // Calculate sums
        sumv += d[i].v;
        sumv2 += d[i].v2;
        sumxv += d[i].vx;
      }

      // Get a fresh value
      register int val = adc.analogRead(ANALOGPIN) >> SHIFTVAL;

      // put it into last slope calculation slot
      d[DAMPER - 1].v = val;
      d[DAMPER - 1].v2 = val * val;
      d[DAMPER - 1].vx = val * (DAMPER - 1);
      // Update sums
      sumv += val;
      sumv2 += val * val;
      sumxv += val * (DAMPER - 1);

      avgv = sumv / DAMPER;

      // Calculate slope
      slope[ticks] = (DAMPER * float(sumxv) - float(sumx) * float(sumv)) / float(denom);

      int avgDiff = avgv - lastAVG;
      if (abs(avgDiff) > AVGLIM) {
        AVGcnt++;
        if (AVGcnt > 3) {
          if (avgDiff < 0) {
            noteval = 0;
          } else {
            noteval = val;
          }
          AVGcnt = 0;
        }
      } else {
        AVGcnt = 0;
      }
      if (noteval) {
        noteval = val > 0 ? val : noteval;
      }

      v[ticks] = val;
      n[ticks] = noteval;
      p[ticks] = avgv;
      if (noteval && val != lastATval) {
        lastATval = val > 127 ? 127 : val;
      }
      lastAVG = avgv;

      Serial.printf("%4d;%5.3f;%4d;%4d\n", v[ticks], slope[ticks], p[ticks], n[ticks]);
      ticks++;
    } else {
      for (uint16_t i = 0; i < SAMPLES; ++i) {
        // Serial.printf("%4d;%5.3f;%4d;%4d\n", v[i], slope[i], p[i], n[i]);
      }
      // Serial.println("-----");
      ticks = 0;
    }
    atic = millis();
  }
}
 
Change X to the number of the ADC you are using. E.g.
Code:
adc.adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::LOW_SPEED); // change the sampling speed

Pete
 
Ah, so I will have to set both ADCs separately? I was under the impression that there are calls affecting both and separate for each. There is no common call to set both in one, I suppose?
 
I have this same problem, but just changing:

Code:
adc->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED, ADC_0);

with

Code:
adc.adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED, ADC_0);

doesn't solve the problem - now I get this error

Code:
IRBeaconHomingModule.ino: 209:5: error: request for member 'adc0' in 'adc', which is of pointer type 'ADC*' (maybe you meant to use '->' ?)

do I need to change the declaration of 'adc' from

Code:
ADC *adc = new ADC(); // adc object;

to something else?

TIA,

Frank
 
Never mind - I figured it out:

This:

Code:
	//ADC_0
	//adc->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED, ADC_0);
	//adc->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED, ADC_0);
	//adc->setResolution(12, ADC_0);
	//adc->setAveraging(1, ADC_0);


Changes to this:

Code:
adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED);
	adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED);
	adc->adc0->setResolution(12);
	adc->adc0->setAveraging(1);


Note that not only does 'adc->SetConversionSpeed..' change to 'adc->adc0->setConversionSpeed', but the 'ADC_0' or 'ADC_1' parameter gets dropped from the calling signature as well.

Frank
 
Back
Top