unsigned long millis issue

I working on a project that measures the time a force sensing resistor has been pressed, and then turns on an LED with a ramp up and ramp down based on how long the force sensing resistor has been pressed. Currently the ramp up and down parts don’t seem to be working because I don’t know what I’m doing. The program will also only run for about 24 seconds and then stops working. Any help would be greatly appreciated.

Thank you.

const int buttonPin = A0;  

int pinOut =  13 ;     //   
int buttonState;         // current state of the button
int buttonValue = 0;     // capture the analog data from the sensor
int lastButtonState;     // 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;            // how long the button was held in milliSeconds
float hT = 0.00 ;        // converted button hold time to Seconds
float timeCalc = 0;      // Calculation from hold time
float a1 =3.8987, a2 = -15.473, a3 = 30.396, a4 = -29.517, a5 = 13.787, a6 = -2.4753, n1 = -.0066, 
      d1 = 2.7746, d2 = -.2296, r3 = 17.672, r2 = -49.544, r1 = 42.296, r0 = 9.5709, e1 = 6.1041;
float delayTime = 0;     // how long to delay for terminal stance
unsigned long currentMillis = millis();
float rampdownTime = 0;   // allows for the calculation of stimulation ramp down based on hT
float rdTp;
float rampupTime = 0;     // calculates time for increased stimulus gradient
float ruTp;         



void setup() {
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input
  pinMode(pinOut, OUTPUT);        // initialize pin 13 as output
  Serial.begin(9600);
}

void loop() {
   buttonValue = analogRead(buttonPin); // read the button input
   
  
  if (buttonValue <= 100) {         // resistance under 100 will act as an off switch.  Resistance gap acts as a functional debounce.
    buttonState = LOW;
    }
  if (buttonValue >= 300) {         // resitance over 300 will act like having pushed a button
    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();
   

  }    

  // the button has been just released
   else {
      endPressed = millis();
      holdTime = ((endPressed - startPressed));      // calculates the time the button is pressed in MilliSeconds
      hT = (holdTime/1000.00);                  // returns the time pressed in seconds instead of MilliSeconds
       if (hT > 2){                            // sets a maximum hold of 2 seconds
         hT = 2; 
       }
  
  timeCalc = ((a6* hT*hT*hT*hT*hT*hT) +       // Calculation for "ON" time
             (a5 * hT*hT*hT*hT*hT) + 
             (a4 * hT*hT*hT*hT) + 
             (a3 * hT*hT*hT) + 
             (a2 * hT*hT) + 
             (a1 * hT) + n1);  

  delayTime = (500);                   // Calculation for delay between when the button is no longer pushed and the "ON" time

  rampupTime = ((e1 *hT) - 0.2296) ;          // Calcualtion for the time used to incrementally increase the modulated "ON" time

  ruTp = (rampupTime / 9) ;                   // Segments into 9 sections to be able to perform rough PWM like On/Off cycle

  

  

  rampdownTime = ((r3 * hT*hT*hT) +           //  Calculates the ramp down of total current
                  (r2 * hT*hT*hT) +
                  (r1 * hT) +
                  (r0));

  rdTp = (rampdownTime / 36);              // devides the total rampdown time into 36 sections to use like a really really slow PWM

Serial.print(hT);
Serial.println();  
Serial.print(timeCalc);
Serial.println();
Serial.print(delayTime);
Serial.println();
Serial.print(rampupTime);
Serial.println();
Serial.print(rdTp);
Serial.println();


  if (endPressed + delayTime >= currentMillis){     // wait "delayTime" seconds before turning pin 13 on
      digitalWrite (pinOut, HIGH);
     } if (endPressed + delayTime + ruTp >= currentMillis){      // Makes the signal ramp up 
        digitalWrite (pinOut, LOW);
       } if (endPressed + delayTime + (3 * ruTp) >=currentMillis){
        digitalWrite (pinOut, HIGH);
        } if (endPressed + delayTime + ( 5 * ruTp) >=currentMillis){
          digitalWrite (pinOut, LOW);
         } if (endPressed + delayTime + ( 7 * ruTp) >= currentMillis){
           digitalWrite (pinOut, HIGH);
          }  
       if (endPressed + delayTime + timeCalc >=currentMillis) { // this section of code creates a reduction in duty cycle for the ramp down.  Intial cycle is 3/4 on/off ratio and runs for 1/3 of the rampdownTime
       digitalWrite (pinOut, LOW);
        } if (endPressed + delayTime + timeCalc + (rdTp) >=currentMillis) { 
          digitalWrite (pinOut, HIGH);  
          } if (endPressed + timeCalc + delayTime + ( 4 * rdTp) >=currentMillis) { 
            digitalWrite (pinOut, LOW);
            } if (endPressed + delayTime + timeCalc +  (5* rdTp) >=currentMillis) { 
             digitalWrite (pinOut, HIGH);
              } if (endPressed + timeCalc + delayTime + ( 8 * rdTp) >=currentMillis) { 
               digitalWrite (pinOut, LOW);
                } if (endPressed + delayTime + timeCalc +  (9* rdTp) >=currentMillis) { 
                 digitalWrite (pinOut, HIGH);
                   } if (endPressed + timeCalc + delayTime + ( 12 * rdTp) >=currentMillis) { 
                    digitalWrite (pinOut, LOW);
                     } if (endPressed + delayTime + timeCalc +  (14 * rdTp) >=currentMillis) { 
                      digitalWrite (pinOut, HIGH);
                       } if (endPressed + timeCalc + delayTime + ( 16 * rdTp) >=currentMillis) { 
                        digitalWrite (pinOut, LOW);
                         } if (endPressed + delayTime + timeCalc +  (18 * rdTp) >=currentMillis) { 
                          digitalWrite (pinOut, HIGH);
                            } if (endPressed + delayTime + timeCalc +  (20 * rdTp) >=currentMillis) { 
                             digitalWrite (pinOut, LOW);
                               } if (endPressed + delayTime + timeCalc +  (22 * rdTp) >=currentMillis) { 
                                digitalWrite (pinOut, HIGH);
                                  } if (endPressed + delayTime + timeCalc +  (24 * rdTp) >=currentMillis) { 
                                    digitalWrite (pinOut, LOW);
                                      } if (endPressed + delayTime + timeCalc +  (27 * rdTp) >=currentMillis) { 
                                        digitalWrite (pinOut, HIGH);
                                          } if (endPressed + delayTime + timeCalc +  (28 * rdTp) >=currentMillis) { 
                                            digitalWrite (pinOut, LOW);
                                              } if (endPressed + delayTime + timeCalc +  (31 * rdTp) >=currentMillis) { 
                                                digitalWrite (pinOut, HIGH);
                                                  } if (endPressed + delayTime + timeCalc +  (32 * rdTp) >=currentMillis) { 
                                                    digitalWrite (pinOut, LOW);
                                                      } if (endPressed + delayTime + timeCalc +  (35 * rdTp) >=currentMillis) { 
                                                        digitalWrite (pinOut, HIGH);
                                                        }
     
     if (endPressed + timeCalc + delayTime >=currentMillis) { // after timeCalc seconds turn pin 13 off
       digitalWrite (pinOut, LOW);
       }
       
    
   }   
   }

Please don’t write code that drifts like this

  if (endPressed + delayTime >= currentMillis){     // wait "delayTime" seconds before turning pin 13 on
      digitalWrite (pinOut, HIGH);
     } if (endPressed + delayTime + ruTp >= currentMillis){      // Makes the signal ramp up 
        digitalWrite (pinOut, LOW);
       } if (endPressed + delayTime + (3 * ruTp) >=currentMillis){

Instead, it will be much easier to read if you put the closing } under its IF and on a line of its own - like this

  if (endPressed + delayTime >= currentMillis){     // wait "delayTime" seconds before turning pin 13 on
      digitalWrite (pinOut, HIGH);
  } 
  if (endPressed + delayTime + ruTp >= currentMillis){      // Makes the signal ramp up 
        digitalWrite (pinOut, LOW);
  } 
  if (endPressed + delayTime + (3 * ruTp) >=currentMillis){

When working with millis() always use this style so that it continues to work when millis() rolls over to 0

if (currentMillis - endPressed >= (delayTime + ruTpP)) {

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

I don’t know if your program really needs all those IF statements but, for the purpose of getting advice here I suggest you shorten the program so there are only a few IF statements. It will make things much easier to figure out.

I wonder if the subsequent lines should be ELSE IF rather than simply IF

…R

Robin2:
I wonder if the subsequent lines should be ELSE IF rather than simply IF

I would think so.
Also check for the longer delays FIRST then drop through and check for the shorter delays NEXT in descending order.

If you check for the shortest delay first, that delay will always take precedence.

Also all time values need to be unsigned long...

int startPressed = 0;    // the moment the button was pressed
int endPressed = 0;      // the moment the button was released

There may be other errors.

Using floats is extremely ill advised and offers no greater accuracy when comparing millis() values. In fact it may even result in LOST accuracy or your code going completely AWOL...

float delayTime = 0;     // how long to delay for terminal stance
float rampdownTime = 0;   // allows for the calculation of stimulation ramp down based on hT
float rdTp;

Do you calculations in floats if necessary, cast to unsigned long variables for the result and use them for comparing with millis().

Hi Paul,

what do you mean by "ramp up" ?

Should the LED start glowing dark getting slowly brighter and brighter?

Can you please describe in normal words and in detail: what will a user of your device see
Please please please use normal words not any kind of code and not any kind of calculations
just simple normal words.

I'm sorry to write this You seem to have some misconceptions of how programs work. And that is the reason why I insist to describe it in normal words.

If somebody who has no idea about how programming works. How would such a person describe the behaviour of the LED?

best regards Stefan