String Pot LCD Readout with Touch off feature

Greetings!

I hitting a bit of a snag in my code that I believe should be pretty straight forward, but haven't been able to iron it out. My overall objective is to take the analog readout of a string potentiometer and display it on an LCD screen with the capability of 'zeroing out' or 'touching off' the value by pressing a button and have that displayed as a separate line displaying the distance traveled by the string potentiometer since the button was clicked. i.e. If the string pot traveled 2" and the button is pressed and then the string pot travels another 2" the readout will display 'Overall Travel:4" Touch Off Value: 2" '

Currently all of the issue comes from the code for pressing the button. I am not able to zero out the value when the button is pressed. I have checked wiring and can get values from the 'Touch off' line though. I am sure it is something fundamental that I am overlooking in my code, so I have attached the code in hope that I could be pointed in the right direction. Thanks!

/*  
  String Pot Readout
  Takes in analog inputs on pin0 and takes results to the serial monitor.
  Attach the center pin of the potentiometer to pin A0, and the outside pins to +5V and to ground.
  */

  
//Constants
const int buttonPin = 9;     
const int ledPin =  3;     

//Variables
int buttonState = 0;
int flag=0;

#include <LiquidCrystal.h> //  LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int potPin = A0; //Potentiometer input pin
int potValue1 = 0;
int currentValue = 0;  // global variables are retained on each iteration of loop()
int previousValue = 0;
int analogPin=0;
int potValue2 = 0; // final display variable
int potValue3 = 0;
int offset = 0;
void setup() {
  //Input or output?
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT_PULLUP); 
lcd.begin(16, 2); // lcd rows and columns
lcd.print("Travel"); // title of sorts
}
void loop() {
// read then divide the input(max 1020 in this case) by 25.4
potValue1 = analogRead(potPin);
float inches = (potValue1/26.4);
// put value into inches
lcd.setCursor(7, 0);
//display final percentage
lcd.print(inches,3);
//print the percent symbol at the end
lcd.print("In");
//wait 0.1 seconds
delay(100);
 //Read button state (pressed or not pressed?)
  float potValue2 = currentValue;
  buttonState = digitalRead(buttonPin);
 currentValue = analogRead(analogPin);  // get a new reading
  //If button pressed..
if(digitalRead(buttonPin)== HIGH)
   buttonState==1;
 {potValue3= potValue2 - potValue1;
 offset= potValue3/26.4;
  digitalRead(potValue1); 
 lcd.setCursor(0, 1);
   lcd.print("Touch off"); // title of sorts
   lcd.print(offset, 3); 

//if touch off is pressed start value at 0
  if(digitalRead(buttonPin)== LOW)
  buttonState==0;
    digitalWrite(buttonPin, LOW);
    lcd.setCursor(0, 1);
    lcd.print("Touch off"); // title
    lcd.print(offset, 3);
}
//wipe the extra characters
lcd.print(" ");
delay(1);
}
 //Read button state (pressed or not pressed?)
  float potValue2 = currentValue;

How does that code relate to that comment?

  buttonState = digitalRead(buttonPin);
 currentValue = analogRead(analogPin);  // get a new reading
  //If button pressed..
if(digitalRead(buttonPin)== HIGH)

Why do you need to read the pin twice?

With INPUT_PULLUP, LOW means pressed, and HIGH means released.

PaulS:
How does that code relate to that comment?

Hi Paul, my apologies, that comment was one line out of place.

PaulS:

  buttonState = digitalRead(buttonPin);

currentValue = analogRead(analogPin);  // get a new reading
  //If button pressed..
if(digitalRead(buttonPin)== HIGH)



Why do you need to read the pin twice?

With INPUT_PULLUP, LOW means pressed, and HIGH means released.

Ah yes, that does seem to be redundant as well. Thanks for pointing that out.

Why do you use "potPin" (A0) in one place and "analogPin" (0) in another?

The statements "buttonState == 1;" and "buttonState == 0;" don't do anything. Perhaps you meant "buttonState = 1;" and "buttonState = 0;"?

  //Read button state (pressed or not pressed?)
  float potValue2 = CurrentPotValue;

Why are you declaring a local float variable with the same name as a global int variable?!?

  //If button pressed..
  if (digitalRead(buttonPin) == HIGH)
    buttonState == 1;


  {
    potValue3 = potValue2 - potValue1;

Was that block of code supposed to be part of the 'if' statement? It is unusual to start a new block for no reason.

  // read then divide the input (max 1020 in this case) by 25.4
  potValue1 = analogRead(potPin);
  float inches = (potValue1 / 26.4);

Note: 26.4 is not 25.4. The string pot reads directly in mm?

You should get rid of delays and only read inputs once at the top of loop(). The rest of the code should not need to re-read the inputs.

/*
  String Pot Readout
  Takes in analog inputs on pin0 and takes results to the serial monitor.
  Attach the center pin of the potentiometer to pin A0, and the outside pins to +5V and to ground.
*/


//Constants
const byte ButtonPin = 9;
const byte LEDPin =  3;


//Variables


boolean ButtonAlreadyPressed = false;
int PreviousPotValue = 0;
int TouchOffPoint = 0;


#include <LiquidCrystal.h> //  LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte potPin = A0; //Potentiometer input pin


void setup()
{
  //Input or output?
  pinMode(LEDPin, OUTPUT);
  pinMode(ButtonPin, INPUT_PULLUP);


  lcd.begin(16, 2); // lcd rows and columns
  lcd.print("Travel"); // title of sorts
}


void loop()
{
  int currentPotValue = analogRead(potPin);
  boolean buttonPressed = digitalRead(ButtonPin) == LOW;  // Active LOW


  // Display only when changed, to limit flicker
  if (currentPotValue != PreviousPotValue)
  {
    PreviousPotValue = currentPotValue;
    // Display current value in inches
    lcd.setCursor(7, 0);
    lcd.print(currentPotValue / 26.4, 3);
    lcd.print(" In  ");
  }


  //If button just pressed..
  if (buttonPressed && !ButtonAlreadyPressed)
  {
    TouchOffPoint = currentPotValue;


    // Display Touch off value in inches

    lcd.setCursor(0, 1);
    lcd.print("Touch off "); // title of sorts
    lcd.print(TouchOffPoint / 26.4, 3);
    lcd.print(" ");  // Cover any old characters
  }


  ButtonAlreadyPressed = buttonPressed;
}