Question for DAC adjust in dependence of analog input

Hello people!
with an adjustment to DAC (MCP4921) I have the following problem and hope for your help. sorry for my bad english!!!

The output is applied from a trans-impedance amplifier to a input of a differential amplifier. The second input of differential amplifier is a variable voltage → by the DAC to my signal (see picture) is approximately equal to hold a minimum level because my generated signal varies physiologically.

I have to continually read and evaluate my minimum & maximum values from my signal graph.
As for my minimum to the DAC up or down depending on the fluctuations regulate equal to the minimum (eg, 0-10mV) to hold. (Sample and Hold)
If my DAC dat first time is set, it will keep the value at fluctuations it should increase from the previous value / minimize and not start again from zero.( because zu long)
Unfortunately, only inkremtiert to maimalen DAC value (4096) and not my desired minimum, regulate not go down
Someone an idea? Thank you very much in advance

my code:

#include <AH_MCP4921.h>
#include <SPI.h>

int sensorValue = 0; // value
const int sensorPin = A0;
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int sensorM = 0; // minimum sensor value
int sensorMx = 0; // maximum sensor value

int i = 0; // zum DAC einstellen
AH_MCP4921 AnalogOutput1(51,52,53);// für SPI
void setup()
{
Serial.begin(9600);
AnalogOutput1.setValue(0); //DAC first zero
while ( sensorValue>=0) { // read value
sensorValue = analogRead(sensorPin);
if (sensorValue < sensorMin) {
sensorM = sensorValue; //find min value
}
if (sensorValue > sensorMax){ // find maximum value
sensorMx = sensorValue;
if(sensorValue>0) {
Serial.print("low = ");
Serial.println(sensorM); }

if(sensorMx>0) {
Serial.print("high= ");
Serial.println(sensorMx); }
}
if (sensorM<10)
{i++; // DAC inkrement
AnalogOutput1.setValue(i);
}// DAC hold
if (sensorM<10){
i–; //DAC from prevoius go down
AnalogOutput1.setValue(i); //DAC hold
}

}
}

void loop()
{}

In your code:

if (sensorM<10)
          {i++;      // DAC inkrement
           AnalogOutput1.setValue(i);
          }// DAC hold
            if (sensorM<10){
             i--; //DAC from prevoius go down
            AnalogOutput1.setValue(i); //DAC hold

you have “if (sensorM<10)” twice. Shouldn’t one of them be changed to “if (sensorMx>10)” ?

It might be simpler to use an analog circuit to adjust the threshold, then you wouldn’t need a DAC. You could use an op amp configured as an integrator to generate the variable voltage, so that it maintains the average output of the amplifier at the desired level.

dc42:
It might be simpler to use an analog circuit to adjust the threshold, then you wouldn't need a DAC. You could use an op amp configured as an integrator to generate the variable voltage, so that it maintains the average output of the amplifier at the desired level.

Do you mean integrator or comparator like Schmitt-Trigger?

I mean an integrator, with its non-inverting input connected to a voltage divider that supplies the average voltage you want at the amplifier output, so that it compares the amplifier output with the average and integrates the difference.

if I understand correctly

a Integrator instead difference amplifier?