Arduino Lesson 4 – If Statements

So if I wire this up so Ive wired the button up to ground on this side, five volts on this side and the digital read wire is hooked up in the middle to pin seven of the Arduino. Now I explained how these buttons worked in the in the last episode of tutorials, but Ill go over it again. So basically, the digital read wire and the ground wire are connected via this PCB through this resistor, which is between 1 and 10 K. So when we press the button, it bridges the gap between the digital wire and the 5 volt line, because theres no resistor between those two, the digital wire is pulled high. The LED is wide to ground on one side and the LED circuit is just a green LED and a 220 ohm resistor and then on the other side of the circuit, its wired to one of our digital pins on the Arduino. In this case its wired. To pin 8 so if we digital right to pin 8, if we digital, write low, the LED will turn off. If we digital write high, it will turn on so now we can start writing some code so now Ive wide. All that up, we can start coding. Little circuit, so the aim is to be able to turn the LED on when we press the button. So first off were gon na set the button and the LED up right at the top here by giving them English name.

So we can call our English name. Instead of calling the pin number so we can int and then you can call it whatever you want, but Im gon na call it button equals and then Ive wired my button to pin seven so Im gon na write, seven and semicolon. Now we have set up the LED, so were gon na get LED, equals Ive void. My LED to pen eight, so Im gon na write eight. So now we come down to the void setup and we write, pin mode with a capital N to setup the inputs and outputs and were gon na write. Button and button is an input because were gon na read a signal from it. So we write input in all capitals semicolon and then were gon na write pen mode again and this time were gon na set up the LED as an output, because we write a signal to an LED. We dont read one so its an output, so Ive explained this more in previous tutorial. So if you dont understand what Im doing here, then go back and take a look at the previous ones. So now weve finished all our setup code. We can move on to the void loop and we can start with our if statement. So we start, if statement by just writing if in the code and then we open and close some brackets and then we open and close some squiggly brackets. So this here is our.

If statement now between these brackets here, we have to make a statement which, if it, if the statement we make is true, then were gon na run the code between this squiggly bracket and this squiggly bracket. If the statement we make here is false, then were going to ignore the code thats between this squiggly bracket, then this squiggly bracket. So in this example, what were trying to do is check if a buttons been pressed or not if the buttons been pressed were going to turn an LED on. So what we do is in here wed write a statement which says if the button has been pressed, then in this pot here youd right turn the LED on and then underneath youd right, otherwise turn the LED off. So first off lets start by writing our true or false statement up here. That is in relation to the button. So, first off we have to check the status of the bottom, so you have to say digital read and then we have to tell it what were checking. So you brackets right button and then we close the brackets so so were digital reading the button and, as I said at the end of the tutorial on digital reads that I did two tutorials ago. I think now this returns either a high or a low. So at the moment that on its own isnt theres no comparison there, so you cant check whether its true or false.

So we have to give it something to compare against. So we know this is either going to return a high or a low. So lets say if the digital reader of a button is equal, equal, which means equal to so theres two equals there, and then we write either high or we write low in this instance. I want this if statement to be triggered when we are pressing the button, which means I want it to be triggered when it goes high. So what were saying here is, if we read the button and the buttons value is equal to high, then do what is ever whatever is in between these brackets. So in this instance, we want the LED to turn on so we write in here digital right and then we open the brackets tell it which pin were talking about which in this case you could either write eight or because right at the top weve set up Led equals eight we can write, LED, then comma and then either high or low. In this instance, we want to turn the LED on so we write high close the brackets semi colon, so thats, the if statement finished. The only problem with this is the LED will start off being turned off well press the button once it will turn the LED on and then the LED will never turn off again, because weve not got a digital write low anywhere in this void loop.

So what we can do is write an else statement which is in some way its connected to if statement, so it basically means else just means if what were saying here isnt true then do this. Instead, so we write else and then we open our squiggly brackets and then we close our squiggly brackets here and then in between we write digital right LED low. So basically that means, if were not pressing, the button then were just going to turn the LED off. So now were going to upload that and see how it looks on our Arduino when we press the button its going to pull our digital Reed pin high, which will make our if statement true. That means that we can then digital right to our LED high. So when we press the button, the LED will turn on the else.

Share.
Exit mobile version