if statement issue

Hello
I am little a bit confused about if statement.

Summary
I am planning to connect to sensor, solenoid valve, Relay, LED’s. Measuring some voltage from their sensor output through ADC and turn LED off and on to indicate if test passed.
I use Arduino UNO V3.

Issues

  • my code is not working apart from faulty section and seems :frowning:.
  • And ADC is not very accurate

Steps:

  1. Check all voltage output from sensor and input before connected anything to sensors
  2. If all pass turn relay on to close valves
  3. After 5sec if voltage drop as desire turn green LED on
  4. If voltage does not drop turn RED led on
  5. After 10 sec regardless of pass or fail , turn everything off apart from blue LED to indicate ready for another test .
  6. Same cycle over and over

I attached my code and block diagram

Thanks

int Timer_led_blinking = 1000 ;    // LED- blinking Timer  
int Timer_Relay        = 10000 ;   //Relay ON/OFF
int Timer_display      = 2000 ;   // for serial monitor 
int Timer_Operation    = 5000 ;   //for LED to be on and off 
float Vout_inlet;                 // 
float Vout_outlet;                // 
float Voltage_Regulator;          // 
                    
const int Inlet_sensor_output      = A0 ; // inlet flow sensor
const int Outlet_sensor_output     = A1;  // outlet flow sensor
const int Voltage_Regulator_output = A2 ; // Regulator flow sensor

//----------------------  Pins setting ---------------------------------------

const int Green_led =  3 ;   /// Green Led for pass condition
const int Red_led   =  4 ;   /// Red Led for fail condition 
const int Yellow_led=  5 ;   //  Yellow Led for issue 
const int Blue_led  =  6 ;   //  Blue Led waiting for another unit to be tested 
const int Relay_pin =  8 ;   //  connected to transistor-- relay is On/off

//--------------------------Set limit-------------------------------

float Max_limit_upper = 2.7 ;     
float Max_limit_lower = 1.5 ;     
float Min_limit_upper = 0.8 ;     
float Min_limit_lower = 0.4 ;     
int   Zero_voltage    = 0   ;     
float Voltage_input   = 4.9 ;     


void setup() {
   Serial.begin(9600);
   pinMode(Green_led  ,OUTPUT);     
   pinMode(Red_led    ,OUTPUT);       
   pinMode(Yellow_led ,OUTPUT);
   pinMode(Relay_pin  ,OUTPUT);
}


void loop() { 
  
  Vout_inlet        = (analogRead(Inlet_sensor_output))*(5.0 / 1023.0);   
  Vout_outlet       = (analogRead(Outlet_sensor_output))*(5.0 / 1023.0);
  Voltage_Regulator = (analogRead(Voltage_Regulator_output))*(5.0 / 1023.0);

 
 Serial.println("Vout_inlet --> " + (String)Vout_inlet + " Volt" );
 Serial.println("Vout_inlet --> " + (String)Vout_outlet + " Volt" );
 Serial.println("Vout_inlet --> " + (String)Voltage_Regulator + " Volt" );
 delay (Timer_display);
 
 

///--------------------------------------------------------------------
//----------------  inital  check and fault detected    ---------------
//---------------------------------------------------------------------
 
if ((Vout_inlet <= Zero_voltage) || (Vout_outlet <= Zero_voltage)||(Voltage_Regulator <= Voltage_input)|| (Vout_inlet < Min_limit_lower) || (Vout_outlet <Min_limit_lower))

{  
   digitalWrite(Relay_pin,LOW);
   digitalWrite(Blue_led,LOW);
   digitalWrite(Green_led,LOW);
   digitalWrite(Red_led,LOW);  
   digitalWrite(Yellow_led, !digitalRead(Yellow_led));
   delay(Timer_led_blinking);
   Serial.println("fault detected");
  
   } 
else
{
    digitalWrite(Blue_led,HIGH);
    digitalWrite(Yellow_led,LOW);
    digitalWrite(Green_led,LOW);
    digitalWrite(Red_led,LOW); 
} 


//------------------------ Pass and fail condition -----------------------


if ((Vout_inlet >= Max_limit_lower) && (Vout_outlet >= Max_limit_lower))
{  
   delay(Timer_Relay);
   digitalWrite(Relay_pin,HIGH);
if ((Relay_pin== HIGH) &&(Vout_inlet <= Min_limit_upper ) && (Vout_outlet <= Min_limit_upper  )) 
        {
          digitalWrite(Green_led,HIGH);
          digitalWrite(Yellow_led,LOW);
          digitalWrite(Blue_led,LOW);
          digitalWrite(Red_led,LOW);
          Serial.println(         "  PASSED  "       );
          delay(Timer_Relay);
          digitalWrite(Relay_pin,LOW);
          digitalWrite(Green_led,LOW);
          digitalWrite(Blue_led,HIGH);   // turn Blue LED ready for next unit to be tested 
         }
        else 
         {
          digitalWrite(Relay_pin,HIGH);
          digitalWrite(Blue_led,LOW);
          digitalWrite(Yellow_led,LOW);         
          digitalWrite(Green_led,LOW);
          digitalWrite(Red_led,HIGH);
          Serial.println(            " FAILED "       );         
          delay(Timer_Operation);
          digitalWrite(Relay_pin,LOW);  
          digitalWrite(Red_led,LOW);
          digitalWrite(Blue_led,HIGH);   // turn Blue LED ready for next unit to be tested 
         }

}
}

my code is not working apart from faulty section and seems :frowning:.

So the faulty section is working? And the non-faulty sections are not working? Is that what you meant to say? Did you review what you typed before you clicked "Post"? It would really help those here who would like to help you.

    digitalWrite(Relay_pin, HIGH);
    if ((Relay_pin == HIGH) && (Vout_inlet <= Min_limit_upper ) && (Vout_outlet <= Min_limit_upper  ))

Your if statement is testing the pin number of Relay_pin NOT the value of the pin. But even if you fix that, what is the point of setting something HIGH and then instantly checking to see if it’s HIGH? What else could it be?

If you really want help you need to tell us more clearly what is working and exactly what doesn’t work.

Steve

slipstick:

    digitalWrite(Relay_pin, HIGH);

if ((Relay_pin == HIGH) && (Vout_inlet <= Min_limit_upper ) && (Vout_outlet <= Min_limit_upper  ))



Your if statement is testing the pin number of Relay_pin NOT the value of the pin. But even if you fix that, what is the point of setting something HIGH and then instantly checking to see if it's HIGH? What else could it be?

If you really want help you need to tell us more clearly what is working and exactly what doesn't work.

Steve

Thank you for your post and sorry it was not clear enough.

Section " inital check and fault detected " is working. when I disconnect power supply from one of valves yellow LED start blinking so far so good . But in case Relay_pin is High it won’t turn Relay_pin to LOW immediately until delay timer for relay finish although meantime yellow LED would blinking.

Section “Pass and Failed condition” is not working. always it jump to “else statement” even voltage are within range of pass condition it always show failed unit instead. Code always jump to else statement even I changed limits to lower but still showing fail condition.

what I want do , first check voltage from both inlet and outlet sensor , if they are within limit close Valve (using Relay_pin to high) then check voltage again while Valve is close and if sensor voltage from both inlet and outlet are within limit pass otherwise is failed . after 5 sec open valves and display blue LED do this process again and again.

samanshams:
then check voltage again while Valve is close

Nowhere in your code do you read the voltages again. You read them only once at the top of loop and use those values all the way through. If you want to check if they have changed you need to READ them again.

Steve

slipstick:
Nowhere in your code do you read the voltages again. You read them only once at the top of loop and use those values all the way through. If you want to check if they have changed you need to READ them again.

Steve

Thanks Steve for your time
I have two issue now .
I modify code again but still have some issue with “Pass and fail condition "
it always jump to " test failed” to be clear I change code to "if (Vout_inlet > Min_limit_upper && Vout_outlet > Min_limit_upper ) "
another issue
ADC is reading 3.2 V from inlet voltage output but my DMM is reading 2.66 V instead ( I use Keysight U1232A ).

I have attached block and flow chart to this post

Thanks again

int Timer_led_blinking = 1000 ;       // (milliseconds) LED- blinking Timer  
int Timer_Relay        = 10000 ;   // (milliseconds) Relay ON/OFF
int Timer_display      = 2000 ;   // (milliseconds) for serial monitor 
int Timer_Operation    = 5000 ;   // (milliseconds) for LED to be on and off 
float Vout_inlet;                 // 
float Vout_outlet;                // 
float Voltage_Regulator;          // 
                    
//-------------ADC  setting  -------------------------------------------


const int Inlet_sensor_output      = A0 ; // Reading output voltage --> inlet flow sensor
const int Outlet_sensor_output     = A1;  // Reading output voltage --> outlet flow sensor
const int Voltage_Regulator_output = A2 ; // Reading output votlage --> Regulator flow sensor

//----------------------  Pins setting ---------------------------------------

const int Green_led =  3 ;   /// Green Led for pass condition
const int Red_led   =  4 ;   /// Red Led for fail condition 
const int Yellow_led=  5 ;   //  Yellow Led for issue with test jig
const int Blue_led  =  6 ;   //  Blue Led waiting for another unit to be tested 
const int Relay_pin =  8 ;   //  connected to transistor in order to check relay is On/off

//--------------------------Set limit-------------------------------

float Max_limit_upper = 2.6 ;     // Check datasheet and measure output voltage 
float Max_limit_lower = 2.3   ;   // Check datasheet and measure output voltage 
float Min_limit_upper = 0.6 ;     // Check datasheet and measure output voltage 
float Min_limit_lower = 0.4 ;     // Check datasheet and measure output voltage 
int   Zero_voltage    = 0   ;     // Check voltage regulator output
float Voltage_input   = 4.8 ;     // output votlage regulator set to 5vdc +-0.1%

//// ------------------- void ------------------
void setup() {
   Serial.begin(9600);
   pinMode(Green_led  ,OUTPUT);     
   pinMode(Red_led    ,OUTPUT);       
   pinMode(Yellow_led ,OUTPUT);
   pinMode(Relay_pin  ,OUTPUT);
}

//------------- Reading and recording votlage-----------
void loop() { 
  
  Vout_inlet        = (analogRead(Inlet_sensor_output))*(5.0 / 1023.0);   
  Vout_outlet       = (analogRead(Outlet_sensor_output))*(5.0 / 1023.0);
  Voltage_Regulator = (analogRead(Voltage_Regulator_output))*(5.0 / 1023.0);

 // if you want to check result on serial monitor otherwise igonre this part
 // unit is "V"
 
 Serial.println("Vout_inlet --> " + (String)Vout_inlet + " Volt" );
 Serial.println("Vout_outlet --> " + (String)Vout_outlet + " Volt" );
 Serial.println("Vout_Voltage --> " + (String)Voltage_Regulator + " Volt" );
 delay (Timer_display);
 
 

///--------------------------------------------------------------------
//----------------  inital  check and fault detected    ---------------
//---------------------------------------------------------------------
 
if ((Vout_inlet <= Zero_voltage) || (Vout_outlet <= Zero_voltage)||(Voltage_Regulator <= Voltage_input)|| (Vout_inlet < Min_limit_lower) || (Vout_outlet <Min_limit_lower))
{  
   digitalWrite(Relay_pin,LOW);         
   digitalWrite(Blue_led,LOW);
   digitalWrite(Green_led,LOW);
   digitalWrite(Red_led,LOW);  
   digitalWrite(Yellow_led, !digitalRead(Yellow_led));
   delay(Timer_led_blinking);
   Serial.println("-----------------------");
   Serial.println("fault detected");
   Serial.println("-----------------------");
   
   } 
else
{
    digitalWrite(Blue_led,HIGH);
    digitalWrite(Yellow_led,LOW);
    digitalWrite(Green_led,LOW);
    digitalWrite(Red_led,LOW); 
} 

///--------------------------------------------------------------------
//-----------------------------------------------------------------------
//------------------------ Pass and fail condition -----------------------
//-----------------------------------------------------------------------


// when voltage output both inlet and outlet are higher than "2.6 V" turn relay on.
// 

if ((Vout_inlet >= Max_limit_lower) && (Vout_outlet >= Max_limit_lower))
      {  
      digitalWrite(Blue_led,LOW);
      digitalWrite(Yellow_led,LOW);
      digitalWrite(Relay_pin,HIGH);
      delay (5000);
            
// wait 5 sec and read votlage again 

      Vout_inlet        = (analogRead(Inlet_sensor_output))*(5.0 / 1023.0);   
      Vout_outlet       = (analogRead(Outlet_sensor_output))*(5.0/ 1023.0);
      Voltage_Regulator = (analogRead(Voltage_Regulator_output))*(5.0 / 1023.0);

      Serial.println("Vout_inlet_relay_on --> " + (String)Vout_inlet + " Volt" );
      Serial.println("Vout_outlet_relay_on --> " + (String)Vout_outlet + " Volt" );
      Serial.println("Vout_Voltage_relay_on --> " + (String)Voltage_Regulator + " Volt" );
      delay (5000);
      
// if vout from both inlet and outlet are less than " Min_limit_upper" ONLY turn GREEN LED on.
// then after 5 sec turn Green and relay off then turn blue led on. 
// otherwise istead of green led turn RED led on.
     
if (Vout_inlet < Min_limit_upper && Vout_outlet < Min_limit_upper  ) 
        { 
          digitalWrite(Blue_led,LOW);
          digitalWrite(Yellow_led,LOW);
          digitalWrite(Red_led,LOW);
          digitalWrite(Green_led,HIGH);
          Serial.println("-------------------------" ); 
          Serial.println(        "    test  "        );
          Serial.println(         "  PASSED  "       );
          Serial.println("-------------------------" ); 
          delay(Timer_Relay);
          digitalWrite(Relay_pin,LOW);
          digitalWrite(Green_led,LOW);
          digitalWrite(Blue_led,HIGH);   // turn Blue LED ready for next unit to be tested 
        }
if (Vout_inlet > Min_limit_upper  && Vout_outlet > Min_limit_upper ) 
        {
          
          digitalWrite(Blue_led,LOW);
          digitalWrite(Yellow_led,LOW);         
          digitalWrite(Green_led,LOW);
          digitalWrite(Red_led,HIGH);  
          Serial.println("--------------------------" ); 
          Serial.println(      "   test  "            );
          Serial.println(            " FAILED "       );
          Serial.println("--------------------------" ); 
          delay(Timer_Relay);
          digitalWrite(Relay_pin,LOW);  
          digitalWrite(Red_led,LOW);
          digitalWrite(Blue_led,HIGH);   // turn Blue LED ready for next unit to be tested 
          }
          
      }
}

What do you see when you print the values being tested ?
Are they what you expect ?

UKHeliBob:
What do you see when you print the values being tested ?
Are they what you expect ?

Thanks for your comment
these are valves I am expecting to get 2.3-2.5 V when valves is open
when valves is close 0.4-0.6 V from both inlet and outlet.
I attached block diagram and flow chart to previous post.
This is result when valves are open

13:33:44.212 -> Vout_inlet_relay_on --> 3.19 Volt   /// DMM reading 2.6
13:33:44.258 -> Vout_outlet_relay_on --> 2.71 Volt   /// DMM reading 2.4
13:33:44.258 -> Vout_Voltage_relay_on --> 4.99 Volt  /// 
13:33:45.237 -> Vout_inlet_relay_on --> 3.07 Volt    /// DMM reading 2.6
13:33:45.285 -> Vout_outlet_relay_on --> 2.65 Volt   /// DMM reading 2.4
13:33:45.331 -> Vout_Voltage_relay_on --> 4.99 Volt   /// 
13:33:46.311 -> --------------------------
13:33:46.311 ->    test  
13:33:46.311 ->  FAILED 
13:33:46.359 -> --------------------------

this is result I am getting when I block valves manually , it doesn’t show test pass neither GREEN led turn on

13:36:46.469 -> Vout_inlet --> 0.58 Volt
13:36:46.517 -> Vout_outlet --> 0.58 Volt
13:36:46.564 -> Vout_Voltage --> 4.99 Volt

samanshams:
Thanks for your comment
these are valves I am expecting to get 2.3-2.5 V when valves is open
when valves is close 0.4-0.6 V from both inlet and outlet.
I attached block diagram and flow chart to previous post.
This is result when valves are open

13:33:44.212 -> Vout_inlet_relay_on --> 3.19 Volt   /// DMM reading 2.6

13:33:44.258 -> Vout_outlet_relay_on --> 2.71 Volt   /// DMM reading 2.4
13:33:44.258 -> Vout_Voltage_relay_on --> 4.99 Volt  ///
13:33:45.237 -> Vout_inlet_relay_on --> 3.07 Volt    /// DMM reading 2.6
13:33:45.285 -> Vout_outlet_relay_on --> 2.65 Volt   /// DMM reading 2.4
13:33:45.331 -> Vout_Voltage_relay_on --> 4.99 Volt   ///
13:33:46.311 -> --------------------------
13:33:46.311 ->    test  
13:33:46.311 ->  FAILED
13:33:46.359 -> --------------------------





this is result I am getting when I block valves manually , it doesn't show test pass neither GREEN led turn on





13:36:46.469 -> Vout_inlet --> 0.58 Volt  \ this is valve I am expecting to get
13:36:46.517 -> Vout_outlet --> 0.58 Volt    \ this is valve I am expecting to get
13:36:46.564 -> Vout_Voltage --> 4.99 Volt    \ this is valve I am expecting to get

I don't know enough about your project and cannot really make any sense of your readings. Therefore a simple question. Are you getting the voltage readings that you expect ?

One thing that you should take note of is that the Uno has only one ADC which is used by all of the analogue inputs. There have been suggestions in many forum posts that you should do 2 analogRead()s from the pins, discarding the first one, when reading multiple analogue pins in succession to allow the value to settle. It would be worth trying this as it is easy to do. Note that I have no proof that successive readings of different pins is actually a problem

UKHeliBob:
I don't know enough about your project and cannot really make any sense of your readings. Therefore a simple question. Are you getting the voltage readings that you expect ?

One thing that you should take note of is that the Uno has only one ADC which is used by all of the analogue inputs. There have been suggestions in many forum posts that you should do 2 analogRead()s from the pins, discarding the first one, when reading multiple analogue pins in succession to allow the value to settle. It would be worth trying this as it is easy to do. Note that I have no proof that successive readings of different pins is actually a problem

Simple answer I don't get result I am looking for from ADC.
Yes I found some article about it , I will try them . Also I am planning to divide project into sub-section to check where problem is and fix it /
Thanks

Thank you all for help

Finally my code start working and found issue .

But only issue I have is ADC which is not accurate . when there is no air flow ADC is working fine and no issue with reading but as soon as air flow to sensor voltage reading from ADC is not accurate compared with DDM and this effect code and result.

so part I am using is D6F-P0001A1.
I changed arduino UNO to arduino Mega 2560 , still have same issue .
Also I tried delay() between each ADC reading and this didn't work even changed timing from 5 to 50.

      Vout_inlet        = (analogRead(Inlet_sensor_output))*(5.0 / 1023.0);   
      delay(10);
       Vout_outlet       = (analogRead(Outlet_sensor_output))*(5.0 / 1023.0);
      delay(10);

Any suggestion ? Should I use Ardunio DUE ?

Thank you very much
Saman

Finally my code start working and found issue .

What was the issue ?
Please post the full working code. It may help someone in the future

ADC which is not accurate . when there is no air flow ADC is working fine and no issue with reading but as soon as air flow to sensor voltage reading from ADC is not accurate compared with DDM

Write a simple sketch that only reads one analogue input, prints the raw value and the computed voltage. Does the raw value vary as expected under different conditions and is the calculated voltage correct for the different conditions ?

UKHeliBob:
What was the issue ?
Please post the full working code. It may help someone in the future
Write a simple sketch that only reads one analogue input, prints the raw value and the computed voltage. Does the raw value vary as expected under different conditions and is the calculated voltage correct for the different conditions ?

Thank you for your suggestion
I use very simple code to read voltage even my wires are not connected anywhere they reading voltage .
how can I fix this issue ? thanks
14:44:53.394 -> 1.84
14:44:54.418 -> 1.60
14:44:55.399 -> 1.38
14:44:56.378 -> 1.37
14:44:57.405 -> 1.52

/*
  ReadAnalogVoltage

  Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
  delay(1000);
}

I use very simple code to read voltage even my wires are not connected anywhere they reading voltage .

No surprise there as the inputs are not being held at a known input voltage, rather they are floating at an unknown voltage. Try touching the pins with your finger and watch them change. However, if you have no wires connected to them then why read from them ?

You can fix the problem by using a pullup or pulldown resistor to hold them at either 5V or GND or using 2 resistors at any voltage between 5V and GND

UKHeliBob:
No surprise there as the inputs are not being held at a known input voltage, rather they are floating at an unknown voltage. Try touching the pins with your finger and watch them change. However, if you have no wires connected to them then why read from them ?

You can fix the problem by using a pullup or pulldown resistor to hold them at either 5V or GND or using 2 resistors at any voltage between 5V and GND

Thank you for your help.
I compared my code with one reading voltage
my code difference between each reading is ( +- 0.5 mv)
if I do one reading ( +- 0.3mv)
But I need two reading because they are comparing . do you think arduino DUE will fix this issue ? or I need ADC or something else
thanks

my code difference between each reading is ( +- 0.5 mv)

A 1 millivolt variation is not bad when you consider that the Uno ADC can only return 1024 different values (10 bits)

The Due has a 12 bit ADC so can return 4096 different values but I have no experience of using it