Error of Code compile

Dear all.

I am trying to upload below 2 code. Here below code compile properly. But Code i have attached in Rar file giving me error.
I have just seprated variable & declare globally.Let me Know what mistake i am doing here.

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   
void setup() {
  // sets the serial port to 9600
  Serial.begin(9600);
    pinMode(RELAY1, OUTPUT);
}

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() {
  float sum=0.0;
  // read analog input pin 0
  int reading = analogRead(A0);
  readings[index] = reading;
  // incrementing the index
  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);
   // sum=sum+(.0264 *newaverage -13.51);
    
    
  // float current= 0.0264*(newaverage-512);
    //float current=0.0264*(newaverage-518);
      current = 0.0336666666667*newaverage - 17.17;
     Serial.print("current is :");
  Serial.println(current);
  }
  
  if(current>1.0)
  {
    digitalWrite(RELAY1,LOW);
      Serial.println("relay got tripped");
  }else
  {
    digitalWrite(RELAY1,HIGH);
    Serial.println("relay not tripped");
  }
   Serial.println(".....................");
  
  delay(1000);
  
  // wait 1000/num_readings ms for next reading
  //delay(int(1000/num_readings));
}

ACS712_Modified.rar (1.1 KB)

How does one open a rar file?

Post code like normal.

Read the error: it tells you exactly what's wrong.....

#include"glob.h"
float std;
float  CS_value;

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

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




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

void loop()
{
  reading = analogRead(A0);
  readings[index] = reading;
  index = index + 1;//increment the index
  if (index >= num_readings) {
    // set the index to 0
    index = 0;
    // compute the average
   int  avg = average();
    // compute the standard deviation
  std= standard_deviation(avg);

    madstd = mad * std;
    lowlimit = avg - madstd;
    highlimit = avg + madstd;
    //count = 0;
   // 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
    newaverage = total/count;
    // send it to the serial port (as ASCII digits)
    Serial.println(newaverage, DEC);

    //  current= 0.0264*(newaverage-512);

    current = 0.0336666666667*newaverage - 17.17;

    Serial.print("CS value:");
    Serial.println(current);

  }
  delay(500);
}

glob.h

#ifndef GLOB_H
#define GLOB_H


 #define RELAY1  3
 
 
const int num_readings = 5;
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;
  int avg;

 float madstd;
 float lowlimit ;
 float highlimit;
 int count = 0;
 int total = 0;
int newaverage;
int reading;


#endif

functions call & function definition defined properly so why it is giving error

Here is my updated code. It seem working .
Now i have another problem below.

Problem1:
for first sample CS_Value is 512 and other 10 reading are 0.I think i am calling loop to fast that i cant accumulate data.

  1. once current exceed 0.95 A , My relay got tripped . once relay got tripped it must wait for 30s to close switch again. But in below program . it is tripping and connecting not waiting for 30S.

can some one suggest me how to overcome this problem,

#include <MsTimer2.h>

#define RELAY1  3
#define RELAY2  4

int num_readings=10;
//int total;
int readings[10];
int index;
float CS_Value;
static float Output_Current;
float mad;
float madstd;
float lowlimit ;
float highlimit;
int count;
int total;  
int reading;  
static int newaverage;
unsigned long Wait_Time=30000;
unsigned long Current_Time;


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

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

float TakeReading()
{
  
  reading = analogRead(A0);
  readings[index] = reading;
  index = index + 1; 
  
  if (index >= num_readings)
  {
    index=0;
     int avg = average();
    float std = standard_deviation(avg);
    madstd = mad * std;
    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;
    }

  }
 /* Serial.print("total:");
  Serial.println(total);
  Serial.print("count:");
  Serial.println(count);
 */ 
  // 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();
 //  MsTimer2::set(500, TakeReading); // 500ms period
//  MsTimer2::start();
  
}

void loop()
{

  CS_Value=  TakeReading();
  Serial.print("current is :");
  Serial.println(CS_Value);
  if(CS_Value>0.95)
  {
    Relay_Tripped();
  }
  
  
  
  
  
  
  Serial.println(".....................");
  delay(500);
}

void  Relay_Tripped()
{
 
  Current_Time= millis();
  if(Current_Time<Wait_Time &&digitalRead(RELAY1)==LOW)
  {
     digitalWrite(RELAY1,HIGH);
     digitalWrite(RELAY2,HIGH);
     Serial.println("RELAY GOT TRIPPED");
  }else
  {
   digitalWrite(RELAY1,LOW);  
   digitalWrite(RELAY2,LOW);
    Serial.println("RELAY  MOT TRIPPED");
  }
  
}


void Relay_Intialize()
{
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,HIGH);
}
  Current_Time= millis();
  if(Current_Time<Wait_Time &&digitalRead(RELAY1)==LOW)

Once 30 seconds have elapsed Current_Time will always be greater than Wait_Time.

Maybe read this:

And maybe this:

Is there any way to reset the Current_Time reset.

if (millis () - startTime >= interval)will it work

I have changed the below part. My code look like this . Now it seems almost ok. But I need clarification Is any mistake i am doing here.

void loop()
{

  CS_Value=  TakeReading();
  Serial.print("current is :");
  Serial.println(CS_Value);
 if (CS_Value>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(600);
 
}


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;
  
}

But I need clarification Is any mistake i am doing here.

Serial.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,");

You're wasting RAM and your indentation needs work.

Ya. sure. But i am asking major part Here. IS my code correct.

IS my code correct

Turing proved, long ago, that you can never know.

For below code i am getting output as in jpg file. I am wondering something wrong i am doing with taking average.Please clarify

#include <MsTimer2.h>

#define RELAY1  3
#define RELAY2  4

int num_readings=10;
//int total;
int readings[10];
int index;
float CS_Value;
static float Output_Current;
float mad;
float madstd;
float lowlimit ;
float highlimit;
int count;
int total;  
int reading;  
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);
  return sqrt(total/num_readings);
}

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

void TakeReading()
{
  
  reading = analogRead(A0);
  readings[index] = reading;
  index = index + 1; 
  
  if (index >= num_readings)
  {
    index=0;
     int avg = average();
    float std = standard_deviation(avg);
    madstd = mad * std;
    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();
//  CS_Value=  TakeReading();
 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);
}

I dont know why data accumulated is improper.I have tested both code. for Newaverage top code first value read as 512 for reference and other 9 value as zero.

You have done over 900 posts and you are still posting in quote tags rather than code tags?

Oh dear, oh dear.

How to use this forum

I suggest you read it. Now.

AMPS-N:
Ya. sure. But i am asking major part Here. IS my code correct.

Test it. If it works, good. If not, then it is not correct.

    total = total + pow((avg1 -readings[i]), 2);

Stupid. Using pow() to square a number is the height of incompetence.