# Counting  with analog sensor value

**URL:** <https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874>\
**Category:** General Guidance\
**Created:** [April 10, 2020, 3:22pm UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874 "2020-04-10T15:22:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![panducdrmwn](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@panducdrmwn](https://forum.arduino.cc/u/panducdrmwn)\
**Post date:** [April 10, 2020, 3:22pm UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/1 "2020-04-10T15:22:02Z")

</div>

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.

```cpp
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.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [April 10, 2020, 4:12pm UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/2 "2020-04-10T16:12:49Z")

</div>

Like this:

```cpp
fsrReading = analogRead(fsrPin);  
count++;

```

---

<div class="post-metadata">

**Author:** ![panducdrmwn](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@panducdrmwn](https://forum.arduino.cc/u/panducdrmwn)\
**Post date:** [April 10, 2020, 5:19pm UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/3 "2020-04-10T17:19:14Z")

</div>

> aarg:  
> Like this:
> 
> ```arduino
> fsrReading = analogRead(fsrPin);  
> 
> ```

count++;

> ```arduino
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [April 10, 2020, 5:25pm UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/4 "2020-04-10T17:25:00Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![panducdrmwn](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@panducdrmwn](https://forum.arduino.cc/u/panducdrmwn)\
**Post date:** [April 10, 2020, 6:17pm UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/5 "2020-04-10T18:17:58Z")

</div>

> 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,

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

```

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [April 10, 2020, 7:58pm UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/6 "2020-04-10T19:58:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dougp](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dougp/32/238908_2.png) [@dougp](https://forum.arduino.cc/u/dougp)\
**Post date:** [April 10, 2020, 8:44pm UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/7 "2020-04-10T20:44:36Z")

</div>

> panducdrmwn:  
> even that single press is detected multiple time if i did this,
> 
> ```arduino
> if (sensorvalue > 0) {
> 
> ```

count++;  
}

> ```arduino
> 
> ```

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.

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [April 11, 2020, 2:07pm UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/8 "2020-04-11T14:07:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![panducdrmwn](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@panducdrmwn](https://forum.arduino.cc/u/panducdrmwn)\
**Post date:** [April 12, 2020, 7:00am UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/9 "2020-04-12T07:00:17Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![wvmarle](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/wvmarle/32/1013044_2.png) [@wvmarle](https://forum.arduino.cc/u/wvmarle)\
**Post date:** [April 12, 2020, 8:19am UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/10 "2020-04-12T08:19:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 7:11pm UTC](https://forum.arduino.cc/t/counting-with-analog-sensor-value/647874/11 "2021-05-05T19:11:57Z")

</div>


