Load Control with Arduino

Hi,
I'm doing a project on load control. I have designed a system to turn ON four loads (L1, L2, L3, L4) sequentially at 4 second interval. Then used ACS712 (30A) module to monitor the total current of the loads. When the current is greater than 1.6, I want to turn OFF L4. The code attached here is the code I have been able to write and upload to the board. However, I am finding it difficult in turning OFF L4 when result (total current) is greater than 1.6.
For the hardware description: I connected the output of the Arduino to drive 25A solid state relays and I am able to measure the current accurately using the serial monitor.
Please I need suggestions on how to make this work out , thanks.

/*
  Code for load control system

*/

// constants won't change. They're used here to set pin numbers:
const int L1 = 2;     // Load 1
const int L2 =  3;    // Load 2
const int L3 = 4;     // Load 3
const int L4 =  5;    // Load 4

const int sensorIn = A0;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

void setup() {
  Serial.begin(9600);
  pinMode(L1, OUTPUT);
  pinMode(L2, OUTPUT);
  pinMode(L3, OUTPUT);
  pinMode(L4, OUTPUT);

  digitalWrite(L1, HIGH);
  delay(4000);
  digitalWrite(L2, HIGH);
  delay(4000);
  digitalWrite(L3, HIGH);
  delay(4000);
  digitalWrite(L4, HIGH);
  delay(4000);
}

void loop() {
  float result;
  if (result > 1.6)
  {
    digitalWrite(L1, HIGH);
    digitalWrite(L2, HIGH);
    digitalWrite(L3, HIGH);
    digitalWrite(L4, LOW);
  }
  Voltage = getVPP();
  VRMS = (Voltage / 2.0) * 0.707;
  AmpsRMS = (VRMS * 1000) / mVperAmp;
  Serial.print(AmpsRMS);
  Serial.println(" Amps RMS");
}


float getVPP()
{
  float result;
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here

  uint32_t start_time = millis();
  while ((millis() - start_time) < 1000) //sample for 1 Sec
  {

    readValue = analogRead(sensorIn);
    // see if you have a new maxValue
    if (readValue > maxValue)
    {
      /*record the maximum sensor value*/
      maxValue = readValue;
    }
    if (readValue < minValue)
    {
      /*record the maximum sensor value*/
      minValue = readValue;
    }
  }

  // Subtract min from max
  result = ((maxValue - minValue) * 5.0) / 1024.0;
  return result;
}

Ogu-Reginald:
However, I am finding it difficult in turning OFF L4 when result (total current) is greater than 1.6.

You "find it difficult", that's not really an accurate description of your problem.

Does it work? Does it not work? If so, what is the error message? I'm still new myself, so I'm sure some expert will chime in. But from a first glance I would suspect that

float result;

will initialize result as 0.

I would suggest you initialize result once globally, and call getVPP() before your if statement, so result actually has been calculated and can be used in the statement.

cheers!

It’s pretty obvious you lifted portions of this code from the web and threw it together and thought it would work. You need a lesson in variables. Local variables versus global variables and the scope of variables. You should not use subroutines until you understand variables and scope. You need to start over. No subroutines. Do one thing a time, test it and do the next step.

Get the data. Process the data. Act on the data. Repeat.

If you have trouble, post your questions in the programming subsection as the problem is your lack of programming knowledge, it’s not a project question.

chuckyx:
But from a first glance I would suspect that

float result;

will initialize result as 0.

You would be wrong about that. You get 0 for global and static variables, but for local variables if you don't give them a starting value then they start with whatever garbage was left over in that memory location when they are created.

He never gives it any other value anywhere in the code, so it just keeps comparing garbage to 1.6 and deciding what to do with the relays.

avr_fred:
It’s pretty obvious you lifted portions of this code from the web and threw it together and thought it would work. You need a lesson in variables. Local variables versus global variables and the scope of variables. You should not use subroutines until you understand variables and scope. You need to start over. No subroutines. Do one thing a time, test it and do the next step.

Get the data. Process the data. Act on the data. Repeat.

If you have trouble, post your questions in the programming subsection as the problem is your lack of programming knowledge, it’s not a project question.

Your post was very helpful in making me read about variables, I was checking for result instead of AmpsRMS. I just changed if (result>1.4) to if (AmpsRMS>1.4) and my device started working well. Thank you so much.