CONDITIONS IF STRUCTURE and an if inside another

0

How is the structure to put 2 conditions in the IF?

if (digitalRead (0 & 1), HIGH) would look like this

And can I put an if inside another?

for example '' if the sensor1 is high climb claw, (if it has climbed up) if sensor 2 is high turn claw

    
asked by anonymous 29.11.2017 / 05:28

1 answer

3

You have given 2 possible 4 cases. Then we have the diagram below:

          sensor 1
        0    |    1
     +-------+-------+
s    |       |       |
e  0 | ???1  | ligar |
n    |       | garra |
s ---+-------+-------+
o    |       |       |
r  1 | desli |  ???2 |
     |   gar |       |
2    | garra |       |
     +-------+-------+

I do not know what your intention is in these two cases not described. It may be interesting to do nothing in the case of the two switched sensors (case ???1 ), but I believe that one of the sensors should stand out in case of the two sensors connected (case ???2 ).

When you implement this, you need to define the four cases. Even if the decision is not to call any other action, you have decided what to do with this input.

Of all sorts, we can transform into a framework of conditions. I'm going to say here that%% is true if the reading for sensor 1 is h1 , as is HIGH for sensor 2. If the reading is not h2 , HIGH (or h1 if it is the sensor 2) will have false value:

if (!h1 && !h2) {
  incognito1();
} else if (h1 && !h2) {
  ligar_garra();
} else if (!h1 && h2) {
  desligar_garra();
} else if (h1 && h2) {
  incognito2();
}
  

suggested a solution more or less like this one in the first comment of it, of all luck my version is complete

I used here the h2 functions for the incognito1 and ???1 to incognito2 cases. So just fill in the incognito value with the determined action. If it is to do nothing, you can even remove the function call.

I am not making use of any advantage of the knowledge I acquired previously. For example, the last ???2 I put is tautological in binary systems: if none of the other 3 conditions is true, then this fourth one will occur.

  

Things get confusing when you get out of binary logic and remove the axiom of the excluded third ... call this fuzzy logic or fuzzy logic .

Another way to try to use a bit better of the values previously obtained would be to do a structure like this:

se H1 && H2:
  incógnito 2
senão se H1:
  ligar garra
senão se H2:
  desligar garra
senão:
  incógnito 1

This gives exactly the same as the set of if in the sequence previously presented. Why?

  • If the first condition is false, then at least one of the sensors is not supplying if-else-if
  • The second and third comparisons are performed in the event that at least one sensor does not return% cos_de%, so now they imply that if one is true, the other is necessarily false. (but there is no implication of a false being, the other is true, the counter-positive does not hold)
  • If none of the above three conditions is true, then this implies that the two signals received from the sensors are not HIGH
  • Or else I could nest the HIGH s inside the other:

    if (h1) {
      if (h2) {
        incognito2();
      } else {
        liga_garra();
      }
    } else {
      if (h2) {
        desliga_garra();
      } else {
        incognito2();
      }
    }
    
      

    suggested a solution more or less like this one in his second comment, of all sorts my version is complete

    I'm sometimes not exactly a guy who follows design / coding patterns, so in that case it's quite capable of me going the other way ... You did not put in what language you're programming this, so my only solution it makes sense if it has support for the HIGH stream structure, or else you could unlink in a sequence of if s to simulate switch .

    As we are dealing with signals, we can transform them into a string of bits. For example, considering if-else-if in big-endian , if sensor 2 were in switch and 1 in another value, I'd get the% string of% bits, which equals the number h2,h1 on a decimal basis. So I would make a% of% of that number obtained.

    The whole operation is to move the received bit from HIGH to a more meaningful house to join with the received bit in 10 . In C, considering that the value "truth" is a single bit with a value of 1, specifically the least significant bit:

    h2 << 1 | h1
    
    • 2 : I'm shifting the bits from switch to a more meaningful position
    • h2 operator of h1 bit-a-bit joining previous result with h2 << 1 ; in this case h2 results in | h1 and ou results in h1

    Then, on this account, I'd put a 10 | 01 and treat all 4 cases. The case in 11 of 00 | 01 however needs to be treated separately (therefore 01 ).

    switch (h2 << 1 | h1) {
    case 0:
      incognito1();
      break;
    case 1:
      ligar_garra();
      break;
    case 2:
      desligar_garra();
      break;
    case 3:
      incognito2();
      break;
    default:
      incognito3();
    }
    

    How not to do

    A very simple example of doing wrong:

    if (h1) {
      ligar_garra();
    }
    if (h2) {
      desligar_garra();
    }
    

    Why not do so?

    Simple. Imagine calling switch and default will immediately communicate with a very sensitive piece of hardware called switch . Here, you are in a few microseconds sending two distinct and contradictory commands to it. Depending on the sensitivity of the hardware, you can even generate a crash in it. It's as if you have the car accelerate forward and reverse at the same time ... it might even break the engine if you get that feat.

        
    29.11.2017 / 06:46