Arduino Frequency Measurement

Dear all, I just join the topic by the way.

I am building a grid frequency measurement with arduino Uno. Here is the sketches that i made.

#include<math.h>
int sensorPin =0;
int sensorValue=0;
int i=0;
int k=0; \\ counter
int arr[500];\\ sample size 500
float f; \\ frequency

void setup()
{
  Serial.begin(9600);
  
}

void loop ()
{ i=0;
  k=0;
  for (i=0;i<500;i++) \\ loop until reach sample size
  { arr[i]=analogRead(sensorPin);
    if(arr[i]==0 && arr[i+1]>0)\\ zero crossing rule
    {k=k+1;}\\ increment zero crossing
  
  break;
  }
  f=k/568;\\frequency formula
  Serial.println(f,4);
  Serial.println(k);
}

................................................................................

I am supplying a 2.5VAC to analog pin 0.

My question is, does the for loop correct? I have not tried this yet on the board as it already weekend and my hardware is at the university.

Last time when i tried another program similar to this, it gives wierd output at serial.

below is the problematic sketch which i used before and tested without looping the whole thing. The above sketch i just did last night.

	int sensorPin = 0;    // select the input pin for the potentiometer
	      // select the pin for the LED
	int sensorValue = 0;  // variable to store the value coming from the sensor

        int i;
        int k=0;

	int arr[500] ;

        float f;

	void setup() {

		Serial.begin(9600);
	
        
                for(i=0;i<501;i=i++)
		{
                arr[i] = analogRead(sensorPin);
                
		}

//for(i=0;i<251;i=i+1)
//{
//Serial.println(arr[i]);
//}

for(i=0;i<501;i=i++)
{
if(arr[i]==0 && arr[i+1]!=0)
{
  while(arr[i+1]!=0)
  {
    k=k+1;
    //Serial.println("k");
  i=i+1;
}
break;
}

}
f=1000/(2*k*0.112);
Serial.println(k);
Serial.println("FREQUENCY MEASURED IS");
Serial.println(f, 4);


}

void loop (){

}

The serial monitor showed the same value of k and frequency for this sketch which makes me wonder does the for loop really goes to 500 sample or stop somewhere. when i make serialprintln (i), it stoped and 200+ and still print the frequency value.

Thank you in advance

moderator update - added code tags == #button above smileys

I am supplying a 2.5VAC to analog pin 0.

Plan on buying a new Arduino and not doing that.

Again. -2.5 v Isn't healthy for a device that diode clamps inputs to the supply rails, latchup would be the least of the many issues encountered in an attempt like that.

Doc