04) Lighting Magic!

Let's get started learning code! Don't worry, this one is really easy!
Instead of the boring lectures, lets get a light to turn on, then I'll explain what each line does.

To do this project, you need to have:
    1 LED.

Plug the LED into digital port 5. (long leg into port 5 and short leg into ground)
To do this you may need the assistance of a breadboard!

Once you got EVERYTHING plugged in (oh man, one LED, that's a lot), lets get the code to turn on the LED!
ready?

void setup()
{
}
void loop()
{
    digitalWrite(5,HIGH); // This turns digital port 5 ON
}

So what did this do? If we compile with this, it will turn on the LED if properly hooked up and compiled.

So lets look at line 1: "void setup()"
This line is starting a "Function", a function can due multiple events, but this particular function is a void. Due to being a void, all we care about is whats inside it. There are also "(" and ")" that are closed off right next to each other, these would be where arguments for the function would be, but since this function does not take any arguments, it is left blank (but we still leave them there like ()!). I will go over what arguments are a few lines down. This particular function runs right when the Arduino is turned on. Which leads us to line 2.

Line 2: "{"
This curly bracket "{" declares the introduction of the function. Every thing between the root "{" and "}" is all part of the same function.

Line 3: "}"
The setup function is abruptly ended. There was nothing in the function, well at least no commands... When the function setup ends, the function called loop begins.

Line 4:"void loop()"
This line looks like line 1, except, this function is called loop instead of setup. Just like setup, it is a void and we only care about the substance inside of the function. Instead of setup, where it happens at start up, this function happens when setup finishes. When this function reaches its "}" ending mark, it runs the function again.

Line 5:"{"
Starting up the loop function.

Line 6:"digitalWrite(5,HIGH); // This turns digital port 5 ON"
There is quite a lot going on here! But don't get overwhelmed. The text in grey is actually a comment I made to both myself and my reader. It is a gentle reminder that does not get in the way of the code. It is a way of documenting my thoughts for future reference and for others such as you trying to interpret the code. The comment can be started with "//" which runs to the end of the line, or by "/*" which runs until it finds "*/" (which can go through multiple lines).

Now we read the text in the orange. It says "digitalWrite". This is actually a function, but instead of defining a function like we did in "setup()", we are actually running one now. This function has already been defined by the computer. I can tell that I am running a function by the format of the line. Running functions are written like: "functionName(argument1,argument2);". Where the line starts out with the functions name, in this case "digitalWrite", then moving on to opening the parenthesis for arguments that the function would like to know. The function that I am using is digitalWrite and it has only two arguments, those arguments are its port number and its state. Our port number, as described above, is 5. And we also want to turn that port 5 on, so our LED can turn on. To do that we set the second argument to HIGH (as opposed to LOW). There are no more arguments, so we close the parenthesis, and then put a semicolon,";", at the end of it.

Line 7:"}"
This is the end of the function, as well as the loop! The code will now go back to line 5 where the function started.


Congratulations! You have just finished understanding your first piece of code! For something as simple as turning on a light, it is a big accomplishment to get your first project working. When your ready, move on to making the light blink.

If you have any questions or comments, leave them here or email me! I would like to hear what you think. This is the most critical lesson!
Comments