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);
}
}
}