Force Sensing Resistor (FSR) & state machine

Hello,

I am hoping someone can help me with an issue I am having.

I am using a FSR and a state machine to try and detect how long a force has been applied. When the force has been applied, I want to take the time at which that happens in the program, and also note the time at which it is released. Subtracting the two gives me the duration.

The code is below

int digitalPin = 2;
int val, start, finish = 0;
int pressureState = 0;
int result = 0;

void setup()
{
    Serial.begin(9600);
    pinMode(digitalPin, INPUT);
}

void loop()
{
    pressureState = digitalRead(digitalPin);
    if(pressureState == HIGH)
    {
        start = millis();
    }

    if(pressureState == LOW)
    {
        finish = millis();
    }

    if (finish - start > 0)
    {
        result = finish - start;
        Serial.println(result);
        start = 0;
        finish = 0;
    } 
}

However, when I open the serial monitor, it seems to be counting up to about 32760, holds there for about 30 seconds, and the restarts. Any time I press the FSR, the figures stop and seem o hold for as long as I apply the pressure.

I am sure it is a complete newbie mistake I am making, but than, I am a newbie, so that is okay I guess.

Hope someone can point me in the correct direction.

Seán

A force sensor does not provide an on/off value, so reading a FSR using digitalRead doesn't make sense.

If you want to use it as an on/off switch, you need to capture the time when the state [u]changes[/u] from HIGH to LOW or from LOW to HIGH, not just the time when the state [u]is[/u] HIGH or LOW.

Finally, millis() returns an unsigned long, not an int. You need to store time data in the correct type variable.

Hello,

And thank you for the reply!

I will change the time variable. But how would I detect when the state changes from HIGH to LOW rather than when it is HIGH or LOW?

Thanks again.

Seán

But how would I detect when the state changes from HIGH to LOW rather than when it is HIGH or LOW?

Well, if it is HIGH now, and was not last time, that represents a change. Similarly, if it is LOW now and was not last time, that represents a change.

Therefore, you need to, at the end of loop(), store in a global variable the value this time, so that on the next pass through loop, you know what the value was last time.

Hmmm, ok, thanks for that! I will try and get something going. :)

Seán

I was a bit surprised, I used this FSR in a project that only needed an on/off indication, and I treated it exactly like a tact switch, using a little debounce library I developed, and internal pullups just like the tact switches, and it worked perfectly. I believe the I/O pins have some amount of hysteresis which no doubt helps.

I can share the library if anyone's interested. It will tell if a button is pressed currently, whether it changed since it was last checked, and whether it's been pressed for a certain amount of time. To instantiate a button, specify the pin it's connected to, whether the internal pullup resistor should be enabled, whether a HIGH signal level should be interpreted as pressed or released, and the debounce time in milliseconds.

o.fithcheallaigh: ...it seems to be counting up to about 32760...

The clue here is where you stop - 32767 happens to be the highest positive number that can go into an int, and you're having troubles around 32760. Checking your code, you're storing the millisecond value (which is unsigned long) in two ints, start and finish. Change them to unsigned long and I think your problem will go away.

Also, if I understand, you're just taking the start and stop times, and subtracting - but you know the stop time at the point you stop, so do the calculation there and you don't even need finish:

    if(pressureState == HIGH)
    {
        start = millis();
    }
    if(pressureState == LOW)
    {
        // stop and calculate time now
        result = millis() - start;
        Serial.println(result);
        start = 0;
    }
}

Whether you really need start = 0; is up to you now...

Ooh yes, I never thought of that! Thank you!

Seán