Arduino code : calculating the time how long the FSR sensor is pressed?

I have a code for 6 pin FSR sensor .
from that I can read the pressure and it give the output in reading and LED . I combined 3pin in one red LED and 3 pin in another green LED .
Now I need to calculate the time how long the pressure sensor is pressed? I need to add the code for the calculation of time in my code.

Kindly help..........

const byte fsrPins[] = {0, 1, 2, 3, 4, 5, 6};
const int GREEN_PIN = 3;
const int RED_PIN = 2;
int fsrReading[6];
void setup(void)
{
  
  Serial.begin(9600);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
}
void loop(void)
{
  for (int sensor = 0; sensor < 6; sensor++)
  {
    Serial.print("Sensor ");
    Serial.println(sensor);       
    if(sensor < 3; analogRead(fsrPins[sensor]) < 1023)
    {
      digitalWrite(GREEN_PIN, HIGH);
      digitalWrite(RED_PIN, LOW);
    }
    if(sensor < 3 ; analogRead(fsrPins[sensor]) == 1023) {digitalWrite(GREEN_PIN, LOW);}

    if (sensor > 3; analogRead(fsrPins[sensor]) < 1023)
    {
      digitalWrite(RED_PIN, HIGH); 
      digitalWrite(GREEN_PIN, LOW);
    }      
    if(sensor > 3 ; analogRead(fsrPins[sensor]) == 1023) {digitalWrite(RED_PIN, LOW);}
    
    fsrReading[sensor] = analogRead(fsrPins[sensor]);
    Serial.print("Analog reading = ");
    Serial.print(fsrReading[sensor]);     
    if (fsrReading[sensor] < 100)
    {
      Serial.println(" - No pressure");
    }
    else if (fsrReading[sensor] < 500)
    {
      Serial.println(" - Light touch");
    }
    else if (fsrReading[sensor] < 1000)
    {
      Serial.println(" - Light squeeze");
    }
    else if (fsrReading[sensor] < 1500)
    {
      Serial.println(" - Medium squeeze");
    }
    else
    {
      Serial.println(" - Big squeeze");
    }
    Serial.println();
  }
      delay(1000);

}

The Arduino State Change Example shows you how to do that.

Detect when the sensor becomes pressed, record the time in milliseconds, detect when it becomes released, note the finish time and subtract start from finish.

what code should i add and where to add the code ? for note the time can I get the time to be displayed while pressing the FSR and not display the time while FSR is released?

could you plz add the code ?

That is not how the forum works.

Forum members are happy to help you solve specific problems with your code. Study the Blink without Delay example to learn how to use the millisecond timer, and the State Change example already mentioned.

If you want someone to write code for you, you are welcome to post on the Jobs and Paid Collaboration forum section, but be prepared to pay for the help.

totalPressDuration += (pressEndTime - startDetectionTime);

What this mean?

The compound operator += is a summation, a shortcut equivalent to the statement

totalPressDuration = totalPressDuration + (pressEndTime - startDetectionTime);

C:\Users\Lan\AppData\Local\Temp.arduinoIDE-unsaved20231013-19492-1vnshac.biifi\sketch_nov13a\sketch_nov13a.ino: In function 'void loop()':
C:\Users\Lan\AppData\Local\Temp.arduinoIDE-unsaved20231013-19492-1vnshac.biifi\sketch_nov13a\sketch_nov13a.ino:33:7: error: 'printHumanReadableTime' was not declared in this scope
printHumanReadableTime(startDetectionTime);
^~~~~~~~~~~~~~~~~~~~~~

exit status 1

Compilation error: 'printHumanReadableTime' was not declared in this scope

How to do this error?

As @jremington says, use the stated change detection method to detect when the analog signal is becomes higher or lower than some threshold.

Here is a way to use the state change detection method on an analog input. It features hysteresis so that the output does not chatter around the threshold. This code uses an LDR to turn a light on when it gets dark.

// turn light on when it gets dark

const byte sensorPin = A0;
const byte ledPin = 13;

const int threshold = 500;
const int hysteresis = 50;

bool sensorState;
bool lastSensorState = true;

void setup()
{
   Serial.begin(115200);
   Serial.println("analog state change demo");
   pinMode(sensorPin, INPUT_PULLUP); // internal pullup as LDR load resistor
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   int sensorReading = analogRead(sensorPin);
       
   // Is sensor output higher or lower than treshold?
   if (sensorReading < threshold - hysteresis)
   {
      sensorState = true;  // higher
   }
   else if (sensorReading > threshold + hysteresis)
   {
      sensorState = false;  // lower
   }

   // has the state changed?
   if (sensorState != lastSensorState)
   {
      // did the state change low to high?
      if (sensorState == false)
      {
         Serial.println("light on");
         digitalWrite(ledPin, HIGH);
      }
      // the state changed high to low
      else
      {
         Serial.println("light off");
         digitalWrite(ledPin, LOW);
      }
      lastSensorState = sensorState;
   }
}

how to print the system time ?

can anyone plz tell me hoe to control the motor speed depending upon the gait view?

I'm using Arduino board , step motor and 6 pin FSR sensor(insole).

please help, I'm a learner.......

An Arduino has no system time. The closest to that would be the millis() function, which gives the number of milliseconds from starting up your board, rolling over to zero every 49 days.
Serial.println(millis());

Your post #10 doesn't make sense.

thanks I did the timing display. but now I need to know how to control the "speed" of the stepper motor using the FSR sensor?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.