pulseIn function for dummies

Hi, I just bought myself my first arduino (uno).

My project is to create a jump mat which returns the jumpheight in cm. Therefore I bought a floormat which returns 0 if no body stands on it and returns 1023 if there is pressure on it. The idea is to measure the time that the jumper is in the air. I thought the start of the counting needs to be triggered when the mat returns a low value (I defined threshold as 40) and should stop counting when the jumper is back on the mat (mat returns a high value). I tried this with the function ‘pulseIn’. Right now I have this code:

/* Floor mat sensor
*/
int sensor=0;
int threshold=40; //threshold for start/stop counting
unsigned long duration;
unsigned long tijd;

void setup (){
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.begin(9600);
}

void loop() {

sensor=analogRead(0); // read data from floor mat

if (sensor < threshold) {
digitalWrite(13, LOW); // blink the internal arduino led LOW

// measure and print jump time

duration = pulseIn(0, 40, 5000);
Serial.println(duration);
//Serial.println(sensor);

}
else {
digitalWrite(13, HIGH); // blink the internal arduino led HIGH

}

}

Unfortunately it does not return the time/duration that te jumper is not on the mat. Instead it returns 0.

Any body an idea what is wrong with the code? Is pulseIn the right function?

Hope to hear from you.

Greats, Wytze

pulseIn() works on digital pins.

That make sense, thanks for the quick answer.

Any suggestion which function I can use instead?

Any suggestion which function I can use instead?

Polling.

Don't use digital pin 0, it is used for serial comms. You wouldn't have this problem if analogRead() didn't stupidly translate a pin value "0" to the value of "A0". It was meant as a convenience but it causes nothing but confusion, when it's mistakenly applied to a digital pin. So if you are using A0, always specify A0.

I connected digital pin 13 to digital pin 7. Digital pin 13 is the output which is HIGH when the sensor value > threshold. So I thought pin 7 would be a digital input by now and I would be able to use pulseIn on pin 7.

Unfortunately the result of Serial.print(duration) where: duration(7, 40, 5000) is 0 when I step of the mat.

Can anybody tell me what's wrong with the code and how I should fix it?

Wytze:
I connected digital pin 13 to digital pin 7. Digital pin 13 is the output which is HIGH when the sensor value > threshold. So I thought pin 7 would be a digital input by now and I would be able to use pulseIn on pin 7.

Unfortunately the result of Serial.print(duration) where: duration(7, 40, 5000) is 0 when I step of the mat.

Can anybody tell me what's wrong with the code and how I should fix it?

Why did you connect the pins together? It makes no sense.

aarg:
Why did you connect the pins together? It makes no sense.

Maybe any suggestion what does make sense to use the function pulseIn when I have the analog input from the mat and need a digital input for the function?

If your mat really measures 0 and 1023 as you say, then you can read it digitally. Because 0 and 1023 are equivalent to HIGH and LOW logic levels. In fact, reading that, I doubt that the mat is analogue at all.

Please provide some information and/or links to the mat.

The mat provides also values between 0 and 1023. This is during stepping on and off the mat. When fully loaded or not loaded the values are 1023 or 0. See the attachment for the pdf with specifications.

I tried reading the data with digitalRead but this showed no results.

751913-an-01-ml-ALARMTRITTMATTE_GROSS_de_en_fr_nl.pdf (141 KB)

Wytze:
The mat provides also values between 0 and 1023. This is during stepping on and off the mat. When fully loaded or not loaded the values are 1023 or 0. See the attachment for the pdf with specifications.

I tried reading the data with digitalRead but this showed no results.

This mat is not analogue, it is simply a switch (as I suspected, and tried to tell you).
From the manual:

when the alarm floor mat is stepped on the integrated NO contact will be closed and thus, the alarm system will be triggered

It is only because of the way that you have it wired, that it does not work correctly. If you connect it between ground and the input pin, and enable the input pull up resistor with pinMode(, INPUT_PULLUP), it will work perfectly well on a digital input.

It is because of the way analogue readings are taken that it happens to produce the analogue values you are seeing. It’s input, but reading it that way will be much more difficult for you. If that hasn’t become obvious to you already.

If you are not getting digital readings, it is only because your wiring or software is incorrect.

By the way, your jump height won't be accurate, because people retract their legs into a spring position on the return, to cushion the shock. That will randomly increase the time spent "in the air".

Maybe any suggestion what does make sense to use the function pulseIn when I have the analog input from the mat and need a digital input for the function?

Nothing about trying to use pulseIn() makes any sense when reading an analog pin. So, forget it. You can't use pulseIn() to measure how long an analog pin is above or below some threshold.

You MUST determine, in a fast loop, when the pin changes from being above the threshold to being below the threshold, and when the pin changes from being below the threshold to being above the threshold. The difference between the times is how long there is no one on the mat.

Not that it makes any difference, since what you have isn't an analog device.

The mat may have latency on its response as it is a mechanical device, you may need to calibrate
for this.