View previous topic :: View next topic |
Author |
Message |
bhas_r
Joined: 19 May 2009 Posts: 18
|
Difference between these two IF statements |
Posted: Mon Aug 29, 2022 9:32 am |
|
|
Hi all,
I am a little bit confused with the below statements:
IF ( (input (TNKH) == 1) && (input (TNKL) == 1) && (input (SUMP) == 0) )
IF ( (input (TNKH) == 1) & (input (TNKL) == 1) & (input (SUMP) == 0) )
I wrote the program from the above if statement I used the first one.
With one condition failing the loop executes and Motor always running.
I need all the inputs should be as equal to the value after the equal sign.
Looking for your great help to complete my project.
The program as below:
Code: |
#include <Deena Wlc.h>
/*
//OUTPUTS
RLY1 PIN_A0 //RELAY 1
SENPW PIN_A1 //SENSOR POWER ENABLE OUTPUT
MON PIN_A2 //MOTOR ON LED
//INPUTS
TNKH PIN_A3 //TANK HIGH INPUT
TNKL PIN_A4 //TANK LOW INPUT
MAN PIN_A5 //MANUAL SWITCH
//OUTPUTS
TNKFL PIN_C0 //TANK FULL LED
DRYRL PIN_C1 //DRY RUN OR SUMP EMPTY LED
RLY2 PIN_C2 //RELAY 2
//INPUTS
STOP PIN_C3 //STOP / RESET BUTTON
SUMP PIN_C4 //SUMP SENSOR INPUT
DRYR PIN_C5 //DRY RUN INPUT
*/
/*****************************************************************************/
unsigned INT8 mst, i;
void Autorun();
void Manrun();
/*****************************************************************************/
void main()
{
port_a_pullups (0xFF);
WHILE (TRUE)
{
//TODO: User Code
mst = read_eeprom (0);
output_high (SENPW);
delay_ms (2000);
//Manual start
IF (input (MAN) == 0)
{
Manrun ();
}
IF (input (STOP) == 0)
{
mst = 0;
write_eeprom (0, mst) ;
output_low (DRYRL);
output_low (MON);
output_low (RLY1);
}
//AUTO start
IF (mst == 0)
{
IF ( (input (TNKH) == 0) && (input (TNKL) == 0))
{
output_high (TNKFL);
//mst = 0;
}
else IF ( (input (TNKH) == 1) && (input (TNKL) == 0))
{
output_low (TNKFL);
}
else IF ( (input (TNKH) == 1) && (input (TNKL) == 1) && (input (SUMP) == 0) )
{
mst = 1;
write_eeprom (0, mst) ;
Autorun ();
write_eeprom (0, mst) ;
}
output_low (MON);
output_low (RLY1);
output_low (SENPW);
}
else IF (mst == 1)
{
mst = 1;
write_eeprom (0, mst) ;
Autorun ();
write_eeprom (0, mst) ;
}
else IF (mst == 2)
{
output_high (DRYRL);
while (input (STOP) == 1);
}
while ((mst == 3) && (input (STOP) == 1))
{
delay_ms (1000);
output_low (DRYRL);
delay_ms (500);
output_high (DRYRL);
}
delay_ms (30000);
}
}
/*****************************************************************************/
VOID Autorun ()
{
output_high (SENPW);
output_high (MON);
output_high (RLY1);
//mst = 1;
//Delay loop FOR dry running check
FOR (i = 0; i <= 6; i++)
{
delay_ms (30000);
}
WHILE ( (input (DRYR) == 0) && (input (TNKH) == 1) && (input (SUMP) == 0)) ;
IF (input (DRYR) == 1)
{
output_high (DRYRL);
mst = 2;
}
IF (input (SUMP) == 1)
{
output_high (DRYRL);
mst = 3;
}
IF ((input (TNKH) == 0) && (input (TNKL) == 0))
{
mst = 0;
}
output_low (SENPW);
output_low (MON);
output_low (RLY1);
}
/*****************************************************************************/
VOID Manrun ()
{
output_high (SENPW);
output_high (MON);
output_high (RLY1);
mst = 1;
//Delay loop FOR dry running check
FOR (i = 0; i <= 6; i++)
{
delay_ms (30000);
}
WHILE ( (input (DRYR) == 0) && (input (STOP) == 1) && (input (SUMP) == 0)) ;
IF (input (DRYR) == 1)
{
output_high (DRYRL);
mst = 2;
}
IF (input (SUMP) == 1)
{
output_high (DRYRL);
mst = 3;
}
IF (input (STOP) == 0)
{
mst = 0;
}
output_low (SENPW);
output_low (MON);
output_low (RLY1);
}
/*****************************************************************************/ |
_________________ Thanks and Regards
R.Bhaaskar |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Tue Aug 30, 2022 10:21 am |
|
|
& is 'bitwise and'. If you have two numbers, it generates the & of their
values. So:
1 & 3 gives 1
The result has the 1 where the same bit is on in both the values.
&& is 'logical and'.
It gives a TRUE only if the values given are logically TRUE.
Your original line will only execute it's associated statement if
all three tests are TRUE.
However I cannot see an 'else' for this statement that actually
stops the motor. Remember once it is on, it'll stay on till you turn it
off. |
|
|
bhas_r
Joined: 19 May 2009 Posts: 18
|
Thanks for the reply |
Posted: Wed Aug 31, 2022 5:56 am |
|
|
As you said the motor always run till the while statement true.
Code: | WHILE ( (input (DRYR) == 0) && (input (TNKH) == 1) && (input (SUMP) == 0));
|
One mistake i made in my program is I used MCLR pin in use as input without disabling the MCLR in fuses.
So my input is not valid in the program. Kindly see the below header file.
I will recheck my program as soon as possible after disabling the MCLR
and i will update the result.
Once again thanks for the reply.
Code: |
#include <16F676.h>
#device ADC=10
#use delay(internal=4MHz)
#use FIXED_IO( A_outputs=PIN_A2,PIN_A1,PIN_A0 )
#use FIXED_IO( C_outputs=PIN_C2,PIN_C1,PIN_C0 )
//OUTPUTS
#define RLY1 PIN_A0 //RELAY 1 CORR
#define SENPW PIN_A1 //SENSOR POWER ENABLE OUTPUT CORR
#define MON PIN_A2 //MOTOR ON LED CORR
//INPUTS
#define TNKH PIN_A3 //TANK HIGH INPUT corr
#define TNKL PIN_A4 //TANK LOW INPUT corr
#define MAN PIN_A5 //MANUAL SWITCH corr
//OUTPUTS
#define TNKFL PIN_C0 //TANK FULL LED CORR
#define DRYRL PIN_C1 //DRY RUN OR SUMP EMPTY LED CORR
#define RLY2 PIN_C2 //RELAY 2 CORR
//INPUTS
#define STOP PIN_C3 //UNUSED NOW STOP INPUT CORR
#define SUMP PIN_C4 //SUMP SENSOR INPUT corr
#define DRYR PIN_C5 //DRY RUN INPUT corr
|
_________________ Thanks and Regards
R.Bhaaskar |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Wed Aug 31, 2022 9:37 am |
|
|
I think that will read as '1' all the time. |
|
|
bhas_r
Joined: 19 May 2009 Posts: 18
|
While statement |
Posted: Wed Aug 31, 2022 10:10 am |
|
|
This is the program for water level controller.
When the water touches the High level sensor the input TNKH Senor reads zero.
Same for dry run and sump empty signal will goes to High.
Then while statement will read zero and loop will break.
After that IF statement writes the value to mst variable.
Depending upon mst variable valve, error or motor stops. _________________ Thanks and Regards
R.Bhaaskar |
|
|
bhas_r
Joined: 19 May 2009 Posts: 18
|
My code is working |
Posted: Fri Sep 02, 2022 12:47 am |
|
|
Happy to tell
My code is working as expected.
Soon i will post my full code. _________________ Thanks and Regards
R.Bhaaskar |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Mon Sep 05, 2022 1:15 am |
|
|
As a comment, the key I suspect here is to check that values are actually
arriving and readable by the pins you try to use.
Always worth simply not trying to do the actual control, but instead write
a basic debug program that shows what each pin is reading, and test this
before trying to code the logic. |
|
|
|