Quantcast
Channel: David Orlo .com | Category Archives: Arduino
Viewing all articles
Browse latest Browse all 8

Arduino – Advanced LED Blink

$
0
0

By now you should have your Arduino and have gone through the Arduino Website and maybe you received some documentation with your board which describes the Electrical Engineering Hello World, The LED Blink Sketch.

The LED Blink sketch gives you a rough idea of how you will send commands to the micro controller and how the micro controller will communicate with components attached to it. Other than that your pretty much on your own, if you have a background in software programming, especially C, C++ or C#, you will be able to pick up on most of this very quickly. There are some components which take thorough explanation on how they operate but other than that its mostly all in the code.

I plan to write several articles and in each one I will cover a circuit, the components and the code. I will introduce and explain components, concepts and code in each one. This first one is the Blink Sketch with a twist and the first thing you will notice is one part needs some explanation as to why its there and its purpose in the circuit.

First I will start by saying I will NOT be explaining electrical principles in great detail because I just don’t have the time and its been done so many times over, I will however attempt to simplify things and clarify fundamentals. I was debating on covering some of this in video blogs since no one really likes to read any more and if I do that then I will go into more detail, anyway basically what I am saying is I expect you to know some DC theory here.

First lets build and look at the circuit.

Arduino Advanced LED Blink Circuit

Now back to that part I was talking about, everything seems to make sense here, we have a ground which connects from the board to the LED. Then we have a Positive which connects to the switch and another to the LED, allowing the switch to act as an input and the LED an output in this circuit, but whats that resistor for?

The resistor is used as whats called a PULLDOWN Resistor, there is also an opposite of this which is the PULLUP Resistor. Each one does exactly as its name implies, one Pulls the Voltage HIGH (Positive) and the other Pulls the Voltage Low (Ground), but why is this important and how does it work? In this circuit we pull the voltage LOW to Ground when the switch is NOT in use. This is important because in general most circuits are susceptible to noise and static electricity, basically if the signal is not being pulled high or low when its not in use the signal will jump around and go high and low on its own. You can test this theory if you like but I can assure you its very real and this is why you see PullUps and PullDowns in just about every design. So how does this work? and why isn’t the circuit shorted out when the button is pressed? Well the resistor is there to reduce the amount of current flowing through the 2 points so its not the same as just touching the positive wire straight to ground which would cause the circuit to arc and likely cause damage to the ATMega chip itself. Without getting too crazy with Voltage and Current theory I will say that I will touch on this more later on and attempt to explain this in more detail but for now just remember the resistor is there to stabilize the signal and keep it low until the button is pressed which will take it high and trigger the event we will program in the micro controller.

The resistor used in the example above is a 10K ohm, I know the color bands are not correct.

The Code

/*
Advanced Blink Sketch
By: David M. Orlo
www.DaviedOrlo.com
*/
byte SWITCHPIN= 2; //Set Pin 2 as Switch
byte LEDPIN = 6; //Set Pin 6 as LED
byte brightness; //Create a Integer variable named brightness
byte delayedoff; //Create a Integer variable named delayedoff
byte delayedon; //Create a Integer variable named delayedon
//If you want to go higher than 255 you must change from "byte" to "int"
boolean buttonstate; //Create a Integer variable named buttonstate

void setup()
{
pinMode(SWITCHPIN, INPUT); //Set Pin 2 as Input
pinMode(LEDPIN, OUTPUT); //Set Pin 6 as Output
}

void loop()
{
buttonstate = digitalRead(SWITCHPIN); //Continually look at the switch to see if its pressed
if (buttonstate == HIGH) //If the switch goes HIGH, act on it
{
crazyLED(); //Now we go into a new function called crazyLED
}
}

void crazyLED()//crazyLED function we created and called whatever we want
{
buttonstate = LOW; //First we tell the micro the switch is now LOW
delay(250);
while (buttonstate == LOW) //White the switch is NOT pressed we do the following
{
buttonstate = digitalRead(SWITCHPIN); //Continually look at the switch to see if its pressed
brightness = random(1, 254); //Generates a random number from 1-254 and assigns it to the variable named brightness
delayedoff = random(1, 125); //Generates a random delay and assigns it to the variable named delayed
delayedon = random(1, 250); //Generates a random delay and assigns it to the variable named delayed
analogWrite(LEDPIN, brightness); //Uses the random number we generated to write the value to our LED
delay(delayedon); //random delay on time
analogWrite(LEDPIN, 0); //We turn the LED off for a blinking effect
delay(delayedoff); //Random delay off time
}
//Once the switch is pressed again we break out of the loop above and then break out of the function completely and go back to our main loop
buttonstate = LOW; //First we tell the micro the switch is now LOW
analogWrite(LEDPIN, 0); //We turn the LED off before leaving our custom function
delay(500);
}

I want to stop here and say that by now you should already be very familiar with the Arduino website and all of the great help it has to offer, some areas of extreme interest I will link here and give a breif description as to what they are used for.

http://arduino.cc/en/Guide/HomePage
The getting started page, this should have been your very first step in this whole Arduino process and if wasn’t then I highly suggest you put everything else on pause and visit this page now.

http://arduino.cc/en/Reference/HomePage
The Complete Arduino Language Reference home page, I would take the time to visit every link on that page if only to briefly view what each function does. Even if you don’t fully understand what is being said, I can still guarantee this will be of value during your Arduino learning experience.

http://www.arduino.cc/en/Tutorial/HomePage
The Arduino Examples / Tutorials page, my personal favorite. There are plenty of great examples on this page that not only gives a description of how to use specific code and products but also shows real working code that you can copy into your Arduino IDE and start using right away.

http://arduino.cc/forum/
The Arduino forums, when you can’t find the answer after some Thorough searching, you may try posting a question on the forums and you be delighted to see the amount of knowledge available to you and the friendliness is second to none.

Now lets step through the code, You will notice I really tried hard here to break up the code and comment it well so its very self explanatory.  I also introduced a few advanced concepts such as a WHILE Loop and a separate function called CRAZYLED.

The first thing we do is setup the variables, you should know by now a variable is just a name we give to something to keep it simple for humans to understand and work with. A variable might be a number, a set of numbers, an equation, a name or just about anything. First we have to put the word “int” which is short for integer, this just tells the micro that our variable is going to be a number. Further down in the code we use those same variables but actually assign real values to them, the reason we do this in 2 separate steps is because the first step is to declare the variable which only needs to be done once, the second step is assigning an actual value to the variable. It all makes sense if you just think about it for a while. You will see some of our variables have a static meaning like the pin number on the board while others have a variable meaning like a random number from 1 through 254. Using variables allows us to later refer to something like a pin on the board by a name rather then a number and keeps the code easier to maintain and write.

The next thing we do that is slightly more advanced is use an IF statement, an If statement is very simple and just says IF this is true, false, or equal THEN do this next thing OR do this other thing. The While statement is very similar in that While something is true, false or etc… you do this thing that we tell the micro to do.

The last thing up on the list here is the function we added, the Main Loop that you see in every Arduino sketch is just a function. We can create our own functions and call them whatever we like and use them however we like. I have seen a trend in Arduino programs where the user defined functions are above the main function and I myself like to keep traditional to software design where the main function is first and then the other functions are at the bottom of the code, its personal preference and there is no right or wrong way. There are many reasons to use separate functions and often times you can actually get away with no using functions at all if you don’t want to. Use of functions is good practice because once again like the variable, they keep your code neat and make things easier to understand. They also keep the code very modular because now anytime you want to do a specific set of functions (see where the name comes from) you can just call on this word assigned to your function and suddenly your code does these ten things that you wrapped up all in this one function, need to do it again? no problem just call that function again voila your code executes those ten commands again.

One thing you will notice is that once you are running in the CrazyLED function (While the LED is ON) you may need to hold down the push button for a few moment rather than just tapping it, but why is that?
The reason is because while the function loop is running there are many pauses called “delays”, and during those pauses the micro-controller is basically sleeping and ignores all input output requests except some and those are called interrupts which will explore in the next segment.

That’s it for now, good luck and remember this stuff is all really simple once you get into it


Viewing all articles
Browse latest Browse all 8

Trending Articles