Arduino ADC locking up with SmoothADC Library

Hi
thanks for taking time to read my post!
I'm trying to read multiple analog pins on Teensy3.2. All works fine for 30-45sec and then the values outputted to the serial monitor lockup and stay the same no matter if the input varies. Reboot brings them back alive for awhile and they lockup again.
I tried adding delays here and there, etc but I was not successful to resolve the issue. I was wondering if the issue is in may code or in the library
here is my code:

#include <Arduino.h>
#include <SmoothADC.h>


#define ADC_RESOLUTION 10


int bitRes =  pow(2,ADC_RESOLUTION);

//****** We tie 3.3V to ARef and measure it with a multimeter!********

float ANALOG_REFERENCE = 3.33; 

//*******setup resitor dividers value for voltage anaog inputs**********
double v_r1 = 47000;  //resistor R1 on AX inputs
double v_r2 = 5610;   //resostor R2 on AX inputs

//*******setup resitor dividers value for temperature anaog inputs**********
double t_r1 = 471;  //resistor R1 on T_AX input
double t_r2 = 5600; //resistor R2 on T_AX input



//***********create objects for smoothADC**************
SmoothADC   ADC1;       // SmoothADC instance for S_A1
SmoothADC   ADC2;       // SmoothADC instance for S_A2
SmoothADC   ADC3;       // SmoothADC instance for S_A3
SmoothADC   ADC4;       // SmoothADC instance for S_A4
SmoothADC   ADC5;       // SmoothADC instance for S_A5
SmoothADC   ADC6;       // SmoothADC instance for S_A6
SmoothADC   ADC7;       // SmoothADC instance for S_A7
SmoothADC   ADC8;       // SmoothADC instance for S_A8

SmoothADC   ADC_T1;       // SmoothADC instance for T1
SmoothADC   ADC_T2;       // SmoothADC instance for T2
SmoothADC   ADC_T3;       // SmoothADC instance for T3
SmoothADC   ADC_T4;       // SmoothADC instance for T4



unsigned int  ADC1Value = 0;    // ADC1 final value
unsigned int  ADC2Value = 0;    // ADC2 final value
unsigned int  ADC3Value = 0;    // ADC3 final value
unsigned int  ADC4Value = 0;    // ADC4 final value
unsigned int  ADC5Value = 0;    // ADC5 final value
unsigned int  ADC6Value = 0;    // ADC6 final value
unsigned int  ADC7Value = 0;    // ADC7 final value
unsigned int  ADC8Value = 0;    // ADC8 final value

unsigned int  ADC_T1Value = 0;    // ADC1 final value
unsigned int  ADC_T2Value = 0;    // ADC2 final value
unsigned int  ADC_T3Value = 0;    // ADC3 final value
unsigned int  ADC_T4Value = 0;    // ADC4 final value

unsigned int  MemTimeSerial;









int ledState = LOW;
const int ledPin =  13;      // the number of the LED pin



void setup() {
  Serial.begin(115200);
  
  pinMode(ledPin, OUTPUT);


  //*********Setup the S_AXinputs to Teensy AX pins for measuring voltage*********
  ADC1.init(A6, 20);  // Init ADC1 attached to A6 with a XXms acquisition period
  if (ADC1.isDisabled())  {
    ADC1.enable();
  }
  ADC2.init(A7, 20);  // Init ADC2 attached to A7 with a XXms acquisition period
  if (ADC2.isDisabled())  {
    ADC2.enable();
  }
  ADC3.init(A4, 20);  // Init ADC3 attached to A4 with a 50ms acquisition period
  if (ADC3.isDisabled())  {
    ADC3.enable();
  }
  ADC4.init(A5, 20);  // Init ADC4 attached to A5 with a 20ms acquisition period
  if (ADC4.isDisabled())  {
    ADC4.enable();
  }
  ADC5.init(A2, 20);  // Init ADC5 attached to A2 with a 50ms acquisition period
  if (ADC5.isDisabled())  {
    ADC5.enable();
  }
  ADC6.init(A3, 20);  // Init ADC6 attached to A3 with a 20ms acquisition period
  if (ADC6.isDisabled())  {
    ADC6.enable();
  }
  ADC7.init(A0, 20);  // Init ADC7 attached to A0 with a 50ms acquisition period
  if (ADC7.isDisabled())  {
    ADC7.enable();
  }
  ADC8.init(A1, 20);  // Init ADC8 attached to A1 with a 20ms acquisition period
  if (ADC8.isDisabled())  {
    ADC8.enable();
  }
  //*********Setup the T_Xinputs to Teensy AX pins for measuring temperature
  ADC_T1.init(A9, 20);  // Init ADC_T1 attached to A9 with a 50ms acquisition period
  if (ADC_T1.isDisabled())  {
    ADC_T1.enable();
  }
  ADC_T2.init(A8, 20);  // Init ADC_T2 attached to A8 with a 50ms acquisition period
  if (ADC_T2.isDisabled())  {
    ADC_T2.enable();
  }
  ADC_T3.init(A10, 20);  // Init ADC_T3 attached to A10 with a 50ms acquisition period
  if (ADC_T3.isDisabled())  {
    ADC_T3.enable();
  }
  ADC_T4.init(A11, 20);  // Init AD_TC4 attached to A11 with a 50ms acquisition period
  if (ADC_T4.isDisabled())  {
    ADC_T4.enable();
  }
  //*****************************

}



void loop() {

  String      toSerial = "";
  analogReadResolution(ADC_RESOLUTION);
  unsigned int  tempTime = millis();


  ADC1.serviceADCPin();
  delay(2);
  ADC2.serviceADCPin();
  delay(2);
  ADC3.serviceADCPin();
  delay(2);
  ADC4.serviceADCPin();
  delay(2);
  ADC5.serviceADCPin();
  delay(2);
  ADC6.serviceADCPin();
  delay(2);
  ADC7.serviceADCPin();
  delay(2);
  ADC8.serviceADCPin();
  delay(2);
  ADC_T1.serviceADCPin();
  delay(2);
  ADC_T2.serviceADCPin();
  delay(2);
  ADC_T3.serviceADCPin();
  delay(2);
  ADC_T4.serviceADCPin();
  delay(2);


  if ((tempTime - MemTimeSerial) > 100)
  {
    MemTimeSerial = tempTime;

#ifdef DEBUG
    ADC1.dbgInfo();
    ADC2.dbgInfo();
    ADC3.dbgInfo();
    ADC4.dbgInfo();
    ADC5.dbgInfo();
    ADC6.dbgInfo();
    ADC7.dbgInfo();
    ADC8.dbgInfo();

    ADC_T1.dbgInfo();
    ADC_T2.dbgInfo();
    ADC_T3.dbgInfo();
    ADC_T4.dbgInfo();

#endif

    ADC1Value = ADC1.getADCVal();delay(2);
    ADC2Value = ADC2.getADCVal();delay(2);
    ADC3Value = ADC3.getADCVal();delay(2);
    ADC4Value = ADC4.getADCVal();delay(2);
    ADC5Value = ADC5.getADCVal();delay(2);
    ADC6Value = ADC6.getADCVal();delay(2);
    ADC7Value = ADC7.getADCVal();delay(2);
    ADC8Value = ADC8.getADCVal();delay(2);

    ADC_T1Value = ADC_T1.getADCVal();delay(2);
    ADC_T2Value = ADC_T2.getADCVal();delay(2);
    ADC_T3Value = ADC_T3.getADCVal();delay(2);
    ADC_T4Value = ADC_T4.getADCVal();delay(2);

    /*
      toSerial += "\t";
        //toSerial += calc_t(ADC_T4Value);
        toSerial += "\t";
        toSerial += calc_v(ADC1Value);
        toSerial += "\t";
        toSerial += calc_v(ADC2Value);
        toSerial += "\t";
        toSerial += calc_v(ADC3Value);
        toSerial += "\t";
        //toSerial += pow(2,ADC_RESOLUTION);

        Serial.print( calc_t(ADC_T4Value),2);
        Serial.println(toSerial);
    */
//Serial.print("\t  ");  
Serial.print(calc_t(ADC_T1Value));
Serial.print("\t  ");
Serial.print(calc_t(ADC_T3Value));
Serial.print("\t  ");
Serial.print(calc_t(ADC_T4Value));
Serial.print("\t  ");
Serial.println(calc_v(ADC3Value));
    blinkLED();
    delay(10);
  }
}

double calc_t (int pinreadValue){
 
  double dividerMultiplier = (t_r1 + t_r2) / t_r2 * ANALOG_REFERENCE / bitRes;
 double t_voltage =  dividerMultiplier * pinreadValue;
 double Temperature = (t_voltage - 0.5) * 100 ;
 return Temperature;
  }

double calc_v (int pinreadValue){
  int bitRes =  pow(2,ADC_RESOLUTION);
  double dividerMultiplier = (v_r1 + v_r2) / v_r2 * ANALOG_REFERENCE / bitRes;
 double voltage =  dividerMultiplier * pinreadValue;
 return voltage;
  }

  
void blinkLED() {

  if (ledState == LOW)
    ledState = HIGH;
  else
    ledState = LOW;

  digitalWrite(ledPin, ledState);
 
}

I would appreciate any help! The SmoothADC library can be found here:

Thank you!

int bitRes =  pow(2,ADC_RESOLUTION);

That was as far as I read. Do NOT use pow() to do bit shifting.

float ANALOG_REFERENCE = 3.33;

ANALOG_REFERENCE is a named constant. Use your own name.

Arrays would certainly be a good idea.

    ADC8Value = ADC8.getADCVal();delay(2);

Ifyoucan'tusetwolinesfortwostatements,atleastusesomespaces.

The SmoothADC library can be found here:

Have you tried it on ONE pin?

Hi,
I did resolve my issue today (actually it was one of the lead SW guys at work that I asked for help :)). I'm just simple product manager learning how to program...
We did establish first that the error is in the library by printing the raw value from the ADC and monitoring if it continue to change after the output locks. The raw value was still updating...
Next step for him was to see where is the issue in the library.

He did use some Serial.print("something"); @few suspected functions to figure out what function was failing and established that the spillover in library file SmoothADC.cpp "bool SmoothADC::TestAcqRate()" wasn't handled right - when it reaches 65536 and zeros out, it was failing. He wrote the following fix and now it works fine.

I will forward the fix to the autor of the library to implement.Thanks for your time writing reading my post and writing reply, and tanks to @Chris.MonkieWicz for the quick fix today. :slight_smile:

Did you manage to get your voltage jump problem solved?

I released a new version of the lib (fixed timing locking on 32b archs), it should be available soon through the arduino lib manager.
SmoothADC

Did you manage to fix the voltage jump you had?

I did use a different averaging. I ended up writing simple sapling array with interrupts. It works well and does not have an issue with the voltage jump. I will try your new version in the next few days.