HOME > > ARDUINO LANGUAGE COURSE  t.o.c.      Delicious Bookmark this on Delicious Recommend to StumbleUpon

-- Getting To Know You --

A trivial exercise, just to get started:

Winking two LEDs... but doing it RIGHT!

* This is the first in a series of sequenced tutorials which, together, aim to show you "everything" about the Arduino's programming language.
* There is a page for you with more information about the project in general, and the way these pages are organized.
* Please visit my page with hints for power browsing sometime.

------------ This page, and the software it references, ©TK Boyd, 1/2010.


You are ready for the material here as soon as you have your Arduino set up, and the free, open source, IDE set up in your desktop or laptop. The material here covers programming the Arduino, starting from the basics. Along the way, sundry related electronics issues are covered, but the electronics tuition is less comprehensive.

Please introduce this site to any intelligent, self-disciplined 12 year olds you may know. I wrote it intending that age group as part of my audience.

If you are just getting started with Arduinos, you may need to access different tutorials for "how to hook up an LED?", for "how do I set up the compiler?", etc. Excellent tutorials for those needs already exist. For instance, there are the lessons at LadyAda.net.

If you look at the LadyAda material, don't be frightened off by the mention of the $65 starter kit... It includes an Arduino, among other things. It may good value, if you haven't started your Arduino "life" yet, but is certainly not the most cost conscious option available to you.


Our first program, or "sketch" as the Arduino types like to call it, is Really Badly Written. But it works. Here it is:

void setup()
{
   pinMode(12,OUTPUT);
   pinMode(13,OUTPUT);
}

void loop()
{
   digitalWrite(12,LOW);
   digitalWrite(13,HIGH);
   delay(400);
   digitalWrite(12,HIGH);
   digitalWrite(13,LOW);
   delay(400);
}

Connect an LED (with the usual current limiting resistor) to the D12 and D13 outputs of your Arduino, and run the program. You should see the two LEDs flashing alternately. Whoopie Do! Don't worry... we'll be doing fancier things before long! The program is called FEACrudeWink in the zip filled with the programs for this tutorial... which I will create one day! For now, use copy/paste. (FEA: "Flat Earth Academy", a1: "Arduino examples, first set.")


You've had a bit of play... now some work. Take a deep breath, don't skim, don't go to sleep. I'm going to go over some Basic Facts. Boring, yes. But with solid foundations, and you will go a lot farther than the "What button do I press?" crowd.

Even in the crude, simple program above, many basic points are illustrated.

The Arduino language is case sensitive. Our program begins with the word "void", and that won't be recognized if you type "Void". When you reach the realms of having a choice, it is tempting to write everything in lower case letters. Resist that temptation. Use mixed case to help make your code easy to read.

The program consists of two functions, "setup" and "loop". What is a function? For now, although you will later learn about alternatives, consider a function to consist of the following. Anything in italics is NOT part of what you would type. It is descriptive.

--

keyword void function name setup followed by ()

--

That's the header for the function. It is followed by the function's body, which is enclosed in curly braces....

-----

{  curly brace says "body starts here"
pinMode(12,OUTPUT);  just an example of a function body... they can be made of many, many things.
}  the body will always end with a close-curly brace....

-----

(We'll come back to "pinMode(12,OUTPUT);" later. It is a detail. We should to get the overall shape of things clear first.)

Look at the whole of our crude program again. You should see that it consists of two functions: "setup" and "loop". Both start "void", both have the "()" after the function name, both have bodies enclosed in a curly-brace pair "{}". Try in your studies to note the overall patterns in things. Then when something in your program isn't working, look to see if you have deviated from the pattern's rules.

Hang in there! No skimming. No dozing. It does get better.

Those two functions, "setup" and "loop" are present in every Arduino program. When the program is started, what is in "setup" is done once, then what is in "loop" is done over and over again.

You will soon be making your own functions... but any you make will have to be invoked from somewhere within "setup" or "loop".

Wait for it. We WILL get through this boring "establish the foundations" stuff.

A moment ago, I called "void" a keyword. Keywords are the words "built into" the language. They have a meaning already, they are not available to you to use for other things. For now, just remember that they are reserved. They too are case sensitive, as other things are. (Because of that, you might, in fact, get away with using "vOID" somewhere, as, to the computer, it is not "void"... but it would be silly to take advantage of this.

Look at the program in your Arduino programming window. Notice how "void", "setup", "loop" (and other words) are displayed in a different color from things like "12", "13", "400"? This is not just because they are not numbers. Change "void" to "Void": It will display differently. Type "Some Gibberish" into the program anywhere you wish. It will display differently. This is called "syntax highlighting" and helps you recognize keywords and also to spot typos.

The "setup" function in our program consists of two statements....

pinMode(12,OUTPUT);

and

pinMode(13,OUTPUT);

"Statements" to a program are a bit like "sentences" to a novel. Always put a semi-colon (;) at the end of a statement. (Strictly speaking, the statements are "pinMode(12,OUTPUT)" and "pinMode(13,OUTPUT)", and the semicolons are a conjunction to join the parts of the body together into one "big statement", which should have a semicolon at its end, as well as between the constituent statements. (Inserting the semi-colons is a bit like putting the } at the end of the function.... although it is UN-like the punctuation for function bodies in that you don't need to put anything in front of the statement.)

"pinMode" is another keyword. There really are not so very many keywords to master, although it may seem that way at first. Let's get some uses of the "pinMode" keyword out of the way....

"pinMode" is always followed by something in ordinary brackets, aka parentheses.

The "something" will consist of a number (or something that "boils down to" a number... more on that later), followed by a comma, followed by the word INPUT, or the word OUTPUT... all in capitals. (Did I mention the language is case-sensitive?)

What does pinMode DO???

The chip at the heart of the Arduino has many pins which you will connect things to as your Arduino skills grow. (And a few pins you will never do anything with.)

These tutorials are concentrating on the software aspects of using an Arduino, so I won't go into much electronics depth here about the differences between "inputs" and "outputs", but...

In our crude winky program, pins 12 and 13 are being used as outputs. Inside the Arduino, they will, effectively, either connect to the 5 volt side of the power supply, or to "ground", aka the zero volt side of the power supply. So, with our program running don't put a wire from either pin directly to 5v or ground, as that could damage your Arduino! Remember that an LED on its own is like a wire. Each LED needs to have a resistor in series with it.

The statement...

pinMode(12,OUTPUT);

...tells the Arduino to set pin 12 up to work as an output.

This Arduino stuff really is simple! If you can stay awake. Chess is simple... but winning a game against an experience player isn't. Making your Arduino do some of the things you will want it to do isn't easy... but the "ingredients", the "things you need to know", are not hard.


Okay... onward....

Now that we've dealt with the idea of a function, whew, and dealt with the details of this program's "setup" function, the "loop" function should be a doddle. Have a look at it, see how much you can recognize as part of the pattern we've already discussed. (I've merely repeated it here, to save you scrolling back to where it first appeared.)

void loop()
{
   digitalWrite(12,LOW);
   digitalWrite(13,HIGH);
   delay(400);
   digitalWrite(12,HIGH);
   digitalWrite(13,LOW);
   delay(400);
}

"digitalWrite" is another keyword.

Like "pinMode", it is followed by some stuff inside parentheses. Again, that stuff starts with a number then there's a comma and then the word HIGH or LOW. And after the statement, a semicolon, as ever.

If your program has already set pin 12 to be an output, then...

digitalWrite(12,HIGH);

... makes pin 12 high, i.e. it "connects" it to 5v. (We set pin 12 to be an output in the "setup" function of the program under dissection.)

Execute the statement....

digitalWrite(12,LOW);

... and pin 12 will be "brought low", i.e. "connected" to zero volts. (Which, by the way, is different from being disconnected from everything. (Which cannot be done by, say, digitalWrite(12,DISCONNECT); (There is no way (that I want to digress into at this point) to disconnect a pin. But you needed to know that "connected to zero volts" is very different from "disconnected".)

"delay" is a keyword. It causes the Arduino to sit and twiddle its thumbs for a period of time. How long? If the statement is "delay(400);", the Arduino will "do nothing" for 400 thousandths of a second. (I.e. 400 milliseconds.)

So! Now you know "everything" about our little program!

And you've got past the worst of the "getting foundations laid" material.... HURRAH!


Thought you were done?

'Fraid not. That program "works", but I would argue that it is a Bad Program. In the next tutorial we will look at another program that does the same thing... but does it BETTER.




   Search this site or the web      powered by FreeFind

Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.

SPELL your search term properly. When I review search logs, it is amazing how many people ask the engine to search for something meaningless.


Why does this site cause a script to run? I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try it, check out their site. And if there are Google ads on the page, they are run with scripts, too.


Click here to return to Arduino COURSE table of contents.
Click here to go to the author's home page.

Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.


Here is how you can contact this page's editor. This page, and the software it references, ©TK Boyd, 1/2010.

Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org