time problem (analogRead)

Hi,

I am reading one sensor with the analogRead command. That works fine so far.
My loop code:

sensorValue = analogRead(analogSensor);    
Serial.println(sensorValue); 
delay(50);

The values are (example):

0
0
0
10
35
8
80
0
0
0
0
0
0
0
0
45
7
100
0
0
0

Now I would like to know how long are the values bigger than 0 and how long are the values = 0.
I would be very grateful if you could guide me through my problem.

Introduce a counter and state variable.
Increment the counter on every loop pass.
When the state is "zero" and the read value is none zero, change state. Ditto the other way.
Whenever you change state, the counter shows how long the zero or non-zero period was (before you zero it)

State change detection example gives you an idea on how to detect a signal edge. Use millis() to record the time of the signal edge. At a later time, subtract said recording from millis() to get the elapsed time.

The example demonstrates the concept using digital signals, but it can work fine for analog signals as well (you may just need to use > or < instead of ==).

Thank you very much for your answers :slight_smile:
It is working fine now!