if statement

Thread Starter

ecka333

Joined Oct 1, 2009
76
I im trying to write such condition in C:
Rich (BB code):
if(j==1)
    {
    if(PumpEnabled[0]==1&&Fert1VolPassed<NeedFert1Amount){PORTC.B5=1;} 
    else {PORTC.B5=0; j=2;}
    }
Problem is that PumpEnabled[0] variable must be checked only once. When microcontroller executes code second, third and etc.. time, condition PumpEnabled[0]==1 must be omitted. Tried to check PumpEnabled[0] variable only once with the help of for(i=0;i<1;i++) construction, but compiler gave me error. How how to write the code?
 

John P

Joined Oct 14, 2008
2,026
Sounds like you have a little state machine going there. How about
Rich (BB code):
switch(j)
{
  case 0:
    break;                      // As before, don't do anything if j == 0
  case 1:
    if (PumpEnabled[0]==1)
    {
      PORTC.B5=1;
      j = 2;
    }
    break;
  case 2:
    if (Fert1VolPassed<NeedFert1Amount)
      PORTC.B5=1;
    else 
    {
      PORTC.B5=0;
      j=3;
    }
    break;
  default:
    break;
}
 

WBahn

Joined Mar 31, 2012
30,045
That's not the same logic. For instance, what happens if (PumpEnabled[0]==1) is TRUE but (Fert1VolPassed<NeedFert1Amount) is FALSE? The original code sets the bit LO but the offered alternative sets it HI.

@ecka333: What you need is not clearly stated due to ambiguities in the english language. When you say that PumpEnabled[0] must be checked only once, are you saying that it is sufficient to only check it once, but okay to check if multiple times (perhaps not desirable, but valid), or are you saying that it must be checked exactly once and if it is checked in subsequent passes the results are not valid?

Assuming you check it only once, are you then saying that subsequent passes need to use the value from that first check, or that in subsequent passes it doesn't matter what the value is or was?
 

John P

Joined Oct 14, 2008
2,026
WBahn is right of course. That problem can be dealt with by testing for both conditions, as before:

Rich (BB code):
 case 1:
    if ((PumpEnabled[0]==1) && (Fert1VolPassed<NeedFert1Amount))
Even though it takes more screen space to show it, I like the switch:case construction, as it lets you get a clear view of how the logic for each state functions.

I also think the code becomes more readable if you leave spaces and add parentheses even if they aren't necessary.
 

Thread Starter

ecka333

Joined Oct 1, 2009
76
@WBahn: I mean, that variable PumpEnabled[0] must be checked only once. If it is equal to 1, subsequent passes need to use the value from that first check(1).
@John P: your routine is a litle complicated, maybe there is simplier way to omit PumpEnabled[0] variable checking?
 

WBahn

Joined Mar 31, 2012
30,045
@WBahn: I mean, that variable PumpEnabled[0] must be checked only once. If it is equal to 1, subsequent passes need to use the value from that first check(1).
@John P: your routine is a litle complicated, maybe there is simplier way to omit PumpEnabled[0] variable checking?
Then simply store the result in another variable. Since we are only seeing this code snippet, it is hard to be specific. Also, we have no idea how critical it is that PumpEnabled[0] be checked the first time at exactly that spot in the code. The following code might give you an idea:

Rich (BB code):
firstcheck = TRUE;
while (1) // infinite loop
{
   ...
   if(j==1)
   {
      if (firstcheck)
      {
         PumpEnabled_captured = PumpEnabled[0];
         firstcheck = FALSE;        
      }
      if(PumpEnabled_captured==1&&Fert1VolPassed<NeedFert1Amount){PORTC.B5=1;} 
      else {PORTC.B5=0; j=2;}
   }
   ...
}
You can clean this up if you can simply capture the state of PumpEnabled[0] prior to the start of the loop, but that's only if your necessary behavior permits it.
 
Top