To get started using Codebender instead of the Arduino IDE, click here [1].
Here is a screen capture of the Arduino Development Environment edit pane on the left, containing a simple sketch that sends a “Hello!” message to the Serial Monitor window on the right.
void setup() { Serial.begin(9600); Serial.print("Hello!"); } void loop() { //Add code that repeats automatically here. }
Now you are ready to see How the Hello Sketch Code Works [2].
Now that you have an account to use with codebender : edu, and have installed the required plug-in and drivers, it is time to try a sketch!
void setup() { Serial.begin(9600); Serial.print("Hello!"); } void loop() { //Add code that repeats automatically here. }
Later, you can choose Open from this menu to re-open your sketch. Or, navigate to the un-zipped example code archive you downloaded previously to open sketches used in this book.
To keep things simple, the rest of the Shield Robot tutorials will feature directions and screen-captures for the Arduino IDE software.
A function is a container for statements (lines of code) that tell the Arduino to do certain jobs. The Arduino language has two built-in functions: setup and loop. The setup function is shown below. The Arduino executes the statements you put between the setup function’s curly braces, but only once at the beginning of the sketch.
In this example, both statements are function calls to functions in the Arduino’s built-in Serial pre-written code library: Serial.begin(speed) and Serial.print(val). Here, speed and val are parameters, each describing a value that its function needs passed to it to do its job. The sketch provides these values inside parentheses in each function call.
Serial.begin(9600); passes the value 9600 to the speed parameter. This tells the Arduino to get ready to exchange messages with the Serial Monitor at a data rate of 9600 bits per second. That’s 9600 binary ones or zeros per second, and is commonly called a baud rate.
Serial.print(val); passes the message “Hello!” to the val parameter. This tells the Arduino to send a series of binary ones and zeros to the Serial Monitor. The monitor decodes and displays that serial bitstream as the “Hello!” message.
After the setup function is done, the Arduino automatically skips to the loop function and starts doing what the statements in its curly braces tell it to do. Any statements in loop will be repeated over and over again, indefinitely. Since all this sketch is supposed to do is print one "Hello!" message, the loop function doesn’t have any actual commands. There’s just a notation for other programmers to read, called a comment. Anything to the right of // on a given line is for programmers to read, not for the Arduino software’s compiler. (A compiler takes your sketch code and converts it into numbers—a microcontroller’s native language.)
What is void? Why do these functions end in ()? The first line of a function is its definition, and it has three parts: return type, name, and parameter list. For example, in the function void setup() the return type is void, the name is setup, and the parameter list is empty – there’s nothing inside the parentheses (). Void means ‘nothing’—when another function calls setup or loop, these functions would not return a value. An empty parameter list means that these functions do not need to receive any values when they are called to do their jobs.
Microcontroller programs generally run in a loop, meaning that one or more statements are repeated over and over again. Remember that the loop function automatically repeats any code in its block (the statements in between its curly braces). Let’s try moving Serial.print("Hello!"); to the loop function. To slow down the rate at which the messages repeat, let’s also add a pause with the built-in delay(ms) function.
The added line delay(1000) passes the value 1000 to the delay function’s ms parameter. It’s requesting a delay of 1000 milliseconds. 1 ms is 1/1000 of a second. So, delay(1000) makes the sketch wait for 1000/1000 = 1 second before letting it move on to the next line of code.
How about having each "Hello!" message on a new line? That would make the messages scroll down the Serial Monitor, instead of across it. All you have to do is change print to println, which is short for ‘print line.’
Still have questions? Try the Arduino Language Reference. It’s a set of pages with links you can follow to learn more about setup, loop, print, println, delay, and lots of other functions you can use in your sketches.
If you are using Codebender, go to https://www.arduino.cc/en/Reference/HomePage. [3]
Links
[1] http://learn.parallax.com/tutorials/activity-2-codebender-hello-sketch
[2] http://learn.parallax.com/node/133
[3] https://www.arduino.cc/en/Reference/HomePage