Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Simple C tutorial for Arduino, based on example program Blink.

ino
Comments in the program are explanation for the human reader, not instructions to the
computer.

1. Comments can be indicated in 2 ways: Everything between “/*” and the closing
characters “*.” Is a comment.
2. Everything on a line after the characters “//” are comments.

Arduino programs are made up of 2 subroutines: void setup() { } which is executed only
once, when the program begins (either processor is reset, turned on, or program is reloaded), and
void loop() { }. All statements in the braces for loop are executed over and over forever, until
the reset button for the processor is pushed, or the power is shut off. The declarations include the
word “void” because these subroutines do not return any values to the calling routine.

A subroutine consists of a sequence of statements that is executed when the subroutine is called.
These statements are located between the curly braces “ { }”.

Spaces and tabs are ignored. Formatting to make the program look nice is for the human reader
only.

Each statement must be terminated by a semicolon.

Parameters to a subroutine are given in the parentheses after the subroutine name.

All variables have to be declared before use, e.g., “int led = 13;” in the program below. The
variable type (int, float, char, etc.) must be given, followed by the variable name. The initial
value of the variable can optionally be given. Note that each variable declaration is a statement
that must end with a semicolon.

The subroutine setup has one statement, which calls the subroutine pinMode, which has 2
arguments in the parentheses: led, Output. Calling this subroutine specifies that pin 13 will be
used as an output pin, instead of an input.

Within loop, we therefore see that 2 more subroutines are called, digitalWrite and delay.
digitalWrite has 2 arguments, the pin number and HIGH or LOW, specifying the voltage level to
set for that pin. Delay has one argument, which is a number in milliseconds. Thus, a delay of
1000mS = 1 sec.
Below is the example program, Blink.ino:

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.


*/

// Pin 13 has an LED connected on most Arduino boards.


// give it a name:
int led = 13;

// the setup routine runs once when you press reset:


void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:


void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

Now try modifying the program. You can save your trial program under the same name (Blink)
by clicking the menu item “File, Save” or using the keyboard shortcut “Control – S.” You can
save your trial program under another name by using the menu item “File, Save As.”

1. Change the program to change the times for the LED to be on versus off. For
example, you can try turning it on for 1.5sec, and off for 0.5sec.
2. Duplicate some lines of the code so that the LED turns on for 2 sec, off for 1 sec, on
for 1 sec, and off for 0.5 sec.
3. Make up your own variation of times for LED on and off.
4. Connect additional LEDS (several colors available) to other pins (see figure in lab
writeup). Rewrite program to turn them on or off.

You might also like