Go Down

Topic: Timing pressure using a stopwatch (Read 601 times) previous topic - next topic

Seán

Hello,

I hope someone can help me with a little bit of code here.

I am trying to time how long a pressure is applied to a FSR. So, I will start a stopwatch running when the pressure is applied (above a value of 200 and the analogRead), noting that time, and I want to note the time the stopwatch is finished, and I want it to finish when the pressure drops below a specific value (i.e. below 100 on the analogRead).

Code: [Select]

#include <StopWatch.h>

StopWatch MypressureSW;

int analogPin = 0;
int fsrReading, fsrVoltage;
float val1 = 0;
float val2 = 0;
float totalTime;

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

void loop()
{
  if(analogRead(analogPin) < 100)
  {
    ;;
  }
  if(analogRead(analogPin) > 200)
  {
    pressureTime();
  }
 
  delay(1000);
}

void pressureTime()
{
  MypressureSW.start();
  val1 = MypressureSW.value();
  Serial.print("Pressure Applied At: ");
  Serial.println(val1);
  delay(10);
 
  if(analogRead(analogPin)<100)
  {
    MypressureSW.stop();
    val2 = MypressureSW.value();
    Serial.print("Pressure removed at: ");
    Serial.println(val2);
   
    totalTime = val2 - val1;
    Serial.print("The total time of pressure applied is: ");
    Serial.println(totalTime);
    MypressureSW.reset();
  }
}


However, all I get on the Serial Monitor, when I apply a constant pressure is

Quote

Pressure Applied At: 0.00
Pressure Applied At: 2037.00
Pressure Applied At: 3076.00
Pressure Applied At: 4115.00


And when I let go, it just sits with that information. So, it seems it isn't reading the time when I let the pressure go.

I imagine I am doing something obvious and silly, but I can't see it.

Thanks in advance.

Seán
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

PaulS

Get rid of the separate function, until the timing works.

Start the stopwatch if the pressure is above 200 AND the stopwatch is not running.

Stop the stopwatch if the pressure is below 200 AND the stopwatch is running. When you stop the stopwatch, print the time.

In order to do this, you need to know if the stopwatch is running. You can define a flag (bool) to keep track of that, if the stopwatch class doesn't have a method to tell you.

Seán

Hey,

Thanks for the reply.

I get what you are say, but I am not too sure how to use flags etc, so I will do some reading and try to figure that out.

Thanks!

Seán



Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

AWOL

Don't think of a flag, think of a pencil tick in the box:
Action: I've started the stopwatch, put a pencil tick in the box.
Action: I've stopped the stopwatch, rub-out the tick.

Question: Is the stopwatch running? - look in the box.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Something like this, maybe:
Code: [Select]
bool watchRunning = false; // This is a flag

void loop()
{
   int pressure = analogRead(pin);
   if(pressure > 200)
   {
      if(!watchRunning)
      {
         // Start it running
         watchRunning = true;
      }
   }
   else
   {
      if(watchRunning)
      {
         // Stop it
         // Print time
         watchRunning = false;
      }
   }
}

Seán

Hey,

Thanks.

I can't find anything explaining what 'bool' does (I am assuming it is connected with Boolean?), do you know anywhere were I can read up on this? I'd like to understand the code rather than take someones else.

Again, thanks :)

Seán
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

wildbill

bool and boolean are synonyms. Boolean reference on the main site (not much there though): http://arduino.cc/en/Reference/BooleanVariables

Go Up