Counting with analog sensor value

im currently working on a CPR project, and using FSR.

i need to count the amount of compression while the FSR read the amount of the compression force given.

this is the FSR code.

int fsrPin = 0;     // the FSR and 10K pulldown are connected to a0
int fsrReading;     // the analog reading from the FSR resistor divider
int fsrVoltage;     // the analog reading converted to voltage
unsigned long fsrResistance;  // The voltage converted to resistance, can be very big so make "long"
unsigned long fsrConductance; 
long fsrForce;       // Finally, the resistance converted to force
 
void setup(void) {
  Serial.begin(9600);   // We'll send debugging information via the Serial monitor
}
 
void loop(void) {
  fsrReading = analogRead(fsrPin);  
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);
 
  // analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
  fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
  Serial.print("Voltage reading in mV = ");
  Serial.println(fsrVoltage);  
 
  if (fsrVoltage == 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!
    fsrResistance = 5000 - fsrVoltage;     // fsrVoltage is in millivolts so 5V = 5000mV
    fsrResistance *= 10000;                // 10K resistor
    fsrResistance /= fsrVoltage;
    Serial.print("FSR resistance in ohms = ");
    Serial.println(fsrResistance);
 
    fsrConductance = 1000000;           // we measure in micromhos so 
    fsrConductance /= fsrResistance;
    Serial.print("Conductance in microMhos: ");
    Serial.println(fsrConductance);
 
    // Use the two FSR guide graphs to approximate the force
    if (fsrConductance <= 1000) {
      fsrForce = fsrConductance / 80;
      Serial.print("Force in Newtons: ");
      Serial.println(fsrForce);      
    } else {
      fsrForce = fsrConductance - 1000;
      fsrForce /= 30;
      Serial.print("Force in Newtons: ");
      Serial.println(fsrForce);            
    }
  }
  Serial.println("--------------------");
  delay(1000);
}

how can i make the FSR count++ everytime it reads a force?

thanks.

Like this:

fsrReading = analogRead(fsrPin);  
count++;

aarg:
Like this:

fsrReading = analogRead(fsrPin);  

count++;

it didnt work, because its analog the sensor value is not always 0 or 1. so with one FSR press i got more than 1 count

panducdrmwn:
it didnt work, because its analog the sensor value is not always 0 or 1. so with one FSR press i got more than 1 count

The sensor value does not influence the incrementing of a variable. If you mean that one press will be detected multiple times through loop() that is a different problem. An easy fix would be once you detect a press, you increment your counter and then wait a bit of time before reading the new values.

blh64:
The sensor value does not influence the incrementing of a variable. If you mean that one press will be detected multiple times through loop() that is a different problem. An easy fix would be once you detect a press, you increment your counter and then wait a bit of time before reading the new values.

yes thats exactly what i mean.

[quote) An easy fix would be once you detect a press, you increment your counter and then wait a bit of time before reading the new values. [/quote]

even that single press is detected multiple time if i did this,

 if (sensorvalue > 0) {
count++;
}

I would bet than you would very rarely get an analog value of 0, there will be something there. I was referring to later on in your code where you are doing your calculations.

You also did not wait any amount of time, that is the key. if your "press" is detected by some value being above of threshhold, you increment count and then delay(500); or however many milliseconds you want to wait.

panducdrmwn:
even that single press is detected multiple time if i did this,

 if (sensorvalue > 0) {

count++;
}

Use the fact of reaching a threshold in concert with IDE -> file/examples/digital/statechangedetection. Conceptually, reaching some arbitrary analog value is little different from a switch closure - ie. you're either above the threshold or you're not.

Not an easy or straightforward one.

Start by logging a few presses onto the Serial monitor (just the raw ADC values) and plot it out.Take 10-100 readings a second, should give a reasonable curve.

Then look at your curve and start thinking how to detect a compression, that will be one peak in your curve, or maybe one valley depending on your actual setup - so a couple dozen to a couple hundred readings together cover one compression: at what point does it start - and you have to count up one, and at what point does it end - and you have to start looking for a new start. Then find a way to implement this in code.

You will probably have to average out the noise, too. E.g. by taking a rolling average of the latest five readings.

The start/stop parts are kinda covered in the state change detection, though for a high/low signal that is much simpler than for your analog signal.

The amount of force used that will be the area under that curve - i.e. you integrate force over time, from the moment you detect the start until the moment you detect the end.

wvmarle:
Not an easy or straightforward one.

Start by logging a few presses onto the Serial monitor (just the raw ADC values) and plot it out.Take 10-100 readings a second, should give a reasonable curve.

Then look at your curve and start thinking how to detect a compression, that will be one peak in your curve, or maybe one valley depending on your actual setup - so a couple dozen to a couple hundred readings together cover one compression: at what point does it start - and you have to count up one, and at what point does it end - and you have to start looking for a new start. Then find a way to implement this in code.

You will probably have to average out the noise, too. E.g. by taking a rolling average of the latest five readings.

The start/stop parts are kinda covered in the state change detection, though for a high/low signal that is much simpler than for your analog signal.

The amount of force used that will be the area under that curve - i.e. you integrate force over time, from the moment you detect the start until the moment you detect the end.

wvmarle:
Not an easy or straightforward one.

Start by logging a few presses onto the Serial monitor (just the raw ADC values) and plot it out.Take 10-100 readings a second, should give a reasonable curve.

Then look at your curve and start thinking how to detect a compression, that will be one peak in your curve, or maybe one valley depending on your actual setup - so a couple dozen to a couple hundred readings together cover one compression: at what point does it start - and you have to count up one, and at what point does it end - and you have to start looking for a new start. Then find a way to implement this in code.

You will probably have to average out the noise, too. E.g. by taking a rolling average of the latest five readings.

The start/stop parts are kinda covered in the state change detection, though for a high/low signal that is much simpler than for your analog signal.

The amount of force used that will be the area under that curve - i.e. you integrate force over time, from the moment you detect the start until the moment you detect the end.

is it really that complicated? can i just map the analog value to 0 - 1?

That may work as well. Try it! You may even be able to do this by connecting the FSR to a digital input.

But you will have no way of measuring the total amount of force applied. Just that some amount of force, greater than your threshold, has been applied for a period of time.