Output capacitance data to digital out pin

Having trouble integrating programs. One measures capacitance and outputs the manipulated value on serial monitor. The other uses analog input data to trigger DIO. I am trying to use the output data to trigger a digital output pin, but having trouble understanding how the variables are being manipulated in the first part:
Preformatted text

#include <Wire.h>
#include <Protocentral_FDC1004.h>

#define UPPER_BOUND  0X4000                 // max readout capacitance
#define LOWER_BOUND  (-1 * UPPER_BOUND)
#define CHANNEL 0                          // channel to be read
#define MEASURMENT 0                       // measurment channel

int capdac = 0;
char result[100];

FDC1004 FDC;

void setup()
{
  Wire.begin();        //i2c begin
  Serial.begin(115200); // serial baud rate
}

void loop()
{

  FDC.configureMeasurementSingle(MEASURMENT, CHANNEL, capdac);
  FDC.triggerSingleMeasurement(MEASURMENT, FDC1004_100HZ);

  //wait for completion
  delay(15);
 uint16_t value[2];

 Serial.println(uint16_t value[2]);
   if (! FDC.readMeasurement(MEASURMENT, value))
  {
    int16_t msb = (int16_t) value[0];
    int32_t capacitance = ((int32_t)457) * ((int32_t)msb); //in attofarads
    capacitance /= 1000;   //in femtofarads
    capacitance += ((int32_t)3028) * ((int32_t)capdac);

    Serial.print((((float)capacitance/1000)),4);
    Serial.print("  pf, ");

    if (msb > UPPER_BOUND)               // adjust capdac accordingly
  {
      if (capdac < FDC1004_CAPDAC_MAX)
    capdac++;
    }
  else if (msb < LOWER_BOUND)
  {
      if (capdac > 0)
    capdac--;
    }

  }
}```

Serial.print() the variables before and after the manipulation and see if that helps.

test the computation without doing the I/O

consider

compute: msb 1 0.0000  pf,capdac 0
compute: msb 2 0.0000  pf,capdac 0
compute: msb 4 0.0010  pf,capdac 0
compute: msb 12 0.0030  pf,capdac 0
compute: msb 24 0.0070  pf,capdac 0
compute: msb 52 0.0140  pf,capdac 0
compute: msb 144 0.0290  pf,capdac 0
compute: msb 332 0.0580  pf,capdac 0
compute: msb 1104 0.1160  pf,capdac 0
compute: msb 2212 0.2330  pf,capdac 0
compute: msb 4424 0.4670  pf,capdac 0
compute: msb 13252 0.9350  pf,capdac 0
compute: msb 30544 1.8710  pf,capdac 0
compute: msb 101532 3.7430  pf,capdac 0
#define UPPER_BOUND  0X4000                 // max readout capacitance
#define LOWER_BOUND  (-1 * UPPER_BOUND)

int capdac = 0;

char s [80];

// -----------------------------------------------------------------------------
void
compute (
    int16_t msb )
{
    Serial.print   ("compute: msb ");
    Serial.print   (msb, 6);
    Serial.print   (" ");

    int32_t capacitance = ((int32_t)457) * ((int32_t)msb); //in attofarads
    capacitance /= 1000;   //in femtofarads
    capacitance += ((int32_t)3028) * ((int32_t)capdac);

    Serial.print ((float)capacitance/1000, 4);
    Serial.print("  pf,");

    if (msb > UPPER_BOUND)               // adjust capdac accordingly
    {
        if (capdac < UPPER_BOUND)
            capdac++;
    }
    else if (msb < LOWER_BOUND)
    {
        if (capdac > 0)
            capdac--;
    }

    Serial.print   ("capdac ");
    Serial.println (capdac);
}
    

void setup()
{
    Serial.begin(115200); // serial baud rate

    for (int16_t val = 1; val <= 0x2000; val *= 2)  {
        compute (val);
    }
}

void loop()
{
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.