if statement

Discussion in 'Programmer's Corner' started by ecka333, Apr 24, 2012.

  1. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    I im trying to write such condition in C:
    Code ( (Unknown Language)):
    1. if(j==1)
    2.     {
    3.     if(PumpEnabled[0]==1&&Fert1VolPassed<NeedFert1Amount){PORTC.B5=1;}
    4.     else {PORTC.B5=0; j=2;}
    5.     }
    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?
     
  2. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    Sounds like you have a little state machine going there. How about
    Code ( (Unknown Language)):
    1.  
    2. switch(j)
    3. {
    4.   case 0:
    5.     break;                      // As before, don't do anything if j == 0
    6.   case 1:
    7.     if (PumpEnabled[0]==1)
    8.     {
    9.       PORTC.B5=1;
    10.       j = 2;
    11.     }
    12.     break;
    13.   case 2:
    14.     if (Fert1VolPassed<NeedFert1Amount)
    15.       PORTC.B5=1;
    16.     else
    17.     {
    18.       PORTC.B5=0;
    19.       j=3;
    20.     }
    21.     break;
    22.   default:
    23.     break;
    24. }
    25.  
     
  3. WBahn

    Moderator

    Mar 31, 2012
    17,720
    4,788
    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?
     
  4. John P

    AAC Fanatic!

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

    Code ( (Unknown Language)):
    1.  
    2.  case 1:
    3.     if ((PumpEnabled[0]==1) && (Fert1VolPassed<NeedFert1Amount))
    4.  
    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.
     
  5. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    @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?
     
  6. WBahn

    Moderator

    Mar 31, 2012
    17,720
    4,788
    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:

    Code ( (Unknown Language)):
    1.  
    2. firstcheck = TRUE;
    3. while (1) // infinite loop
    4. {
    5.    ...
    6.    if(j==1)
    7.    {
    8.       if (firstcheck)
    9.       {
    10.          PumpEnabled_captured = PumpEnabled[0];
    11.          firstcheck = FALSE;        
    12.       }
    13.       if(PumpEnabled_captured==1&&Fert1VolPassed<NeedFert1Amount){PORTC.B5=1;}
    14.       else {PORTC.B5=0; j=2;}
    15.    }
    16.    ...
    17. }
    18.  
    19.  
    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.
     
  7. ecka333

    Thread Starter Active Member

    Oct 1, 2009
    76
    0
    Thank you, i will try to use your code with additional variable.
     
Loading...