Datasheet
Chapter 1 • Your Shield-Bot’s Brain
36 • Robotics with the BOE Shield-Bot
Activity 7: Constants and Comments
The next sketch, CountToTenDocumented, is different from CountToTen in several ways.
First, it has a block comment at the top. A block comment starts with
/* and ends with */,
and you can write as many lines of notes in between as you want. Also, each line of code has
a line comment (starting with // ) to its right, explaining what the code does.
Last, three
const int (constants that are integers) are declared at the beginning of the
sketch, giving the names
startVal, endVal, and baudRate to the values 1, 10, and 9600.
Then, the sketch uses these names wherever it requires these values.
/*
Robotics with the BOE Shield – CountToTenDocumented
This sketch displays an up-count from 1 to 10 in the Serial Monitor
*/
const int startVal = 1; // Starting value for counting
const int endVal = 10; // Ending value for counting
const int baudRate = 9600; // For setting baud rate
void setup() // Built in initialization block
{
Serial.begin(baudRate); // Set data rate to baudRate
for(int i = startVal; i <= endVal; i++) // Count from startVal to endVal
{
Serial.println(i); // Display i in Serial Monitor
delay(500); // Pause 0.5 s between values
}
Serial.println("All done!"); // Display message when done
}
void loop() // Main loop auto-repeats
{
// Empty, no repeating code.
}
Documenting Code
Documenting code is the process of writing notes about what each part of the program does.
You can help make your code self-documenting by picking variable and constant names that
help make the program more self-explanatory. If you are thinking about working in a field
that involves programming, it’s a good habit to start now. Why?
• Folks who write code for a living, like software developers and robotics
programmers, are usually under orders to document their code.
• Other people might need to make updates to your code or use it for another
project, and they need to understand what the code does.