Arduino Code: Conditional Statements

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
there are times when you might want your Arduino to respond to some user interaction for example let's say I push a button this might light up a light or make a robot drive forward to do that we'll need to use conditional statements which changed the way of program flows but before we get into conditional statements let's talk about relational operators a relational operator asks a yes or no question about the relationship between two numbers for example is 15 equal to 9 well no is 42 greater than 23 yes is 7 not equal to 70 yes 7 is not the same number as 70 we can express these questions using 6 different operators a double equal sign asks the question are the two numbers on the left and right equal an exclamation mark followed by an equal sign is the opposite are the two numbers not equal to each other the less than symbol asks if the number on the left is less than the number on the right the greater than symbol is the opposite it is a number on the left greater than the number on the right you also have less than followed by an equal sign this checks if the number on the left is less than or equal to the number on the right finally the greater than symbol followed by an equal sign is similar is the number on the left greater than or equal to the number on the right when we use relational operators note that they return an integer value the operation results in either a 1 for true or 0 for false we can test this using some serial print statements for example in a new sketch write serial dot begin 9600 in setup and serial.println 3 equals equals 3 under that upload that and open a serial monitor as you can see the answer to does 3 equals 3 comes out as a 1 for true now let's try a different operator using variables delete the serial trantel end line and write int a equals negative 107 then int B equals 36 under that write serial dot println a greater than equals B upload this and check the serial monitor you ca0 to show that negative 107 is not greater than or equal to 36 printing out yes or no answers as integers is great fun and all but relational operators become far more useful when paired up with conditional statements a conditional statement is some code that performs different actions depending on the outcome of some specified condition this is easily represented in a flowchart let's say that we have a test case in our program where we evaluate some number against another for example is a less than B this question is represented as a diamond in the flowchart if so then execute some code if not then execute some other code instead let's look at a real code example back in our program remove the serial dot println line right if open parentheses a greater than or equal to B close parentheses open curly brace serial dot println open parentheses open quotation marks true exclamation points close quotation marks close parentheses and then make sure you have a close curly brace this says that if the relational operation between the parentheses after if returns true or 1 then execute the code in between the curly braces for us the word true would be printed to the serial monitor only if a was equal to or greater than feet run this and open the serial monitor because a is not greater than or equal to B nothing shows up if we change it to a less than B and run that you should see true appear as negative 107 is less than 36 if we wanted to display something when our relational operation is false instead we could add an else statement after the closed curly brace write else open curly brace and then serial dot println false make sure that we have a closed curly brace now if a is less than B true is printed otherwise the word false gets printed change the relational operation to a equals equals B to C if a is equal to B run this and you should see false appear one common mistake you may find is accidentally typing one equal sign when you mean to compare two numbers for example if I use a equals B for my relational operation what do you think will happen when I run this oddly enough the program compiles and runs just fine when I check the serial monitor I get true wait a minute a doesn't equal B how can this be well remember that the single equal sign is the assignment operator in C it's not for comparing numbers what's happening is that when our program reaches our if statement instead of comparing a and B it assigns the value in B to a so now a becomes 36 one tricky thing about the C programming language is that whenever we perform an assignment operation it returns the value of the newly assigned variable in this case the if statement is asking if the value 36 is true can forsee any value that's not 0 is true we can write a serial dot println a after the if else statement to see this in action upload this and take a look at the serial monitor we see true printed as the conditional part of the if statement soft 36 and that's not 0 so it ran the code under if we see that the value of a was indeed changed to 36 so if you're seeing weird behavior from your if statements in your program definitely check to see if you accidentally used a single equal sign there's one last part to the if-else statement that I'd like to show we can chain together if-else statements to check more than two states delete everything below the int a statement in the setup section right if open parenthesis a equals equals zero close parenthesis open curly brace serial.println open quotes a is 0 close quotes to show that a is equal to 0 then to make sure you have a close curly brace else if open parentheses a less than 0 close parenthesis open curly brace serial.println open quotes a is negative close quote if a isn't equal to 0 then the program checks this statement next if a is a negative number then it will print that finally right close curly brace else open curly brace under that serial dot println open quotes a is positive close quotes and make sure there's a closed curly brace if a isn't 0 and it isn't negative then it has to be positive so we'll print that to the terminal upload this and open the serial monitor because a is negative 107 we should get the middle condition to evaluate to true so we should see a is negative in the terminal as a real example of using if statements let's say we have a button we want to press and have it execute some code to do that in code we would first set the pin connected to the button as an input the program will be able to see if the button has been pressed or not we also need to set the LED pin as an output since we want the program to control that pin we then want to check if the button has been pressed and if so turn on the light if it has not been pressed turn off the light we need to repeat this conditional statement over and over again so we'll have it in the loop function with a button on a breadboard spanning across the channel attach one of the pins to ground and the other pin that's on the same side of the breadboard to pin 7 now when we press the button an electrical connection will be made between the ground pin and pin 7 we'll use some code to set the default state of pin 7 to 5 volts or digital high so that when the button is not pushed pin 7 will read high when it is pushed pin 7 will read low in a new sketch type content button pin equals 7 and content LED pin equals 13 at the top these set the pin numbers of our button and LED in setup pin mode button pin comma input underscore pull up in all capital letters this tells pin 7 that we want to watch its state from code whether that's high 5 volts or connected to ground input pull up is a special identifier in Arduino that says we need to enable an internal pull-up resistor this is what sets 10-7 to some default value specifically digital high when it's not connected to another voltage like ground after that right pin mode led pin comma output to tell Arduino that we want to control the voltage to the pin connected to the onboard led in loop right if open parentheses digital read button pin equals equals low close parenthesis open curly brace under that digital right led pin comma high if the button is pressed digital read will return low which corresponds to zero volts as the button is connecting pin seven to ground if digital read returns low the relational operation will be true and our program will set pin 13 voltage to high which turns on the LED then right close curly brace else open curly brace under that digital right led pin comma low and make sure we have a closing curly brace if the button is not pressed then our button pin will be high and the if statement will be false so the code in the elf section will execute ensuring that the LED is off upload this to your Arduino when I press the button the LED turns on and when I let it go the LED turns off I know I know I could just directly connect the LED to the button to accomplish the same thing but we're doing it in code so it's much cooler okay if you want to challenge try making it so that when you press the button the LED flash is on for half a second and then off for half a second happy hacking
Info
Channel: SparkFun Electronics
Views: 39,124
Rating: 4.8951612 out of 5
Keywords:
Id: YktSocf2vSc
Channel Id: undefined
Length: 10min 25sec (625 seconds)
Published: Mon May 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.