If statement & Serial.println reading const int incorrectly

int PSU_12V = A0;
int BUCK_12V = A1;
int COMB_OUT = A2;
int BATT_BUCK = A3;
int BATT_VOLT = A4;

int PSU_12V_MONITOR = 0;
int BUCK_12V_MONITOR = 0;
int COMB_OUT_MONITOR = 0;
int BATT_BUCK_MONITOR = 0;
int BATT_VOLT_MONITOR = 0;


// voltage Min
int PSU_12V_0 = 11.5;
int BUCK_12V_1 = 3.6;
int COMB_OUT_2 = 3.6;
int BATT_BUCK_3 = 3.6;
int BATT_VOLT_4 = 11.5;


// voltage Max ***************  SET TO 4.3
const int BUCK_12V_MAX_1 = 4.3;
const int BATT_BUCK_MAX_3 = 4.3;

const int buzzer_pin = 2;
const int Volt0_LED = 3;
const int Volt1_LED = 4;
const int Volt2_LED = 5;
const int Volt3_LED = 6;
const int Volt4_LED = 7;
//const int Volt5_LED = 8;

const int RELAY_R6 = 9;
const int RELAY_R7 = 10;
const int RELAY_OVER_VOLT_LIGHT = 11;

bool BUZZER_FAIL_ACTIVE = false;
bool OVER_VOLT_12V_ACTIVE = false;
bool OVER_VOLT_BATT_ACTIVE = false;

bool BUCK_12V_LOW = false;
bool BATT_BUCK_12V_LOW = true;




void setup()  //-------------------------  SETUP
{
   // Setup Serial Monitor
   Serial.begin(9600);
   Serial.println("DC Voltage Test *********************************************");

   pinMode(buzzer_pin, OUTPUT);
   pinMode(Volt0_LED, OUTPUT);
   pinMode(Volt1_LED, OUTPUT);
   pinMode(Volt2_LED, OUTPUT);
   pinMode(Volt3_LED, OUTPUT);
   pinMode(Volt4_LED, OUTPUT);
 //  pinMode(Volt5_LED, OUTPUT);
 /*  
   digitalWrite(buzzer_pin, LOW);
   digitalWrite(Volt0_green, LOW);
   digitalWrite(Volt1_green, LOW);
   digitalWrite(Volt2_green, LOW);
   digitalWrite(Volt3_green, LOW);
   digitalWrite(Volt4_green, LOW);
  // digitalWrite(Volt5_green, LOW);
*/

   pinMode(RELAY_R6, OUTPUT);
   pinMode(RELAY_R7, OUTPUT);
   pinMode(RELAY_OVER_VOLT_LIGHT, OUTPUT);
   digitalWrite(RELAY_R6, HIGH);
   digitalWrite(RELAY_R7, HIGH);
   digitalWrite(RELAY_OVER_VOLT_LIGHT, HIGH);

 delay(1000);
}

void loop() {

  BUZZER_FAIL_ACTIVE = false;
// INPUT 0
int PSU_12V_MONITOR = analogRead(PSU_12V);
float in_voltage  = PSU_12V_MONITOR*(15/1023.0);

// Print results to Serial Monitor to 2 decimal places
  Serial.print("Input Voltage0 = ");
  Serial.println(in_voltage, 2);

// INPUT 1
int BUCK_12V_MONITOR = analogRead(BUCK_12V);
float in_voltage1  = BUCK_12V_MONITOR*(5/1023.0);

// Print results to Serial Monitor to 2 decimal places
  Serial.print("Input Voltage1 = ");
  Serial.println(in_voltage1, 2);

  // INPUT 2
int COMB_OUT_MONITOR = analogRead(COMB_OUT);
float in_voltage2  = COMB_OUT_MONITOR*(5/1023.0);

// Print results to Serial Monitor to 2 decimal places
  Serial.print("Input Voltage2 = ");
  Serial.println(in_voltage2, 2);

  // INPUT 3
int BATT_BUCK_MONITOR = analogRead(BATT_BUCK);
float in_voltage3  = BATT_BUCK_MONITOR*(5/1023.0);

// Print results to Serial Monitor to 2 decimal places
  Serial.print("Input Voltage3 = ");
  Serial.println(in_voltage3, 2);

  // INPUT 4
int BATT_VOLT_MONITOR = analogRead(BATT_VOLT);
float in_voltage4  = BATT_VOLT_MONITOR*(15/1023.0);

// Print results to Serial Monitor to 2 decimal places
  Serial.print("Input Voltage4 = ");
  Serial.println(in_voltage4, 2);

//-----------------------------------------------------------------IF_STATEMENTS--------


// 12V PSU VOLT CHECK
if (in_voltage >= PSU_12V_0) { // GOOD
    digitalWrite(Volt0_LED, LOW);
  } else if(in_voltage <= PSU_12V_0) { // LOW VOLT
    Serial.print("VOLTAGE0 LOW   ");
    digitalWrite(Volt0_LED, HIGH);
    if (BUZZER_FAIL_ACTIVE == false) {
    BUZZER_FAIL();
  }}

// 12V PSU BUCK REGULATOR CHECK

if (in_voltage1 > BUCK_12V_MAX_1, 2) {  // ---------- OVER VOLT  set to: >= (Greater than)!! 
    Serial.print("BUCK_12V OVER-VOLT TRIGGER**  ");
    Serial.print(BUCK_12V_MAX_1, 2);
    Serial.print(" ");
    OVER_VOLT_12V(); }
    
//---------
   if(in_voltage1 < BUCK_12V_1 && OVER_VOLT_12V_ACTIVE == false ) { // LOW VOLTAGE
    Serial.print("VOLTAGE1 LOW  ");
    BUCK_12V_LOW = true;
    digitalWrite(Volt1_LED, HIGH);
    if (BUZZER_FAIL_ACTIVE == false);
    BUZZER_FAIL(); }
    else{
      BUCK_12V_LOW = false;
    }
    
   if (BUCK_12V_LOW == false &&  OVER_VOLT_12V_ACTIVE == false ){                               // GOOD
      Serial.print("BUCK_12V_1 GOOD ");
      OVER_VOLT_12V_ACTIVE = false;
    digitalWrite(Volt1_LED, LOW); }
    
   
    

  
// MAIN LINK CHECK  
  if (in_voltage2 >= COMB_OUT_2) {
    digitalWrite(Volt2_LED, LOW);
  } else if(in_voltage2 <= COMB_OUT_2) { // LOW VOLT
    Serial.print("VOLTAGE2 LOW   ");
    digitalWrite(Volt2_LED, HIGH);
        BUZZER_TOTAL_POWER_FAIL();
  }

// BATT BUCK REGULATOR CHECK
 
    
  if(in_voltage3 <= BATT_BUCK_3) { // LOW VOLT
    Serial.print("VOLTAGE3 LOW   ");
    BATT_BUCK_12V_LOW == true;
    digitalWrite(Volt3_LED, HIGH);
    if (BUZZER_FAIL_ACTIVE == false) {
    BUZZER_FAIL();
  }}
  else{                         // GOOD
    digitalWrite(Volt3_LED, LOW);
    Serial.print("VOLTAGE3 OK   ");}

// --- Needs over-volt check

    

// 12V BATT CHECK
  if (in_voltage4 >= BATT_VOLT_4) {
    digitalWrite(Volt4_LED, LOW);
  } else if(in_voltage4 <= BATT_VOLT_4) { // LOW VOLT
    Serial.print("VOLTAGE4 LOW   ");
    digitalWrite(Volt4_LED, HIGH);
    if (BUZZER_FAIL_ACTIVE == false) {
    BUZZER_FAIL();
  }
  }



}// end of loop

//-----------  Sub-programs


void BUZZER_FAIL(){
Serial.print("BUZZER_FAIL Acive!  ");
BUZZER_FAIL_ACTIVE = true;
    digitalWrite(buzzer_pin, HIGH);
    delay(2000); 
    digitalWrite(buzzer_pin, LOW);
}

void BUZZER_TOTAL_POWER_FAIL(){
Serial.print("BUZZER_TOTAL_POWER_FAIL Acive!");
    digitalWrite(buzzer_pin, HIGH);
    delay(5000); 
    digitalWrite(buzzer_pin, LOW);
}

void OVER_VOLT_12V(){
Serial.print("12V REG OVER_VOLT Acive!(1)  ");
    OVER_VOLT_12V_ACTIVE = true;
    digitalWrite(Volt1_LED, HIGH);
    digitalWrite(buzzer_pin, HIGH);
    digitalWrite(RELAY_R6, LOW);
    digitalWrite(RELAY_OVER_VOLT_LIGHT, LOW);
    delay(2000); 
    digitalWrite(buzzer_pin, LOW);    
}

void OVER_VOLT_BATT(){
Serial.print("BATT REG OVER_VOLT Acive!");
    OVER_VOLT_BATT_ACTIVE = true;
    digitalWrite(buzzer_pin, HIGH);
    digitalWrite(RELAY_R7, LOW);
    digitalWrite(RELAY_OVER_VOLT_LIGHT, LOW);
    delay(2000); 
    digitalWrite(buzzer_pin, LOW);    
}

I believe my problem is within this part:

const int BUCK_12V_MAX_1 = 4.3;
const int BATT_BUCK_MAX_3 = 4.3; 

if (in_voltage1 > BUCK_12V_MAX_1, 2) {  // ---------- OVER VOLT  set to: 
    Serial.print("BUCK_12V OVER-VOLT TRIGGER**  ");
    Serial.println(BUCK_12V_MAX_1, 2);
    Serial.print(" ");
    OVER_VOLT_12V(); } 

The if statement triggers although the voltage input is 4.2v and the Serial.PrintIn reads "100"

Any suggestions?

Thanks!

if (in_voltage1 > BUCK_12V_MAX_1, 2)

What do you think this line of code is doing ?
Why are there 2 parameters on the right of the >

Checking if "in_voltage1" is greater than "BUCK_12_MAX_1".

I added the 2 for it to check 2 decimal points.

this does not work like this in c++.

read about the Built-in comma operator that you used ➜ Other operators - cppreference.com

basically you compare in_voltage1 with 2

just get rid of the , 2 if you want to compare the 2 numbers. if you want to round them to 2 decimal digits, then you have to do so in your code first

1 Like

Just as I thought, hence my question

Oh ok, Thanks for the info!
I will have a read through that!

I've removed the "2" but the Serial.read still reads the int as "100"

you store a floating point variable into an int, so BUCK_12V_MAX_1 is actually 4

use the right types

// voltage Min
const float PSU_12V_0 = 11.5;
const float BUCK_12V_1 = 3.6;
const float COMB_OUT_2 = 3.6;
const float BATT_BUCK_3 = 3.6;
const float BATT_VOLT_4 = 11.5;


// voltage Max ***************  SET TO 4.3
const float BUCK_12V_MAX_1 = 4.3;
const float BATT_BUCK_MAX_3 = 4.3;

const byte buzzer_pin = 2;
const byte Volt0_LED = 3;
const byte Volt1_LED = 4;
const byte Volt2_LED = 5;
const byte Volt3_LED = 6;
const byte Volt4_LED = 7;

you also declare a global and a local variable

int PSU_12V_MONITOR = 0;

that's the global one

and then

  int PSU_12V_MONITOR = analogRead(PSU_12V); // <===== DECLARE IT AGAIN
  float in_voltage  = PSU_12V_MONITOR * (15 / 1023.0);
1 Like

That solved the problem!
I obviously need to do more reading on C++ !

Thank you for the help!

great - have fun (indeed building solid foundations helps develop good applications)

You removed the first "2" but not the one in the Serial.println(). When you are printing a 'float' value, the ", 2)" means "print rounded to two decimal places" but when you are printing an integer, the ", 2)" is the same as ", BIN)" which means "print in binary". The value '4' in binary is One Zero Zero ("100").

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.