# Sample average of analog of current sensor

I have two problems in the code below. This ACS712 sensor is too noisy so I am not getting the count value properly, since I need approximate reading I consider curve is linear, and calculate the slope.

My problems are:

1. When I take a 10 sample average, the first value it reads properly, i.e. 512 is count value, other value taking as 0. I don't why.

2. Once current exceeds 1A, my relay output should go low. Once it goes low it waits for 30s & again it must high the relay high. Since I am checking output in loop, it making high & low without waiting for 30s. Can someone help me how can I do this?

If you used without MStimer, the `newaverage` values print 512 which is my reference, but for other 9 values print `newaverage` as zero.

``````#include <MsTimer2.h>

#define RELAY1  3
#define RELAY2  4

//int total;
int index;
float CS_Value;
static float Output_Current;
float lowlimit ;
float highlimit;
int count;
int total;
static int newaverage;
unsigned long Wait_Time=30000;
unsigned long Current_Time;
static int Tripped_Flag=0;

float standard_deviation(int avg1) {
total = 0;
for( int i=0; i<num_readings; i++)
total = total + pow((avg1 -readings[i]), 2);
}

int  average()
{
total = 0;
for( int i=0; i<num_readings; i++)
{
total = total + readings[i];
}
}

{

index = index + 1;

if (index >= num_readings)
{
index=0;
int avg = average();
float std = standard_deviation(avg);
lowlimit = avg - madstd;
highlimit = avg + madstd;

total=0;
count=0;
for(int i=0; i<num_readings; i++) {
// Check if the values of the readings are within the limits.
if(readings[i] >= lowlimit && readings[i] <= highlimit) {
total = total + readings[i];
count = count + 1;
}

}

// compute the new average
newaverage = total/count;
// send it to the serial port (as ASCII digits)

Serial.print("Newaverage:");
Serial.println(newaverage, DEC);
Output_Current = 0.0336666666667*newaverage - 17.17;
//return(Output_Current);
}

}

void setup() {
// sets the serial port to 9600
Serial.begin(9600);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
Relay_Intialize();

}

void loop()
{
MsTimer2::set(50, TakeReading); // 500ms period
MsTimer2::start();
Serial.print("current is :");
Serial.println(Output_Current);
//if (CS_Value>0.95 && Tripped_Flag==0)
if (Output_Current>0.95 && Tripped_Flag==0)
{
Serial.println("Main Activate");
Serial.print("Tripped_Flag_status:");
Serial.println(Tripped_Flag);
Relay_Activate();

}
else
{

Serial.println("Main Deactivate");
Serial.print("Tripped_Flag_status:");
Serial.println(Tripped_Flag);
Relay_Deactivate();
}
Serial.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
delay(1000);

}

void Relay_Activate()
{
for (unsigned long start = millis(); millis() - start < 10000;)
{
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,LOW);
Serial.println("RELAY GOT TRIPPED");
Tripped_Flag=1;
}

}

void  Relay_Deactivate()
{
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,HIGH);
Serial.println("RELAY NOT TRIPPED");
Tripped_Flag=0;

}

void Relay_Intialize()
{
Serial.println("RELAY GOT INTIALISE....");
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,HIGH);
}
`````` • Please can you write 1) clearer? I can't understand. Sep 9, 2014 at 6:53
• @lesto which point you are not clear about.
– AMPS
Sep 9, 2014 at 7:04
• " first value it reads properlyi.e 512 is count value other value taking as 0.I don't why." I cant understand what you expect andvwhatvyoubget Sep 9, 2014 at 7:10
• Instead of posting output screenshot, could you copy it here as a text? Screenshot is hard to read and impossible to copy.
– Suma
Sep 9, 2014 at 14:50

Please consider that in function `TakeReading()`, if the condition is never true, count is 0 when you come to `newaverage = total/count;`!