Instructables

Step 12: Write your own code

Picture of Write your own code

To write your own code, you will need to learn some basic programming language syntax. In other words, you have to learn how to properly form the code for the programmer to understand it. You can think of this kind of like understanding grammar and punctuation. You can write an entire book without proper grammar and punctuation, but no one will be abler to understand it, even if it is in English.

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.
  1. The sketch usually starts with a header that explains what the sketch is doing, and who wrote it.
  2. Next, it usually defines global variables. Often, this is where constant names are given to the different Arduino pins.
  3. 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.
  4. 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.
  5. 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.
  • Almost all statements written in the Arduino language must end with a ;
  • 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:

File --> Examples --> 1.Basic --> BareMinimum

The BareMinimum Sketch should look like this:
<pre>void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly: 
  
}

Next, lets put a header on the code, so other people know about what we are making, why, and under what terms:
<pre>/*
LED Dimmer
by Genius Arduino Programmer
2012

Controls the brightness of an LED on pin D9
based on the reading of a photocell on pin A0

This code is in the Public Domain
*/

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly: 
  
}

Once that is all squared away, let us define the pin names, and establish variables:
<pre>/*
LED Dimmer
by Genius Arduino Programmer
2012

Controls the brightness of an LED on pin D9
based on the reading of a photocell on pin A0

This code is in the Public Domain
*/

// name analog pin 0 a constant name
const int analogInPin = A0; 

// name digital pin 9 a constant name
const int LEDPin = 9; 

//variable for reading a photocell
int photocell;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly: 
  
}

Now that variables and pin names are set, let us write the actual code:
<pre>/*
LED Dimmer
by Genius Arduino Programmer
2012

Controls the brightness of an LED on pin D9
based on the reading of a photocell on pin A0

This code is in the Public Domain
*/

// name analog pin 0 a constant name
const int analogInPin = A0; 

// name digital pin 9 a constant name
const int LEDPin = 9; 

//variable for reading a photocell
int photocell;

void setup() {
//nothing here right now

}

void loop() {
  //read the analog in pin and set the reading to the photocell variable
  photocell = analogRead(analogInPin);
  
  //control the LED pin using the value read by the photocell
  analogWrite(LEDPin, photocell);
  
  //pause the code for 1/10 second
  //1 second = 1000
  delay(100);  
}

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:
<pre>/*
LED Dimmer
by Genius Arduino Programmer
2012

Controls the brightness of an LED on pin D9
based on the reading of a photocell on pin A0

This code is in the Public Domain
*/

// name analog pin 0 a constant name
const int analogInPin = A0; 

// name digital pin 9 a constant name
const int LEDPin = 9; 

//variable for reading a photocell
int photocell;

void setup() {
  Serial.begin(9600);

}

void loop() {
  //read the analog in pin and set the reading to the photocell variable
  photocell = analogRead(analogInPin);
 
  //print the photocell value into the serial monitor
  Serial.print("Photocell = " );                       
  Serial.println(photocell); 
  
  //control the LED pin using the value read by the photocell
  analogWrite(LEDPin, photocell);
  
  //pause the code for 1/10 second
  //1 second = 1000
  delay(100);  
}

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.

 
Remove these adsRemove these ads by Signing Up
Kryptonite2 years ago
Step 12, the last two images appear to be the same, is this correct?