Step 12: Write your own code
Some important things to keep in mind when writing your own code:
- An Arduino program is called a sketch.
- All code in an Arduino sketch is processed from top to bottom.
- Arduino sketches are typically broken into five parts.
- The sketch usually starts with a header that explains what the sketch is doing, and who wrote it.
- Next, it usually defines global variables. Often, this is where constant names are given to the different Arduino pins.
- After the initial variables are set, the Arduino begins the setup routine. In the setup function, we set initial conditions of variables when necessary, and run any preliminary code that we only want to run once. This is where serial communication is initiated, which is required for running the serial monitor.
- From the setup function, we go to the loop routine. This is the main routine of the sketch. This is not only where your main code goes, but it will be executed over and over, so long as the sketch continues to run.
- Below the loop routine, there is often other functions listed. These functions are user-defined and only activated when called in the setup and loop routine. When these functions are called, the Arduino processes all of the code in the function from top to bottom and then goes back to the next line in the sketch where it left off when the function was called. Functions are good because they allow you to run standard routines - over and over - without having to write the same lines of code over and over. You can simply call upon a function multiple times, and this will free up memory on the chip because the function routine is only written once. It also makes code easier to read. To learn how to form your own functions, check out this page.
- All of that said, the only two parts of the sketch which are mandatory are the Setup and Loop routines.
- Code must be written in the Arduino Language, which is roughly based on C.
- Almost all statements written in the Arduino language must end with a ;
- Conditionals have their own rules and can be found under "Control Structures" on the Arduino Language page
- Variables are storage compartments for numbers. You can pass values into and out of variables. Variables must be defined (stated in the code) before they can be used and need to have a data type associated with it. To learn some of the basic data types, review the Language Page.
Okay! So let us say we want to write code that reads a photocell connected to pin A0, and use the reading we get from the photocell to control the brightness of an LED connected to pin D9.
First, we want to open the BareMinimum sketch, which can be found at:
The BareMinimum Sketch should look like this:
Next, lets put a header on the code, so other people know about what we are making, why, and under what terms:
Once that is all squared away, let us define the pin names, and establish variables:
Now that variables and pin names are set, let us write the actual code:
If we want to see what numbers the analog pin is actually reading from the photocell, we will need to use the serial monitor. Let's activate the serial port and output those numbers:
For more information about formulating code, visit the Foundations Page. If you need help with the Arduino Language, then the Language Page is the place for you.
Also, the Example Sketch Page is a great place to start messing around with code. Don't be afraid to change things and experiment.