counting from a sensor reading

hello, im new in programming and need some help.
im using an FSR (force sensitive resistor) and i need to count everytime the FSR reads the force.

lets say the name of FSR reading is (fsrreading).

thank you!

You need to test the value of the reading and add 1 to the count when the reading becomes greater than the threshold. Note that this is not the same as when it is greater than the threshold

Look at the StateChangeDetction example in the IDE for some ideas on how to do it

hey, thankyou for the quick reply.
i still need some help here. The counter keep increasing if i still press the FSR.

int fsrPin = 0;     
int fsrReading;     
int fsrVoltage = 0;   
int lastfsrVoltage = 0;
int cprcount;
unsigned long fsrResistance; 
unsigned long fsrConductance; 
long fsrForce;       
unsigned long timeBegin, timeEnd, time, totalTime;
 
void setup(void) {
  Serial.begin(9600);  
}
 
void loop(void) {
  time = millis();
  fsrReading = analogRead(fsrPin);  
  
 
  fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
  //Serial.print("Voltage reading in mV = ");
  //Serial.println(fsrVoltage);  


  if (fsrVoltage != lastfsrVoltage) {
    if (fsrVoltage != 0) {  
     cprcount++;
     
    if (cprcount==1) {
     timeBegin = millis();
   }
    fsrResistance = 5000 - fsrVoltage;     
    fsrResistance *= 10000;                
    fsrResistance /= fsrVoltage;
   // Serial.print("FSR resistance in ohms = ");
   // Serial.println(fsrResistance);
 
    fsrConductance = 1000000;           
    fsrConductance /= fsrResistance;
    //Serial.print("Conductance in microMhos: ");
    //Serial.println(fsrConductance);
 
    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("Total Comp : ");
      Serial.println(cprcount);          
  }
  Serial.println("--------------------");
  delay(100);
}


if (cprcount==30) {
  cprcount = 0;
  timeEnd = millis();
  totalTime = (timeEnd - timeBegin) / 1000;
  Serial.print("Total Time: ");
  Serial.println(totalTime);
}

lastfsrVoltage = fsrVoltage;
}

What button? You didn't say anything about a button just an FSR.

Steve

The first thing that I note is this

 fsrReading = analogRead(fsrPin);
  fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);

As analogRead() only returns a value between 0 and 1023 on most Arduinos (which board are you using ?) it is useless to try and expand the range using map() as there can only ever be 1024 distinct values

Secondly, as you are reading a force sensor the value read is likely to change by a small amount quite frequently unlike a digital signal that only has 2 possible values. What do you see when you print the reading ? It would be better to test whether the value has changed by an amount before deciding that the sensor had become pressed

 if (fsrVoltage != lastfsrVoltage)

As you never change the value of lastfsrVoltage it will almost always be true unless fsrVoltage is zero

As you never change the value of lastfsrVoltage it will almost always be true unless fsrVoltage is zero

The last line of OP's code is

 lastfsrVoltage = fsrVoltage;

I think he has this correct, but your comment about lack of threshold for an analog read change of value is likely the problem.

slipstick:
What button? You didn't say anything about a button just an FSR.

Steve

oh sorry i mean pressing the fsr

cattledog:
The last line of OP's code is

 lastfsrVoltage = fsrVoltage;

Only since he naughtily edited his code after I had commented on it...

@panducdrmwn please don't edit posts, particularly code, after it has been commented on