IF CONDITIONS STRUCTURE AND IF WITHIN ANOTHER

Asked

Viewed 6,172 times

0

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

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

And I can put one if inside another?

for example ''if sensor 1 is high-rise claw, (finished climbing) ,if sensor 2 is high-off claw

  • 1

    if (digitalRead(0) == HIGH && digitalRead(1) == HIGH) something like this or

  • if (digitalRead(0) == HIGH){ if(digitalRead(1) == HIGH){"do something}}

1 answer

3


You gave 2 cases of 4 possible. 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 don’t know what your intention is in these two cases not described. Perhaps it is interesting to do nothing in the case of the two sensors turned off (case ???1), but I believe that one of the sensors should stand out in the 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 decided what to do with this input.

Of all sorts, we can transform into a structure of conditions. I will say here that h1 is true if the reading for sensor 1 is HIGH, as well as h2 for sensor 2. If reading is not HIGH, h1 (or h2 if it is 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();
}

The @Brunoh. suggested a solution more or less like the one above in his first comment, of all the luck my version is complete

I used the functions here incognito1 in case ???1 and incognito2 for ???2. Then just fill in the incognito value with the given action. If you’re going to do nothing, you can even remove the function call.

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

Things get confusing when you step out of binary logic and remove the axiom of the excluded third... they call it nebulous logic or Fuzzy logic.

Another way to try to use a little better of the values previously obtained would be to make 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-else-if in sequence previously displayed. Why?

  1. If the first condition is false, then at least one of the sensors is not providing signal HIGH
  2. The second and third comparison of are performed in case at least one sensor does not return HIGH, so now they imply that if one is true, the other is necessarily false
    (but there is no implication of one being being false, the other is true, the counter-positive is not sustained)
  3. 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 cuddle them ifOne inside the other:

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

The @Brunoh. suggested a solution more or less like the one above in his second comment, of all the luck my version is complete

I’m sometimes not exactly a guy who follows design/coding patterns, so in this case it’s quite capable of me going the other way... You didn’t put in what language you’re programming this, so my solution only makes sense if it supports the flow structure switch, or else you could unlock in a sequence of if-else-ifs to simulate the switch.

As we are dealing with signals, we can turn them into a bit string. For example, considering h2,h1 in big-endian, if sensor 2 was in HIGH and the 1 in another value, I would get the bit sequence 10, which equals the number 2 on decimal basis. Then, I would make a switch of that number obtained.

The whole operation is to shift the received bit from h2 for a more significant home to join with the received bit in h1. In C, whereas the value "truth" is a single bit with a value of 1, specifically the least significant bit:

h2 << 1 | h1
  • h2 << 1: I’m shifting the bits of h2 to a more significant position
  • | h1 operator of the ou bit-a-bit joining the previous result with h1; in that case 10 | 01 results in 11 and 00 | 01 results in 01

So, on this account, I would put a switch and would deal with all four cases. The default of switch however needs to be treated separately (therefore incognito3).

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

A very simple example of doing wrong:

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

Why not do so?

Simple. Imagine what to call ligar_garra and deslgar_garra will make immediate communication with a very sensitive piece of hardware called garra. Here, you are in a few microseconds sending two distinct and contradictory commands to her. Depending on the sensitivity of the hardware, you can even generate a crash. It’s as if you had the car accelerate forward and reverse at the same time... it might even break the engine if you achieve this feat.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.