float comparison in IF statement with Logically Checks

Hello Team

i am new in arduino.

i am making a program to get the value that is Float Number and comparing it in a IF statement
with Two more Logical Checks

but IF statement is executing the first check and then second check.

MY ADC value is not going below the LowAC, but that condition become true .

kindly suggest.

float minAC = 200.00;
float maxAC = 250.00;
float LowAC = 198.00;
float AC_Volts =0.0;

AC_Volts i am getting from an ADC.

if (LowAC >= AC_Volts and Var_Gen1 == HIGH and Var_KE1 == LOW)
{

digitalWrite (Gen_Relay, LOW);

digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);

}

else if (AC_Volts >= minAC and AC_Volts <= maxAC and Var_Gen1 == HIGH and Var_KE1 == LOW)
{

delay(One_Min);

digitalWrite (Gen_Relay, HIGH);
delay(2000);
}

regards
NASIR

i am making a program

...but posting only a snippet of it, badly.

try and avoid floats if you can - how does your A/D send it’s data and in what form ?
So integer calcs would be more efficient and possibly simpler.
Either work in terms of A/D values or in 10* the voltage and generate the decimal point at the end ( if needed).

Are all the variables in your “IF” routine of the same type ( all floats?)

Hello nashfaq1981

It's impossible to answer you without ALL your ACTUAL code.

Regards,
bidouilleelec

Hello,

float minAC = 200.00;
float maxAC = 250.00;
float LowAC = 198.00;
float  AC_Volts =0.0;

AC_Volts i am getting from an ADC.

  if (LowAC >= AC_Volts and Var_Gen1 == HIGH and Var_KE1 == LOW)
           {
         
       
          digitalWrite (Gen_Relay, LOW); 
     
           digitalWrite(ledPin, HIGH);
           delay(200);
           digitalWrite(ledPin, LOW);
           delay(200);
           
         
           }

 
        else if (AC_Volts >= minAC and AC_Volts <= maxAC and Var_Gen1 == HIGH and Var_KE1 == LOW)
           {
           
           delay(One_Min);
         
           digitalWrite (Gen_Relay, HIGH);
           delay(2000);
            }

*use </> (code) function on left side toolbars to fix your post

The program you have posted will not compile at all. There is no "executing the first check" - it wont even compile.

You are not getting any value AC_Volts from any source other than where you declare AC_Volts and set it to 0 in the code you have posted.
NB - if you are going to use analogRead(), to get AC_Volts, eliminate the floats and floating point math, as you will only have 1024 possible values, all your checks can be done with an int or unsigned int.

here is the complete code.

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance

int i = 0;
float tenvals = 0.0;
float minval = 1000;
float maxval = 0.0;

int ledPin = 13; // select the pin for the LED

float minAC = 200.00;
float maxAC = 250.00;
float LowAC = 198.00;
float AC_Volts =0.0;

// Relays Defining//
const int KE_NO = 5; // white
const int KE_NC = 6; // Red

const int Gen_NO = 3; //Orange
const int Gen_NC = 4; //yellow

const int KE_Relay = 9; //Green
const int Gen_Relay = 10; //Orange
const int Gen_OFF = 11; // Yellow

int Var_Gen1 = 0;
int Var_KE1 = 0;

int Var_Gen2 = 0;
int Var_KE2 = 0;

// Time Defining//
const long oneSecond = 500;
const long One_Min = oneSecond * 60;
const long Half_Min = oneSecond * 30;

void setup()
{
Serial.begin(115200);

emon1.voltage(A2, 176, 1.7); // Voltage: input pin, calibration, phase_shift
// emon1.current(1, 111.1); // Current: input pin, calibration.
pinMode(ledPin, OUTPUT);

pinMode(KE_NO, INPUT);
pinMode(Gen_NO, INPUT);

pinMode(KE_NC, INPUT);
pinMode(Gen_NC, INPUT);

pinMode(KE_Relay, OUTPUT);
pinMode(Gen_Relay, OUTPUT);

pinMode(Gen_OFF, OUTPUT);

}

void loop()
{
emon1.calcVI(20,2000); // Calculate all. No.of half wavelengths (crossings), time-out
// emon1.serialprint(); // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)

// float realPower = emon1.realPower; //extract Real Power into variable
// float apparentPower = emon1.apparentPower; //extract Apparent Power into variable
// float powerFActor = emon1.powerFactor; //extract Power Factor into Variable
// float supplyVoltage = emon1.Vrms; //extract Vrms into Variable
// float Irms = emon1.Irms; //extract Irms into Variable

float Vrms = (emon1.Vrms - 3);
if (Vrms < 0) { Vrms = 0.0; }

tenvals += Vrms;
if (minval > Vrms) { minval = Vrms; }
if (maxval < Vrms) { maxval = Vrms; }

AC_Volts = tenvals/10 ;

i++;

if (i == 10)
{
Serial.print("Avg: ");
Serial.print(tenvals/10);
Serial.print(" (");
Serial.print(Vrms);
Serial.print(") Min: ");
Serial.print(minval);
Serial.print(" Line_AC: ");
Serial.println(AC_Volts);
Serial.print(" Max: ");
Serial.println(maxval);

i = 0;
tenvals = 0.0;
minval = 1000.0;
maxval = 0.0;
}
delay(100);

//int val; // The value coming from the sensor
// float Avgfloat = tenvals/10;
// The mapped value

// val = analogRead(Analog_IN); // read the voltage on the pot (val ranges from 0 to 1023)
// AC_Volts = map(val, 0, 1023, 0, 250); // percent will range from 0 to 100.

int Var_Gen1 = digitalRead(Gen_NO); // Gen Contactor is Energized
int Var_KE1 = digitalRead(KE_NO);

// ---------------- GEN POWER WITH LOW GRID VOLTAGE ---------//

if (LowAC >= AC_Volts and Var_Gen1 == HIGH and Var_KE1 == LOW)
{

digitalWrite (Gen_Relay, LOW); // Gen Contactor De-Energized 10

digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(200);
digitalWrite(ledPin, LOW); // turn the ledPin on
delay(200);

}

// ----------------- GEN POWER WITH NORMAL GRID POWER----------//

else if (AC_Volts >= minAC and AC_Volts <= maxAC and Var_Gen1 == HIGH and Var_KE1 == LOW)
{

delay(One_Min);

digitalWrite (Gen_Relay, HIGH); // Gen Contactor De-Energized 10
delay(2000);
}

//---------------------- GEN CONTROL END----------------------//

int Var_Gen2 = digitalRead(Gen_NO); // Confirms Gen Contactor is De-Energized
int Var_KE2 = digitalRead(KE_NO);

//----------------- GRID POWER CONTROL WITH NORMAL GRID VOLTAGE------------------//

if (AC_Volts >= minAC and AC_Volts <= maxAC and Var_KE2 == LOW and Var_Gen2 == LOW )
{

delay(Half_Min);
digitalWrite(KE_Relay, HIGH); // Turn ON the KE Contactor 9
// delay(2000);
}

if (LowAC >= AC_Volts and Var_KE2 == HIGH )
{
digitalWrite (KE_Relay, LOW); // if Volatge are lower then the specified Turn OFF KE

digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(200);
digitalWrite(ledPin, LOW); // turn the ledPin on
delay(200);

}

//--------------------GRID POWER CONTROL END--------------//

}

Hello nashfaq1981

if (LowAC >= AC_Volts and Var_Gen1 == HIGH and Var_KE1 == LOW)

If I understand, the test below is true when you expect it to be false?

MY ADC value is not going below the LowAC

1/ did you make a Serial.print of LowAC and AC_Volts?

2/ and what about Var_Gen1 and Var_KE1 ?

Regards,
bidouilleelec

if (LowAC >= AC_Volts and Var_Gen1 == HIGH and Var_KE1 == LOW)

if I understand, the test below is true when you expect it to be false?

Yes, as my ADC output is higher every time, i.e 210 Volts

second BAD thing i noticed that, when connect the UNO with Computer then i am getting the result of the Second Phase,

// ----------------- GEN POWER WITH NORMAL GRID POWER----------//

else if (AC_Volts >= minAC and AC_Volts <= maxAC and Var_Gen1 == HIGH and Var_KE1 == LOW)

if UNO is working independently this check become false.

thats means that the AC_Volts only getting values when communication is established.

please suggest on this issue.

regards
Nasir

Hello

nashfaq1981:
if (LowAC >= AC_Volts and Var_Gen1 == HIGH and Var_KE1 == LOW)

if I understand, the test below is true when you expect it to be false?

Yes, as my ADC output is higher every time, i.e 210 Volts

How do you measure (or know the value) of AC_Volts ?

second BAD thing i noticed that, when connect the UNO with Computer then i am getting the result of the Second Phase,

// ----------------- GEN POWER WITH NORMAL GRID POWER----------//

else if (AC_Volts >= minAC and AC_Volts <= maxAC and Var_Gen1 == HIGH and Var_KE1 == LOW)

if UNO is working independently this check become false.

thats means that the AC_Volts only getting values when communication is established.

please suggest on this issue.

regards
Nasir

What do you mean by connect the UNO with Computer and UNO is working independently?

Regards,
bidouilleelec

hello

How do you measure (or know the value) of AC_Volts ?

// EmonLibrary examples openenergymonitor.org, the code for monitoring the Voltage.

https://forum.arduino.cc/index.php?topic=357095.0

What do you mean by connect the UNO with Computer and UNO is working independently?

i looks that when i am monitoring data via ARDUINO IDE the behavior of the logic is good.
but when i disconnect and reset the UNO so it will not follow the code.

regards
NASIR