You can easily do this in Arduino.
However I've noticed that you've written the code in C with the Windows functions. Why not write the Arduino code and use what the Arduino already has available, which is the millis()
and micros()
functions? The only problem with the micros()
function is that it will overflow after 70 minutes.
The other thing to mention is how will you know when your sensor is on? One way would be to constantly read the value and set a threshold so that you can see whether the pressure sensor is under pressure or not.
Most of the code for this can be extracted from the various Arduino examples that are on the website.
For the pressure sensor threshold you can use the Arduino AnalogInput example and add an if statement after the analog read and combine that with the millis()
function from the Arduino BlinkWithoutDelay example:
int sensorPin = A0; // Choose whichever analog pin you like
int sensorValue = 0;
int flagA = 0;
// flag is used just to indicate if timer has started or stopped
unsigned long startTime;
unsigned long stopTime;
unsigned long elapsedTime;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);
// read the value from the sensor
if(flagA == 0 && sensorValue > xxx) then {
// if above threshold run timer
startTime = millis();
flagA = 1;
}
if(flagA == 1 && sensorValue < xxx) then {
// if falls below threshold stop timer
stopTime = millis();
elapsedTime = stopTime - startTime;
Serial.print("Elapsed time = ");
// Put your print function in here
Serial.print(elapsedTime);
Serial.println(" ms");
flagA = 0;
}