Notes Arduino 6
Notes Arduino 6
Comments in programming are notes that are written in a program (or sketch) by the
programmer. These notes are used to explain what the code is doing, or to add other
useful human readable information to the code. How, when and why to use comments
are explained at the end of this part of the course.
The Arduino increment operator is used to increase the value that a variable holds by
one. This is useful in certain types of loops as will be shown later in this course.
The example below shows the increment operator being used to increment a value
several times.
void setup() {
int count = 0;
Serial.begin(9600);
Serial.println(count++);
Serial.println(count++);
Serial.println(count++);
Serial.println(count);
}
void loop() {
}
Sketch Output
The output of the sketch in the Arduino serial monitor window is shown here.
The following sketch does exactly the same as the previous sketch, but the increment
operations and print operations have been separated into their own statements.
This example may help to clarify what is happening in the previous example. We can
see that the first println() statements prints the value of count and only after this is the
value in count incremented.
void setup() {
int count = 0;
Serial.begin(9600);
Serial.println(count);
count++;
Serial.println(count);
count++;
Serial.println(count);
count++;
Serial.println(count);
}
void loop() {
}
From these two examples, we can see that this single line of code:
Serial.println(count++);
Is the same as these two lines of code:
Serial.println(count);
count++;
The important thing to note is that the incrementing of the variable takes place after
printing it, so the value that the variable holds is printed before it is updated to the new
incremented value.
As always, load the code to your Arduino and experiment with it.
Commenting Sketches
Commenting sketches allows you to write your own text notes along with the code of the
sketch. The next sketch shows two ways of writing comments.
/*
Sketch Name: comments
void loop() {
Serial.println(count++); // print and increment the number
delay(1000); // 1 second delay between printing
}
This sketch uses the increment operator and can be seen running in the video below.
Multi-line Comments
Comments can be written on multiple lines when using the opening forward slash
asterisk (/*) and closing asterisk forward slash (*/) as shown at the top of the sketch.
Any text comments can be written between the opening and closing of the comment
and can span multiple lines. All text in the comment will be ignored by the compiler.
This style of commenting can also be used on a single line, but must always be closed
with the asterisk and forward slash as shown below.
The double forward slash (//) is used to create a single line comment. All text to the right
of the double forward slash on the same line will be a comment and will be ignored by
the compiler.
The single line comment can be used on its own line or to the right of a program
statement.
Comments are used to explain how parts of a sketch work, rather than explain the
actual programming language that is being written.
When you come back to a program or sketch that you wrote a month or a year ago, you
may not remember why you wrote some part of the sketch a particular way. By writing
comments in the sketch, you are making notes for yourself to help explain what you
were doing when you first wrote the sketch.
Comments can also be arranged as a header section or comment block, like the multi-
line comment block at the top of the example sketch. The block contains the name of
the sketch, description or purpose of the sketch, date that it was written and the authors
name. Comments can also contain references to any books, websites or other
resources that were used when writing the sketch.
In short, comments and comment blocks communicate helpful information about the
sketch for yourself and for other programmers who may also use the sketch.