Go Down

Topic: FSR sensor ( counter ) problem (Read 1 time) previous topic - next topic

green_tie8

This the code for FSR sensor counter..

int fsrPin = 0;     // the FSR and 10K pulldown are connected to a0
int fsrReading;     // the analog reading from the FSR resistor divider
int counter = 0;

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

void loop()
{
  fsrReading = analogRead(fsrPin);
   Serial.print(counter);
   Serial.println(" steps");
 
  if (fsrReading > 10 && fsrReading < 350)
  {
    counter=counter + 1;
   Serial.print(counter);
   Serial.println(" steps");
  }
  else
  {
   Serial.print(counter);
   Serial.println(" steps");
  }
 
  delay(100);   
}


-------------------------------------------------------------------


The problem with this coding is fsrReading > 10 && fsrReading < 350 ,  the sensor will keep counting...
What i want is, if the FSR detect range 10 - 350 , the counter just count 1 only... From my code above, the sensor will keep counting when the sensor in the range..
I try to do in this way .. Let say if the FSR detect in range 10 - 350, the count will start count 1 and stay 1 if still in the range.. Then when it below 10, the counter will not count and then, it count 1 again when reach within the range. (just only one not will continously counting if still in the range)

hope u all understand what i try to explain..

Please help me in advance..

Chagrin

Code: [Select]
void loop()
{
  fsrReading = analogRead(fsrPin);
  static byte isReading;
   Serial.print(counter);
   Serial.println(" steps");

  if (fsrReading > 10 && fsrReading < 350 && !isReading)
  {
    counter=counter + 1;
    isReading = 1;
   Serial.print(counter);
   Serial.println(" steps");
  }
  else if (isReading)
  {
    isReading = 0;
   Serial.print(counter);
   Serial.println(" steps");
  }

  delay(100);   
}

green_tie8

Refer to the code you give..
I had try and the result still continuous counting when in range threshold..
is it possible arduino count just only 1 if it in threshold...(not continuous count).
because i want to make FSR as a step counter.
but the problem is when it in range threshold, the counter will continuously counting.
When a person step on a FSR, it will count and doesnt matter how long time it step on the FSR.the FSR will count 1 and if he/she make another step, it will count as 2..

Chagrin

Sorry I don't have an Arduino free to test the code with and I'm not seeing my bug.

Jack Christensen

I was a bit surprised by this at first, but I get excellent results by treating an FSR exactly like a tact switch. The code below illustrates this, using a debounce library that I wrote. I have the FSR connected to A0, but it is used as a digital input; any digital or analog pin can be used. I use the AVR's internal pullup so no external resistors are required.

Code: [Select]
//Counter using a Force-Sensitive Resistor (FSR) as input.
//Connect the FSR from the designated pin to ground, with no
//pullup or pull-down resistors.
//Works with this FSR https://www.sparkfun.com/products/9376

#include <Button.h>              //http://github.com/JChristensen/Button
#define FSR_PIN A0               //connect fsr from this pin to ground
#define PULLUP true              //use the AVR's internal pullup resistor
#define INVERT true              //low level means fsr pressed
#define DEBOUNCE_TIME 50         //milliseconds

Button fsr = Button(FSR_PIN, PULLUP, INVERT, DEBOUNCE_TIME);
int counter;

void setup(void)
{
   Serial.begin(9600);
}

void loop(void)
{
   fsr.read();
   if (fsr.wasPressed()) {
       Serial.print(++counter, DEC);
       Serial.println(" steps");
   }
}

PiJoy

You wrote:

The problem with this coding is fsrReading > 10 && fsrReading < 350 ,  the sensor will keep counting...
What i want is, if the FSR detect range 10 - 350 , the counter just count 1 only... From my code above, the sensor will keep counting when the sensor in the range..

----

Do you want your counter to increment one time for each time the (FSR) reading goes from outside your range to inside it?  In that case, you need a variable (one that keeps its value between calls to loop) to hold the previous state, namely inside vs. outside the signal band of interest.  So, I think you may want something like:

#define LOW_THRESH (10)
#define HIGH_THRESH (350)
#define FSR_PIN (whatever)

static  boolean prevInRange = false;  // *persistent* variable for previous state

void loop()
{
  int fsrVal;
  int count = 0;
  boolean curInRange;

  fsrVal = analogRead(FSR_PIN);

  if ((fsrVal >= LOW_THRESH) && (fsr_val <= HIGH_THRESH)) { // is FSR inside defined band?
    curInRange = true;
  }

  if ( (prevInRange == false) && (curInRange == true)) { // then we've just transitioned into the band
    count +=1;  // increment once per outside-to-inside transition
  }

  prevInRange = curInRange; // update history for next time

  // Your serial output code goes here

}

Hope this helps.


Go Up