Tutorial 7
New PRE-WRITTEN FUNCTIONS OF ENERGIA 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:
- analogWrite (parameter1, parameter 2): If you remember we have a function called digitalWrite (), which we used to send voltage to certain pin, and much like digitalWrite (), analogWrite () has 2 parameter, parameter 1 tells the function which output pin we should send the voltage to, and the parameter 2 is how much voltage to send. The difference between digitalWrite () and analogWrite () is that in the digital is pretty much ON or off and on the analog is how much voltage to send.
Ex. analogWrite(14, 128) //here we are sening 1.5 volts to the pin 14 which is the GREEN_LED
note: the second parameter goes from 0 to 255 0 being 0 volts 127 being 1,5 volts and 255 being 3 volts.
Explanation:
In this tutorial we are back to blinking LEDs. However this will be a bit different since we will be adding a hint of analog . In this project we are going to output a variable voltage on pin 14 ( GREEN_LED), by changing the brightness of the LED we should be able to simulate a heartbeat by making it brighter and slowly make it fade.
Step 1:
Copy the code shown below into Energia
Step 2:
Once you have it in Energia, compile it, then upload it to the brain of your launchpad
Step 3:
By this point you should see that the GREEN_LED gradually increases from OFF to ON and then turns off
So whats going on?
- First thing that we do is set up a new constant which is analogPin equal to GREEN_LED.
- Then on the setup () we set up the GREEN_LED and an output but we don't necessarily need to do this because pin 14 is an analog pin, so it's optional.
- first we create a for loop to give values to "i" we start from 0 and we go 5 by 5 until we reach to 256.// Note: 0= 0 volts, 128= 1.5 volts and 256= 3 volts.
- The we go to analogWrite which will send a certain amount of voltage to the pin 14 (GREEN_LED) so the change in brightness of the LED can be perceived, in difference from the digitalWrite that just sends 3V or 0V pretty much just on or off.We can see that the voltage send to the LED starts from 0 and has an increment of 5 every cycle.
- There is a delay of 30 miliseconds.
Step 4: (the heartbeat:
But this does not look like a heartbeat right?, instead we can see how the brightness slowly increases but then it suddenly turns off. It would be better if it could also slowly fade away right?, well we can do that, by opening up a new sketch in energia and type this code:
Step 5
Like usual compile it, and upload it to your launchpad, you should see that that the GREEN_LED get brighter and slowly turns off, simulating a heart beat.
Congratulations, now you know how to use analog signals, now take your time to analyze it and fully undestand it, by this point it should be a piece of cake! :D
Now if you can play around with the code change the delay or the voltage change, the more you practice the better you get.