Need code advice on summing diffferences of values

I'm looking to take two previous readings from a sensor. And if the current reading is greater than the previous. I need to find the difference and then record it under a cumulative sum. So basically only when the sensor increases do I record the value.

I am currently using manufacturing code from a dissolved oxygen sensor for a pond project.

tried using the sum += analogRead(); but it just listed the values in the horizontal format rather than summing them together.

Kinda a novice with this kind of thing. Any help would be greatly appreciated.

#include <Arduino.h>

#define DO_PIN A0

#define VREF 5000    //VREF (mv)
#define ADC_RES 1024 //ADC Resolution

//Single-point calibration Mode=0
//Two-point calibration Mode=1
#define TWO_POINT_CALIBRATION 0


#define READ_TEMP (22)
 //Current water temperature ℃, Or temperature sensor function

//Single point calibration needs to be filled CAL1_V and CAL1_T
#define CAL1_V (1987) //mv
#define CAL1_T (22)   //℃
//Two-point calibration needs to be filled CAL2_V and CAL2_T
//CAL1 High temperature point, CAL2 Low temperature point
#define CAL2_V (1300) //mv
#define CAL2_T (15)   //℃

const uint16_t DO_Table[41] = {
    14460, 14220, 13820, 13440, 13090, 12740, 12420, 12110, 11810, 11530,
    11260, 11010, 10770, 10530, 10300, 10080, 9860, 9660, 9460, 9270,
    9080, 8900, 8730, 8570, 8410, 8250, 8110, 7960, 7820, 7690,
    7560, 7430, 7300, 7180, 7070, 6950, 6840, 6730, 6630, 6530, 6410};

uint8_t Temperaturet;
uint16_t ADC_Raw;
uint16_t ADC_Voltage;
uint16_t DO;

int16_t readDO(uint32_t voltage_mv, uint8_t temperature_c)
{
#if TWO_POINT_CALIBRATION == 0
  uint16_t V_saturation = (uint32_t)CAL1_V + (uint32_t)35 * temperature_c - (uint32_t)CAL1_T * 35;
  return (voltage_mv * DO_Table[temperature_c] / V_saturation);
#else
  uint16_t V_saturation = (int16_t)((int8_t)temperature_c - CAL2_T) * ((uint16_t)CAL1_V - CAL2_V) / ((uint8_t)CAL1_T - CAL2_T) + CAL2_V;
  return (voltage_mv * DO_Table[temperature_c] / V_saturation);
#endif
}
int sensorPin = A0;
int sensorValue = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  
  
  Temperaturet = (uint8_t)READ_TEMP;
  ADC_Raw = analogRead(DO_PIN);
  ADC_Voltage = uint32_t(VREF) * ADC_Raw / ADC_RES;

  
  Serial.print("\t");
  Serial.print("\t");
  Serial.print("\t");
  Serial.print("\t");
  Serial.println(String(readDO(ADC_Voltage, Temperaturet)) + "\t");

  delay(1000);
}

Welcome to the forums! Great job on using code tags to post your code. It looks like stock code that just takes a reading.

What have you tried to accomplish your goal? People are willing to help you out, not write code for you. Post your best effort at reading, storing and comparing the current reading to a previous reading.

int storethingyprevious1 =0;
int storecumlicatedreadings=0;
boolean populatedthingie = false;
voiding setup()
{

int storecurrentreding =readsomesensorthingy;

//preload thingies with data
if !populatedthingie
{
storethingyprevious1= storecurrentreading
populatedthingie=true;
} else
{
if ( (storecurrentreding > storethingyprevious1)  )
{

 storecumlicatedreadings += storecurrentreding - storethingyprevious1;
storethingyprevious1 = storecurrentreading;
}


}
}

might give some clues or not.

1 Like

Ok, so i got the whole difference calculations. When the numbers increase I am able to pull the difference. The only issue I'm now having is that my sum function isn't actually summing the values its just kinda listing them.

So with


sum += analogread();

// The numbers are 3 digits for your clarification
// 303504602
// Shouldn't it just be summing to get 1409 instead of doing this

How does analogread work?
https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/ <<<Reads the value from the specified analog pin. which analog pin does analogread() specify? How is a pin specified with analog read? See the link for more.

What type of variable is "sum"?

String? That might explain what you see.

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