To start programming on a real Arduino board you need to open up the Arduino Integrated Development Environment (IDE), which you'll find by searching the start menu on Engineering department PC's. You can also install in on your own device by going the to Arduino website and downloading the appropriate version for your machine.
When you open it you will get a window like the one below, which is a blank program. Every program you write MUST have the setup and loop functions. Functions are collections of code that run SEQUENTIALLY. If you want to put code in a function it should go between the open curly bracket ({) and closed curly bracket (}) - a common error is deleting the closed curly bracket!
All code in the setup(){} will run only once.
All code in the loop will run repeatedly (forever) in a loop.
As you move through the course and the programs build on each other, it is CRITICAL you don't overwrite programs you have finished with ones that you are working on.
You can now start working on your first program. Before actually doing any work on it, you MUST save. As you write complex programs and use them as a basis for even more complex programs, you don't want to overwrite the work you have just done. So, you will always save BEFORE you actually start working.
Starting with the blank program, go File -> Save As… and give it the name of your next program, which in this case is Ex1_0_blink. In Arduino speak, programs are called sketches.
Strangely enough, the IDE requires you to have each sketch in its own folder. You created your engineering folder so you can keep all these folders together. Save each new sketch directly to your engineering folder, NOT the folder of another program. Don't worry about making folders with matching names for the sketches, the IDE will do this for you.
You're now hopefully looking at a sketch with the correct name and a couple of empty functions, as below.
The LED is connected to one of the microcontroller pins, but these can control either inputs or outputs. Saying which one it will be is done with the pinMode command. Syntax matters in ArduinoC, so you need to capitalise the M and not the p in pinMode. You will know you've done it right when the text turns orange.
The board - with the various numbered pinMode output pins on the left.
Type pinMode into your blank, but correctly saved Ex1_0_blink program. Indenting each line of code is critical for organisation and troubleshooting. Since the pinMode command is inside the setup function, it should be indented in one tab or space. You will later see how your program becomes difficult to de-bug if you don't do this.
LED 1 is connected to pin 1 and it is an output, so fill that in the setup function, as shown below. Again, syntax is critical and you need to capitalise OUTPUT. The word OUTPUT will turn blue/green when done correctly.
The semicolon (;) is also important since it marks the end of the command. This is the number one most common error when trying to figure out why your code doesn't run.
To enjoy the fruits of your labour, you want to make the LED flash on and off repeatedly, so you want to put the code for this in the loop procedure. digitalWrite is the command for sending a HIGH or LOW voltage to the pin of your choosing. Remembering that the LED we want to work with is connected to pin 1, the code will look something like:
To get the code on your board you first have to plug it into the computer with the grey Belkin micro-USB cable on your desk. If the particular board has already been plugged into this computer, you should see it under Tools -> Port as COM2-99. Select it. Forgetting to select the board every time you open up the IDE (ie. the beginning of each lesson) is the second most common error.
If you can ONLY see COM1 (meaning your board hasn’t been recognised), swap boards with your neighbour, plug his in, then swap back.
Now you can send the program by pressing upload (right-pointing arrow, or Control+U on the keyboard).
During the upload you will see a number of messages at the bottom of your programming window:
First compiling ...
then uploading ...
Finally, done uploading.
After some initial flashing, you'll see LED 1 staying on. The initial flashing of LED1 indicates that the computer is communicating with the tutorial board (uploading your program) via Pin 1.
Why is the LED just on all the time and not turning off?
The processor executes commands very quickly (16 million per second), so you have no chance of seeing the transition from ON to OFF. You need to have the program pause between commands so you can perceive the difference. We do this with a delay command, with the parameter inside the command being the number of miliseconds the program should pause. Let's try 1 second.