Am interfacing ACS 712 with particle photon getting error answer

# include <math.h>
float current=0;
const int currentPin = A0;
const unsigned long sampleTime = 100000UL;                           // sample over 100ms, it is an exact number of cycles for both 50Hz and 60Hz mains
const unsigned long numSamples = 250UL;                               // choose the number of samples to divide sampleTime exactly, but low enough for the ADC to keep up
const unsigned long sampleInterval = sampleTime/numSamples;  // the sampling interval, must be longer than then ADC conversion time
const int adc_zero = 510;                                                     // relative digital zero of the arudino input from ACS712 (could make this a variable and auto-adjust it)

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

void loop()
{
Spark.publish("current", String(current) + "A"); 
CurrentSense();
Serial.println(current);
delay(1000);

}

void CurrentSense()
{
 unsigned long currentAcc = 0;
 unsigned int count = 0;
 unsigned long prevMicros = micros() - sampleInterval ;
 while (count < numSamples)
 {
   if (micros() - prevMicros >= sampleInterval)
   {
     long adc_raw = analogRead(currentPin) - adc_zero;
     currentAcc += (unsigned long)(adc_raw * adc_raw);
     ++count;
     prevMicros += sampleInterval;
   }
 }
 
 float rms = (sqrt((float)currentAcc/(float)numSamples) * (50 / 1024.0));
rms=rms-0.10;
if (rms<0.20)
{
rms=0;
}

current=rms;
}

this is program I have made. I am connecting the 60W bulb as lode I want mo measure current across it I connected the bulb series with ACS 712 and turn on power but am getting wrong value because value should be 0.25 A but am getting 117.25A please help with correct program

The ACS 512 requires a Vcc of 5 VDC. But Particle Photon analog inputs have a maximum input voltage of 3.3V. So if you correctly power the ACS 512, at the highest current supported you may destroy the Photon unless you also install a voltage divider circuit to limit in the voltage on the analog inputs (and change the formula), or use an external 5V I2C or SPI ADC.

You also need to take into account that the max reading of analogRead() is 4095 and not 1024 (on Arduino it would be 1023).

ya I did that also but still not working
I changed that please help with correct program because I want to show it with in two days

const int adc_zero = 2048;
float rms = sqrt((float)currentAcc/(float)numSamples) * (50/ 4096.0);

@vaibhav I just noticed your post. I am having issues making the AC712 sensor work but different. With your code is not working then try the attached which works for me. It uses a timer to fire a routine that takes 200 samples over 100milliseconds and calculates the RMS. I appear to have an offset which I cannot explain fully and the conversion factor is empirical based upon a 100W light bulb (pure resistive) load.

#include "math.h"
/**
 * ACS712 5A current sensor for AC current
 * analogRead () reads the value from the specified analog pin.
 * The ACS712 output can be between 0 and 5V hence a resistor
 * bridge is used to reduce the voltage from 0-5V to 0-3.3V.
 * GND - 2.2kohms - A Pin - 1kohms- OUT(ACS712)
 **/
#define ADCRANGE 4096
#define DEADBAND 0.05
#define LOADCHK 4999
const long adcZero = 2047;
const long adcAdjust = +69;         //calculated difference is +44 - 1.65-1.615/0.0008 - this offset is a mystery
const float adcToCurrent = 70.0;   // empirical 
const unsigned long sampleTime = 100000UL;                  //100mS for 50Hz
const unsigned long numSamples = 200UL;                     //every 0.5mS
const unsigned long sampleInterval = sampleTime/numSamples;
//
long readings[200];
float sAmps;
float amps = 0;
float lastAmps = 0;
const uint8_t currPin = A1;
//
void sampleCurrent()
{
    unsigned long currentAcc = 0UL;
    unsigned long count = 0UL;
    unsigned long prevMicros = 0UL;
    lastAmps = amps;
    prevMicros = micros() - sampleInterval;
    while (count < numSamples)
    {
        if ((micros() - prevMicros) >= sampleInterval)
        {
            long adcRaw = analogRead(currPin) + adcAdjust - adcZero;    //order depends upon current flow
            currentAcc += (unsigned long) (adcRaw * adcRaw);    //square the raw reading and add to sum of sample
            count++;
            prevMicros += sampleInterval;
        }
    }
    amps = (sqrt((float) currentAcc / (float) numSamples)) * (adcToCurrent/ADCRANGE);
    if (amps < DEADBAND) amps = 0.0;
}
//
Timer currentCheck(LOADCHK, sampleCurrent);
//
void setup()
{
    Serial.begin(9600);
    Serial.println("Setup AC712 Sensor");
    currentCheck.start();
}

void loop()
{
    delay(1000);
    sAmps = (amps + lastAmps)/2;     //smooth over last 2 readings
    Serial.printlnf("Latest Amps reading %2.3f", sAmps);
    //do something else
}

The warning I would give is that the ADCs seem to be affected by various factors which I do not fully understand. I have added this code to one with a SPI based display the ADC readings go all over the place (at random). I have tried different Analog pins - no difference; I have tried turning the power off for the display (unplug the Vcc from 5V) - no difference. Not sure what else I can try or what is causing this - Community???

no it is not working

where I have to connect 2.2k resisitor

It is explained in the comments at the top of the program
GND ----///----A?pin----///----Output from AC712 module
2.2K 1Kohms
APin voltage is then 2.2K/3.2K * 5 at maximum. If you want to be really careful then make the 1Kohms 1.2K ohms.

Check your connections to analog pin. Also, if you have a multimeter this is useful to confirm resistors in the correct way around.

what is not working??

It helped me to see the raw readings over the 200 samples by storing then in an array as the values are read and then printing them out to Serial.

will you please send me img or drowing of that how to connect all the because I connected but it shows 0.5 when no load is connected