Tutorial 4
summary:
In this project, we will play with both inputs and outputs by changing the state of an LED(output) by pressing a button (input). We are going to learn how a button works, and how to use some new functions available for us in Energia,
New PRE-WRITTEN FUNCTIONS OF ENERGIA and concepts THAT WE ARE GOING TO BE USING:
This functions are already on Energia and are there for us to use it and make our life a little easier:
- const variable=constant:The const keyword stands for constant. It is used to create new functions that will remain constant along the code, this means that whenever you set up a constant it can not be changed further in the code.
The way to use it is by putting const at the beginning, then followed up by the new constant name (variable) and equal to what.
Ex. const PI=3.1416 //now every time you use PI in Energia would be the same thing as putting 3.1416. - INPUT_PULLUP: INPUT_PULLUP is a parameter2 of pinMode (parameter 1, parameter2), parameter 2 is to set up a pin as an INPUT or OUTPUT but when we talk about the built-in button of our launchpad, in order for it to work we need to activate the pullup resistor, that is inside the launchpad by using INPUT_PULLUP. So just remember if you want to use the button you need to use INPUT_PULLUP.
Ex. pinMode (5, INPUT_PULLUP) //because 5 is the same thing as push button 2 we need to use the INPUT_PULLUP. - digitalRead (pin): It can be seen as the inverse of digitalWrite, which is the one that turns on or off a pin (HIGH or LOW). digitalRead reads the state of a pin as HIGH or LOW, digitalRead will read HIGH if it reads a voltage above 1.5v and it will read LOW if it reads a voltage of 1.5v or below.
Ex. digitalRead(14)//reading the state of pin 14 which is the GREEN_LED.
digital read most of the time needs to be used with another function, because by itself digitalRead, reads the state but it won't do anything or won't let you know what is the state.
Ex2..if ( digitalRead(PUSH2) == HIGH)
{ //do something} // here we say that if the state of the push2 (pin5) is high do something. - if (somevariable < condition): Tests whether a certain condition has been reached, such as an input being above a certain number.
Ex. If (x > 120) {do something} //if x is bigger than 120, do something, otherwise do nothing. - if/else: The if/else statement is a more complete form of the if function. In the if function is the statement is not completed automatically is do nothing, on the If/else function if the statement is not met, it gives you the chance to do something else.
Ex. if (pinFiveInput < 50
{do something // action A }
else { // action B }
Here we say that if the value of pinFiveInput is else than 50 do action A, if not do action B.
Step 1:
To start with this tutorial, we need to lead up an example sketch called "button" into energia. We can do this by:
File> Examples> 2.Digital> Button
File> Examples> 2.Digital> Button
Step 2:
Compile and download this code into your launchpad. This particular code does a good job of introducing us to concept of INPUT, OUTPUT, constants and the if statement.
Step 3:
By this point, if you press the PUSH2 button (the one in the bottom left corner). By default the GREEN_LED should be on. When the button is pressed
Step 4:
Now that the code is working, take your time to analyze it and understand it. If you did not understand it, I will put a brief explanation of the code below. After you finish this step make sure to read the challenge that is at the end of the page, which is to make the code work the other way around.
Congratulations, now you know how a button works and how to make it work.
Still if you do not understand what is going on, I will make a simple explanation of both codes below:
walk trough of the original code
- First thing we see on the code is that we setup 2 constants, we say that buttonPin is the same as PUSH2 (which is the button on the left bottom corner of our Launchpad) and ledPin is the same as GREEN_LED. we do this because if in a future we want to use an external button or other LED we can just change it there and it will change in all the code, instead of going one by one. Also we see that after "const" we use "int", well that is unnecesary but is on the sketch.
- After setting the constants we see that we set up an integer (int) which is buttonState and we set it equal to 0. We do this because further in the code we are going to change the value of buttonState and you can not do that with a constant.
- On the setup first we set the ledPin (GREEN_LED) as an output, the we set the buttonPin (PUSH2) as an INPUT but because is the button of our Launchpad we need to use INPUT_PULLUP.
- On the loop we first see that we change the value of buttonState which was 0 now to digitalRead (buttonPin) . We can do this because we already created buttonState and as an integer so we can change it. what happens with buttonState= digitalRead (buttonPin) is that now button state is equal to the digital read (HIGH or LOW) of buttonPin (PUSH 2), so pretty much buttonState will read if the button is pressed or not (Low if it's pressed).
- Then we enter to a for-loop which says that is the button is HIGH (not pressed) turn the GREEN_LED on, and keep it on, unless the button is LOW(pressed), in that case turn the GREEN_LED off.
Code without constants
The code that we just saw looks really complicated, because it has too many constants, in this kind of short codes we do no neccesarily need to use so many constants, but it is good to introduce them to the user. Still with the code below you will be able to see how much simpler looks the code without all the "extra stuff" and it does the exact same thing.
Challenge:
Can you make the code work the other way around?, make the LED turn on when you press the button try it. Still I will let the code below.
Pretty much you just need to change HIGH for LOW, in the "if (buttonState == xxx)" part.