ADC121 discontinuities

Hi,

I'm using the ADC121 analog input Grove module and using the AnalogIn example. I've noticed that a change in the analog input is several seconds late in being collected by getData(). Can anyone tell me how to get the most current samples available? Also, I need to change the gain/range value. Is there a good reference available on how to control the ADC121 (register functions, etc)?

Thanks! Ted

can you post the code you used + sample of its output?

That's the I2C ADC v1.0 (INT00100P)

#include <Wire.h>
#include <Streaming.h>

#define ADDR_ADC121 0x55 //Analog2DigitalModule
#define V_REF 3.00

#define REG_ADDR_RESULT 0x00
#define REG_ADDR_ALERT 0x01
#define REG_ADDR_CONFIG 0x02
#define REG_ADDR_LIMITL 0x03
#define REG_ADDR_LIMITH 0x04
#define REG_ADDR_HYST 0x05
#define REG_ADDR_CONVL 0x06
#define REG_ADDR_CONVH 0x07

unsigned int getData;

void init_adc()
{
Wire.beginTransmission(ADDR_ADC121); // transmit to device
Wire.write(REG_ADDR_CONFIG); // Configuration Register
Wire.write(0x02); //0x20 //0x10 works
Wire.endTransmission();
}

void read_adc() //unsigned int *data
{
Wire.beginTransmission(ADDR_ADC121); // transmit to device
Wire.write(REG_ADDR_RESULT); // get reuslt
Wire.endTransmission();

Wire.requestFrom(ADDR_ADC121, 2); // request 2byte from device
if(Wire.available()<=2)
{
getData = (Wire.read()&0x0f)<<8;
getData |= Wire.read();
}
//delay(1);
Serial.println(getData); //V_REF2/4096);
}

void setup()
{
Serial.begin(57600);
Wire.begin();
init_adc();
}

void loop()
{
read_adc();//adcRead);
}

I'm also seeing discontinuities in the input. Wondering why that is happening too.

At current gain/range setting (I changed the write following the write to REG_ADDR_CONFIG from the original 0x20 to 0x02), and after connecting the input of the ADC to +5V, all samples are 4095, except for single-sample pulses every 661st or 662nd sample which have a value of 0. It happens repetitively and occasionally doesn't happen at all (4095 instead). I'm curious why and how best to deal with it.

Thanks,
Ted

    if(Wire.available()<=2)
    {
      getData = (Wire.read()&0x0f)<<8;
      getData |= Wire.read();
    }

should be

    if (Wire.available() >= 2)  // <<<<<<<<<<<< HERE WAS THE BUG
    {
      getData = (Wire.read() & 0x0F) << 8;
      getData |= Wire.read();
    }

in your code if only one byte is in it will corrupt getData.

the use of spaces around operators helps to find these kind of bugs more easily. FOr humans it is more readable, compilers just skips the spaces.