# Measure Zero Crossing

I want to start and stop timer whenever I want.and then I need to calculate the time duration.I will put a full wave ac signal to the analog pin.and want to calculate the time duration of first zero crossing to next zero crossing of that signal.please help me

suppose continuous data is coming through the analog pin.I want to store those data in a array.like data are 0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0....................................................................................... I want to store these data spited like [0 1 2 3 4 5 6 7 8 9 10 11] [0 1 2 3 4 5 6 7 8 9 10 11] [0 1 2 3 4 5 6 7 8 9 10 11] and after that I want to find the maximum value from individual array. please give me the code.

If all you want is to find a peak value (and you dont care about when or anything else)

``````setup()
{
max=0
}

loop()
{
if (v>max) {
max=v
}
if (v==0) { // or maybe a bit higher to allow for noise
Serial.println(max)
max=0
}
//maybe delay a bit here
}
``````

how the above code able to calculate the time duration of 1st to next zero crossing????

maybe something like this?

``````setup()
{
max=0
last=0
}

loop()
{
if (v>max) {
max=v
}
if (v==0) { // or maybe a bit higher to allow for noise
now=millis()
duration=now-last

Serial.print(max)
Serial.print(",")
Serial.println(duration)

max=0
last=now
}
//maybe delay a bit here
}
``````

Obviously the first duration wont be accurate and you will probably have to do some checks on the value of duration to ensure its not too low because you read zero on consecutive readings

nabin1994:
I will put a full wave ac signal to the analog pin.

Do you mean that the AC signal is full wave RECTIFIED? Feeding a non rectified AC signal dangerous to the Arduino seeing that the range of input voltages to the analog pin is from 0-5v.