Thames & Kosmos Code Gamer Experiment Manual page 14

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

PROJECT 1
variable
. The way you say it is: "The value 13 is
ledPin
assigned to the variable
Wherever you want to use the number 13 in your program,
you can now write "ledPin" instead of "13."
BUT WHAT DOES
In the Arduino programming language, a variable cannot
take just any value you like. The values must be of a
certain type that has to be determined. In this case, the
type is
. That means that the variable can take a so-
int
called integer value.
For your KosmoDuino, those values are the whole numbers
from -32768 to 32767.
void setup() {
pinMode(ledPin, OUTPUT);
}
Here, a function is defined. The function is called
When a function is invoked in the program code, all the
instructions contained in it are carried out step by step.
Those are all the instructions written between the curly
brackets. So you can think of a function as a small
sub-program.
!
FUNCTIONS
Functions allow you to divide up your programs into smaller blocks. That can help you keep a clear overview, and it
promotes order in the program. Also, you can pack frequently used sequences of instructions into a single function.
Instead of repeating the same text in the program code every time, you can simply invoke the corresponding function.
Just as with the definition of a variable, to define a function you have to start with an example. In this case, let's take
.
void
This is the so-called return type. A function, in other words, can end by returning a value to the program that invoked it.
In this case, the return type is
of a value that might be returned is the temperature measured by a sensor.
Important: Always give your functions a name that conveys what the function does. That will help you understand the
program when you're reading it.
12
CodeGamer manual inside english.indd 12
."
ledPin
MEAN?
int
setup()
, meaning "empty." In other words, this function returns no value at all! An example
void
The function
setup()
, by the way, is a special function. It
is always the first thing that is automatically invoked
whenever you start your KosmoDuino.
But what does the
setup()
invokes another function:
that allows you to specify the operating mode in which a
pin is going to work. In this case, the
13) is to work as
, or the output pin.
OUTPUT
An output pin works like a switch that you turn on or off
as determined by the program. You will learn about the
other possible operating modes in later projects.
If the instructions in
setup()
your KosmoDuino, the next thing to be invoked is the
loop()
function. Let's take a look at what this function
contains:
void loop() {
digitalWrite(ledPin, HIGH);
delay(500);
.
digitalWrite(ledPin, LOW);
delay(500);
}
function actually do? It
pinMode()
. This is the function
(pin number
ledPin
have been processed by
7/19/16 12:31 PM

Advertisement

Table of Contents
loading

Table of Contents