FSR controlled timing for an LED

I’m trying to write a program to time how long a Force Sensing Resistor is pressed then turns on a LED for the same amount of time. I have tested the circuit and found the FSR returns a value of 200 when lightly pressed. In my program I attempted to assign the input from the FSR under 200 to be a LOW value and over 200 as a HIGH value. It doesn’t work so hot. I would appreciate any help in trouble shooting my code.

const int buttonPin = A0;  

int buttonState = 0;     // current state of the button
int lastButtonState = 0; // previous state of the button
int startPressed = 0;    // the moment the button was pressed
int endPressed = 0;      // the moment the button was released
int holdTime = 0;        // how long the button was hold
int idleTime = 0;        // how long the button was idle
int timeActive = 0;       // how long the function will run
void setup() {
  pinMode(buttonPin, INPUT); // initialize the button pin as a input
  pinMode(13, OUTPUT);        // initialize pin 13 as output

}

void loop() {
  buttonState = analogRead(buttonPin); // read the button input
  
  if (buttonState < 199) {
    buttonState == LOW;
    }
  if (buttonState > 200) {
    buttonState == HIGH;
    }

  if (buttonState != lastButtonState) { // button state changed
     updateState();
  }

  lastButtonState = buttonState;        // save state for next loop
}

void updateState() {
  // the button has been just pressed
  if (buttonState == HIGH) {
      startPressed = millis();
      idleTime = startPressed - endPressed;

  }    

  // the button has been just released
   else {
      endPressed = millis();
      holdTime = endPressed - startPressed;

  timeActive = (holdTime);
      
     digitalWrite (13, HIGH);
      delay (holdTime);
     digitalWrite (13, LOW);
      delay (1000);
   }   
   }

So, if buttonState == 200 or buttonState == 199, do nothing at all?

if (buttonState < 199) {
    buttonState == LOW;
  }
  if (buttonState > 200) {
    buttonState == HIGH;
  }

You also treat buttonState alternatively as a logic or a numerical value:

 if (buttonState > 200) {
  if (buttonState == HIGH) {

I see that you convert the numerical value to a logic value, but this is terribly unconventional and confusing. You should capture the analog value with a separate, appropriately named variable like ‘buttonValue’.

 buttonState = analogRead(buttonPin);

  if (buttonState < 199) {
    //buttonState == LOW;
    buttonState = LOW;
    }
  if (buttonState > 200) {
    //buttonState == HIGH;
    buttonState = HIGH;
    }

You should be using = (assignment) in this block not == (comparison).

Awesome. Thank you for your time and help. I added a buttonValue int to capture the data and changed the comparisons to <= and >=. The program works great and I really appreciate your help.

I noticed I was having some problems with artifacts and I needed to debounce my switch. So I followed the debouncing tutorial but now the light doesn’t come on at all. Any ideas as to why? Do I need to add an int for the Millis?

const int buttonPin = A0;  

int buttonState = HIGH;     // current state of the button
int buttonValue = 0;     // capture the analog data from the sensor
int lastButtonState=LOW; // previous state of the button
int startPressed = 0;    // the moment the button was pressed
int endPressed = 0;      // the moment the button was released
int holdTime = 0;        // how long the button was hold
int idleTime = 0;        // how long the button was idle
int timeCalc = 0;       // Calculation from hold time
int timeActive = 0;     // how long the function will activate
int reading = HIGH;
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 200;    // the debounce time; increase if the output flickers


void setup() {
  pinMode(buttonPin, INPUT); // initialize the button pin as a input
  pinMode(13, OUTPUT);        // initialize pin 13 as output

}

void loop() {
   buttonValue = analogRead(buttonPin); // read the button input
   
  
  if (buttonValue <= 199) {       
    reading = LOW;
    }
  if (buttonValue >= 200) {         // resitance over 200 will act like having pushed a button
    reading = HIGH;
    }
    
      // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) { //debounce delay
    if (reading !=buttonState) {
      buttonState = reading;
    

    if (buttonState != lastButtonState) { // button state changed
     updateState();
       }
    }
  }
  lastButtonState = reading;        // save state for next loop
}

void updateState() {
  // the button has been just pressed
  if (buttonState == HIGH) {
      startPressed = millis();
      idleTime = startPressed - endPressed;

  }    

  // the button has been just released
   else {
      endPressed = millis();
      holdTime = ((endPressed - startPressed)/1000.00);
      
 timeCalc = (holdTime);

  
  
    timeActive =  (timeCalc);                
      
     digitalWrite (13, HIGH);
      delay (timeActive);
     digitalWrite (13, LOW);
      
   }   
   }

I noticed I was having some problems with artifacts and I needed to debounce my switch.

You don’t have a digital switch you have an FSR and I think that having an appropriate hysteresis band on the analog readings is going to be a more effective approach than using debounce techniques. Does this eliminate the “artifacts”?

const int fsrPin = A0;
int fsrValue;
int fsrState = 0;     // current state of the fsr
int lastfsrState = 0; // previous state of the fsr
int startPressed = 0;    // the moment the fsr was pressed
int endPressed = 0;      // the moment the fsr was released
int holdTime = 0;        // how long the fsr was hold
int idleTime = 0;        // how long the fsr was idle
int timeActive = 0;       // how long the function will run
void setup() {
  Serial.begin(115200);
  pinMode(fsrPin, INPUT); // initialize the fsr pin as a input
  pinMode(13, OUTPUT);        // initialize pin 13 as output

}

void loop() {
  fsrValue = analogRead(fsrPin); // read the fsr input

  if (fsrValue < 100) {
    fsrState = LOW;
  }
  if (fsrValue > 300) {
    fsrState = HIGH;
  }

  if (fsrState != lastfsrState) { // fsr state changed
    Serial.print("StateChange ");
    if (fsrState == LOW)
      Serial.println("LOW");
    else
      Serial.println("HIGH");
    updateState();
  }

  lastfsrState = fsrState;        // save state for next loop
}

void updateState() {
  // the fsr has been just pressed
  if (fsrState == HIGH) {
    startPressed = millis();
    idleTime = startPressed - endPressed;

  }

  // the fsr has been just released
  else {
    endPressed = millis();
    holdTime = endPressed - startPressed;

    timeActive = (holdTime);

    digitalWrite (13, HIGH);
    delay (holdTime);
    digitalWrite (13, LOW);
    delay (1000);
  }
}