Let's try and get more out of a digital signal than just the ability to transmit it from the input to the output. We can take switch is on and say whether this assertion is true or false. If the switch is on, then the assertion is true, and if the switch is off, the assertion is false. The same thing we can say about the value of some variable. For instance, we were reading off the signal from a potentiometer into the sensor value variable so every time we do it we can test the following assertion sensor value is less than 512. If you're a potentiometer is set to the zero value, the assertion will be true because sensor value is in fact lower than 512. As soon as the received value is 512 or higher, the assertion becomes false. Both these assertions are called Boolean expressions, as any regular arithmetic expression it can be calculated. However, after an arithmetic expression is calculated, we get some numeric value. Whereas, after the calculation of a Boolean expression we get a logical value or a Boolean which can equal either zero or one, true or false. With Arduino, it can also be high or low, they are all synonyms in a way. Do you remember how we wrote a program for the traffic light and improve the blinking of the green light by adding a cycle with a four counter? We set the condition and when it was true, the cycle would be repeated. For instance, the i variable initially equals zero and the cycle is repeated while i is weaker than three. This i weaker than three is in fact a Boolean expression, which was tested at the beginning of each cycle iteration. The cycle is repeated if this value is true. Thanks to our possibility to calculate Boolean expressions, we are able to make our program more complex and introduced program branching. Earlier you used to perform a strict sequence of actions following one another, but now we can check if the condition is true and if it is we can perform one series of actions. However, if it is false there will be a different series of actions. I am going to demonstrate how branching is realized in a program. I have prepared one more experimental sketch where I have the output represented by this LED on pin 13 and the input is at a zero. In my diagram I have a potentiometer connected. Look what happens in the loop, a control flow construct appears in the loop, a conditional if statement. It is required so that certain actions would be performed only if the condition is true. This condition is stated here in brackets. In our case this is analog read, analog in is stronger than 512. The signal will be read from the analog in input which is a zero in our case and it will be compared with 512 only if this condition is true. Will the actions inside these curly brackets be performed? There is one single action here digitalWrite(LedPin, HIGH) which turns on the built-in LED. Let's see if this is true. I will be rotating the potentiometer's knob and you can see how half-way through the LED turns on. What will happen if I rotate the knob in the opposite direction? Maybe someone might think that the LED will turn off but you must have already realized that there is nothing magical in the way the controller works if we don't describe our actions clearly nothing will happen. Our program does not contain information on what to do if the value received at the analog input is less than 512 we are going to upgrade our sketch now. I have made an addition to the sketch now I have this new fragment which starts with the else keyword. It refers to the conditional statement and is in fact its extension which describes what to do if the condition is false. In our situation, it is the only instruction digitalWrite(LedPin, LOW) thus if the given condition about the analog input is false and the value is less than 512 or equals 512, only one of the condition will be performed, the LED will turn off. Let's see if the same happens in reality. I have already downloaded the sketch. I am now rotating the potentiometer's knob. We have just exceeded the value of 512 and the LED is on. Now I am rotating the knob in the opposite direction and it turns off, everything works without a hitch. Now, you can make your programs more complex. The main thing is to remember how this conditional construct is created. Try to remember its syntax. Firstly, write the condition in parenthesis. Secondly, the action which should be performed if the condition is true should be put in curly brackets. If you need to describe an alternative action which will be performed, if the condition is false you need to use the else keyword and a pair of curly brackets inside which you describe the alternative action. We have already practiced calculating Boolean expressions a bit now let's see what else we can do with them. They can be stored and values, a special type of data exists for this called Boolean data. I'm going to create a new Boolean variable in our new test sketch. It's called a trigger and at once I am going to communicate, false value to it. We will need it to store the information about the state of the LED. Now, our task is to turn the LED on and off following the switch. This means that one press of the switch means turning the LED on, while the other one means turning it off. Here we have the explanation of how the devices are connected like I told you already afterwards I am going to use the if clause. If the switch is on, we are going to perform the following action. What is this exclamation mark before trigger to the right of the assignment operator? This is a Boolean complementary or inversion operator. What does it do? It takes the current value of the variable which was initially false and changes it to the opposite one a.k.a true. As a result, the variable is reversed in this line. It is assigned a value which is opposite to the previous value regardless of what it was before. After this conditional construct, we pass the trigger to digitalWrite as a parameter. As you might have already understood, in Arduino the true value of the Boolean variable is equivalent to high which creates high voltage on the selected pen. That's why we are just going to pass on the Boolean variable as a parameter to digitalWrite. Now, let's see how exactly this is going to work. As you can see, this works incorrectly. Let's picture the signal we are reading on a graph. On the x axis we will plot time and on the y axis the signal. Let's imagine that we will have a high signal at this level, and a low signal at this level. The switch was not on and then it was pressed and the value at the input changed.