Recording time difference between input0 and input1

heya, im trying to build a setup that registers a force difference between two FSRs and the time between inputs, where it alerts you when the time is greater than half a second and the weight is more than 5kg.
Using an arduino uno, I have my code recording weight but im really not sure where to go from here

int fsr0 = 0;               // the FSR and 10K pulldown are connected to a0
int reading0;               // the analog reading from the FSR resistor divider
int voltage0;               // the analog reading converted to voltage
unsigned long resistance0;  // The voltage converted to resistance, can be very big so make "long"
unsigned long conductance0;
long force0;  // Finally, the resistance converted to force
long weight0;

int fsr1 = 1;               // the FSR and 10K pulldown are connected to a1
int reading1;               // the analog reading from the FSR resistor divider
int voltage1;               // the analog reading converted to voltage
unsigned long resistance1;  // The voltage converted to resistance, can be very big so make "long"
unsigned long conductance1;
long force1;  // Finally, the resistance converted to force
long weight1;

void setup(void) {
  Serial.begin(9600);  // We'll send debugging information via the Serial monitor
}

void loop(void) {
  reading0 = analogRead(fsr0);
  Serial.print("Analog reading0 = ");
  Serial.println(reading0);

  // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
  voltage0 = map(reading0, 0, 1023, 0, 5000);
  Serial.print("Voltage0 reading in mV = ");
  Serial.println(voltage0);

  if (voltage0 == 0) {
    Serial.println("No pressure");
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V        yay math!
    resistance0 = 5000 - voltage0;  // voltage0 is in millivolts so 5V = 5000mV
    resistance0 *= 10000;           // 10K resistor
    resistance0 /= voltage0;


    conductance0 = 1000000;  // we measure in micromhos so
    conductance0 /= resistance0;


    // Use the two FSR guide graphs to approximate the force
    if (conductance0 <= 1000) {
      force0 = conductance0 / 80;
      weight0 = force0 / 9.81;
      Serial.print("weight0 in kilograms: ");
      Serial.println(weight0);
    } else {
      force0 = conductance0 - 1000;
      force0 /= 30;
      weight0 = force0 / 9.81;
      Serial.print("weight0 in kilograms: ");
      Serial.println(weight0);
    }
  }










  reading1 = analogRead(fsr1);
  Serial.print("Analog reading1 = ");
  Serial.println(reading1);

  // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
  voltage1 = map(reading1, 0, 1023, 0, 5000);
  Serial.print("Voltage1 reading in mV = ");
  Serial.println(voltage1);

  if (voltage1 == 0) {
    Serial.println("No pressure");
  } else {
    // The voltage = Vcc * R / (R + FSR) where R = 10K and Vcc = 5V
    // so FSR = ((Vcc - V) * R) / V        yay math!
    resistance1 = 5000 - voltage1;  // voltage1 is in millivolts so 5V = 5000mV
    resistance1 *= 10000;           // 10K resistor
    resistance1 /= voltage1;
    Serial.print("FSR resistance1 in ohms = ");
    Serial.println(resistance1);

    conductance1 = 1000000;  // we measure in micromhos so
    conductance1 /= resistance1;
    Serial.print("Conductance1 in microMhos: ");
    Serial.println(conductance1);

    // Use the two FSR guide graphs to approximate the force
    if (conductance1 <= 1000) {
      force1 = conductance1 / 80;
      weight1 = force1 / 9.81;
      Serial.print("weight1 in kilograms: ");
      Serial.println(weight1);
    } else {
      force1 = conductance1 - 1000;
      force1 /= 30;
      weight1 = force1 / 9.81;
      Serial.print("weight1 in kilograms: ");
      Serial.println(weight1);
    }
  }
  Serial.println("--------------------");
  delay(1000);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

I edited your post as <CODE> is not the right code tag for proper formatting. (Thanks for trying at least).

Why do you calculate the voltage ? Everything seems linear so you would save time by working from the raw values.

So you want to measure if a value is above a certain threshold for a certain time.

The algorithm is pretty simple

Set a flag to false initially (in setup)

In the loop
Calculate the value

If the value goes over the threshold and the flag was not set then set up the flag to true and mark the time (record millis value in a startTime variable)

If the value is below the threshold set the flag to false

If the flag is true check the difference between the current time and the recorded startTime and if it’s above your target duration take action

You’ll have to decide what to do after taking action - do you keep repeating or you await for the value to go back under the threshold before starting a new cycle

This is a simple state machine. Here is a small introduction to the topic: Yet another Finite State Machine introduction

1 Like

ok finally getting a chance to look at this, not sure im fully understanding

i want to know the time difference between 'reading0'>0 and 'reading1'>0.
i should set both values to false(0), and when one is true(>0) i start a timer that ends when the other value is also true, and if that time is larger than, say, a second i send an alert?
if thats true then i have no clue how im going to do the time measurement (maybe the duration of one value being true and the other false?) but otherwise i think i can figure it out

I would use comparators on the sensor outputs to get clean switch edges instead of ADC.

not really sure what that means (this is funcitonally the first time ive done coding or comp sci, need it for my uni product design final major project)

I am not sure what you want to do

Do you want to be alerted when the difference between the two readings exceeds more than 5kg for more than half a second ?

kinda,

basically this is reading the gait of someone walking with a cane and telling them A) when they have an unequal weight distribution between a cane and foot(ive figured that one out) and B) when the cane isnt in time with the leg its supporting.

to solve problem B, im looking at a true/false statement for each of the sensors(where false is an input of 0 and true is an input greater than), then a third true/false statement for the difference (where its true when either both inputs are true or false, and false when they are different), recording the time that the third statement is false for and setting an alarm when its greater than a second.

this is the first time ive done coding and dont have any sort of experience in it so i dont know if there is a simpler way to do this, rather than frankenstining different bits of code together

Duplicate topic, same subject.

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