Force Sensitive Resistor (FSR) Stopping a Timer

Hello,

I am trying to set up a force sensitive resistor (FSR) so that when a weight falls on it the elapsed/end time will be displayed on a LCD. For example, if the sketch is running for 1000 seconds and then the FSR is activated, "1000" will be displayed on the LCD.

First Attempts
The code below successfully reads the FSR and starts the timer. However, if the weight remains on the pad the elapsed/end time will continue to run. This is my first Arduino project so any advice would be greatly appreciated.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int sensorPin = A0; //pin A0 to read FSR input

long previousMillis = 0; // variable to store last time FSR was activated
long elapsedTime; // elapsed time
int value; //save force reading value

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

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("End Time: ");
  lcd.setCursor(0,1);
  lcd.print("F: "); //FSR read out 

}

void loop() {
  
  value = analogRead(sensorPin);
  Serial.println(value);
  lcd.setCursor(3,1);
  lcd.print(value);
  delay(1000);

  lcd.setCursor(7, 1);
  lcd.print(millis() / 1000); // Print run time 


  // If the weight hits the FSR, print the elapsed/end time on the LCD
  if(value > 100){
    
    elapsedTime = (millis() / 1000) - previousMillis;
    
    lcd.setCursor(10,0);
    lcd.print(elapsedTime) ;
    
  }
  
}

You never assign any value to startTime.

Jobi-Wan:
You never assign any value to startTime.

I edited the code and removed startTime. I was playing around with the code earlier and forgot to remove it. Sorry

Same as before, but this time you never assign a value to previousMillis, other than initializing it to zero.

At the point in your program where you want to 'reset' the timer, you should make previousMillis equal to millis(), so that on your next measurement, it measures the time since this moment.

Untested but the code below should work.

When the weight is removed this line will execute "if (elapsedTime > 0) previousMillis = millis();" to record the time at which the weight was removed so we later use this to determine the time that has passed. When the weight falls this line "if (elapsedTime == 0) elapsedTime = (millis() - previousMillis) / 1000;" will execute because "elapsedTime == 0" while there is no weight (so the first loop where weight value > 100 this will be true). So the second the weight is added it grabs the elapsed time and stops updating it (because now elapsedTime > 0). In the equation "(millis() - previousMillis)" previousMillis is the time at which the previous weight was removed so we can find the time that has passed since the old weight was removed and the new one was added.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int sensorPin = A0; //pin A0 to read FSR input

long previousMillis = 0; // variable to store last time FSR was activated
long elapsedTime = 0; // elapsed time
int value; //save force reading value

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

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // Print a message to the LCD.
  lcd.setCursor(0,0);
  lcd.print("End Time: ");
  lcd.setCursor(0,1);
  lcd.print("F: "); //FSR read out 

}

void loop() {
  
  value = analogRead(sensorPin);
  Serial.println(value);
  lcd.setCursor(3,1);
  lcd.print(value);
  delay(1000);

  lcd.setCursor(7, 1);
  lcd.print(millis() / 1000); // Print run time 


  // If the weight hits the FSR, print the elapsed/end time on the LCD
  if(value > 100){
    if (elapsedTime == 0) elapsedTime = (millis() - previousMillis) / 1000;

    lcd.setCursor(10,0);
    lcd.print(elapsedTime) ;
    
  }
  else
  {
    if (elapsedTime > 0) previousMillis = millis();
    elapsedTime = 0;
  }

  
}