Maker Pro
Maker Pro

Making two if Statements work at the same time

Lei Reyes

Jul 2, 2014
60
Joined
Jul 2, 2014
Messages
60
Hi everyone, just want to ask how am I suppose to make two if statements to operate at the same time?
I mean in this setup,

there are two level sensors that act as input,
if they are triggered, they will trigger their corresponding pumps.
however, the result is this;

the if statement #2 does not operate while the if statement 1 is satisfied/triggered.
how can I make these two statements independent from each other?


Code:
const int level1 = 2;
const int level2 = 3;
const int pump1 = 4;
const int pump2 = 5;

void setup()
{

  pinMode(level1, INPUT_PULLUP);
  pinMode(level2, INPUT_PULLUP);
  pinMode(pump1, OUTPUT);
  pinMode(pump2, OUTPUT);
}

void loop()
{
  int state1 = digitalRead(level1);
  int state2 = digitalRead(level2);
 
    if(state1==0)
    {
      digitalWrite(pump1, HIGH);
    }
    else
    {
    digitalWrite(pump1, LOW);
   
    if(state2==0)
    {
      digitalWrite(pump2, HIGH);
    }
    else
    {
      digitalWrite(pump2, LOW);
    }
  
   
  }
 
 
 
}
 

Lei Reyes

Jul 2, 2014
60
Joined
Jul 2, 2014
Messages
60
Here's the ifs #1 and #2
 

Attachments

  • IFS.png
    IFS.png
    248 KB · Views: 149

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
You have omitted the closing brace from your first if() block, so the second if() block is actually part of the else clause of the first block.

Code:
    if(state1==0)
    {
        digitalWrite(pump1, HIGH);
    }
    else
    {
        digitalWrite(pump1, LOW);
    } <----------------- you need to add this closing brace here!
    if(state2==0)
    {
        digitalWrite(pump2, HIGH);
    }
    else
    {
        digitalWrite(pump2, LOW);
    }
 

Merlin3189

Aug 4, 2011
250
Joined
Aug 4, 2011
Messages
250
I think the answer to your original question may simply be, you can't! If you have to do two separate tests then they can't be done together. Equally, if you have to have two separate output commands, then they have to be done sequentially.

However, if both sensors can be read by reading a single register (say the sensors are represented as particular bits of an input register, which appears to be the case here) then you could test them both at once, using logical tests.
In fact, since finding a 0 on a sensor bit means we want a 1 on the corresponding pump bit, we don't even need to ask any questions - just invert the input bits and send them to the output.
So your bit of code becomes something like this: (no particular language. I've used AND, OR and NOT rather than cryptic C symbols. I assume there will be a function to read and write a port register. /* and */ are start and end of comments. etc! E&OE)

const int level1 = 2;
const int level2 = 3;
const int pump1 = 4;
const int pump2 = 5;
const int port = 0x0308; /* or whatever value this should be */
const int sensormask = 0x30; /* =00110000 to select level sensor bits */
const int pumpmask = 0x03; /* =11000011 to select other bits of port */

void setup()
{
pinMode(level1, INPUT_PULLUP);
pinMode(level2, INPUT_PULLUP);
pinMode(pump1, OUTPUT);
pinMode(pump2, OUTPUT);
}
void loop()
{
int outstate = 0; /* initialise value that will be sent to port */
int instate = ReadPort(port); /* bits 2 and 3 of the port register will be level1 and level2 */
outstate = NOT instate; /* invert bits, because we want low in -> high out */
outstate = outstate AND sensormask; /* isolate bits 2 and 3 */
outstate =outstate + outstate + outstate + outstate; /* shift bits two places up
so that the sensor bits are now in the pump bits place at 4 and 5 */
outstate = outstate OR (instate AND pumpmask);
/* set bits 0,1,6 and 7 to original state so that you don't change anything else*/
WritePort(port, outstate); /* set the bits of the port to updated values */
}

Since each sensor affects only the corresponding pump, I can't see why you would worry about taking the decisions simultaneously? Your code is going to be in some kind of loop to check the levels regularly at intervals longer than the few microseconds between sequential ifs. If the loop interval is an acceptable delay, why is the much shorter sequential processing delay unacceptable?
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Merlin3189, I see what you're saying, but look at his original code, and especially, the indentation. And read his problem description carefully. The problem is that the second if() statement is only executed if the first if() evaluates to false, and the reason is just that he's missed a closing brace as I showed in post #4.
 
Top