Relay triggering
Here is my code. I have 2 problem in below code. This ACS712 sensor is too noisy so i am not getting count value properly .since i need approximate reading i consider curve is linear. and calculate slope
my problem are
1) When i take 10 sample average. first value it reads properlyi.e 512 is count value other value taking as 0.I don't why.
2) Once current exceed 1A , my relay output should go low.once it go low it wait for 30s & again it must high the relay high. since i am chencking output in loop . it making high & low without waiting for 30S. Can some one help me how can i do this
my problem are
1) When i take 10 sample average. first value it reads properlyi.e 512 is count value other value taking as 0.I don't why.
2) Once current exceed 1A , my relay output should go low.once it go low it wait for 30s & again it must high the relay high. since i am chencking output in loop . it making high & low without waiting for 30S. Can some one help me how can i do this
const int num_readings = 10;
float offSet = 2.44009;
const int mad = 2.24;
int readings[num_readings];
int index = 0;
float sample2SolCrnt = 0.0;
float solar_crnt = 0.0; // Solar panel current variable
float solarCrntVal = 0.0; // Current callibration variable
float current;
#define RELAY1 3
#define RELAY2 4
#include <Time.h>
#include <avr/wdt.h>
void Relay_Intialise()
{
digitalWrite(RELAY2,LOW);
digitalWrite(RELAY1,LOW);
}
void setup()
{
// sets the serial port to 9600
Serial.begin(9600);
pinMode(RELAY1, OUTPUT);
Relay_Intialise();
wdt_enable(WDTO_8S);
}
int average() {
int total = 0;
for(int i=0; i<num_readings; i++)
total = total + readings[i];
return total/num_readings;
}
float standard_deviation(int avg) {
int total = 0;
for(int i=0; i<num_readings; i++)
total = total + pow((avg - readings[i]), 2);
return sqrt(total/num_readings);
}
void loop() {
wdt_reset();
float sum=0.0;
int reading = analogRead(A0);
readings[index] = reading;
index = index + 1;
// if have already been done 10 readings...
if (index >= num_readings) {
// set the index to 0
index = 0;
// compute the average
int avg = average();
// compute the standard deviation
float std = standard_deviation(avg);
float madstd = mad * std;
float lowlimit = avg - madstd;
float highlimit = avg + madstd;
int count = 0;
int total = 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
int newaverage = total/count;
// send it to the serial port (as ASCII digits)
Serial.println(newaverage, DEC);
current = 0.0336666666667*newaverage - 17.17;
Serial.print("current is :");
Serial.println(current);
}
Serial.print("current out is :");
Serial.println(current);
if(current>0.90)
{
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY1,LOW);
Serial.println("relay got tripped");
}
else
{
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,LOW);
Serial.println("relay not tripped");
}
Serial.println(".....................");
delay(1000);
}

Comments