Zeroing a linear slope formula

Hello all,

I'm trying to zero a string pot to my arduino, it is a 0-5v sensor (power, signal, ground). I have a slope and an offset for the calibration of the sensor and this works well to display the sensor value in inches.

On the project, due the play in the system, the zero needs to be adjusted often. Essentially changing the offset value each time. I would like to do this using a toggle switch, whenever the user clicks the button, the Arduino would adjust the slope to read 0 inches at the time it is pressed.

How would I go about doing this? Any help is much appreciated. Attached is the code.

Thanks in advance

Linear_Potentiometer_Calibration.txt (490 Bytes)

When the button is pressed, update the offset by subtracting the current reading from the current offset.

Code posted properly, using code tags:

const float ReferenceVoltage = 5.0;
const float LinearPotPin = 0;
int delaytime = 1000;
float dist;
float slope = 0.4209;
float offset = -0.3870;


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

}

void loop() {

int val = analogRead(LinearPotPin);
float volts = (val/1023.0)*ReferenceVoltage;

dist = (volts*slope)+offset;


Serial.print("volts = ");
Serial.println(volts);

Serial.print("dist = ");
Serial.println(dist);


Serial.println("   ");

delay(delaytime);

}