EasyManua.ls Logo

Thames & Kosmos Code Gamer - BUT WHAT DOES int MEAN?; FUNCTIONS

Thames & Kosmos Code Gamer
66 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
Here, a function is defined. The function is called
s e tu p()
.
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.
The function
s e tu p()
, 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
s e tu p()
function actually do? It
invokes another function:
pi n M o d e()
. This is the function
that allows you to specify the operating mode in which a
pin is going to work. In this case, the
ledPin
(pin number
13) is to work as
OUTPUT
, or the output pin.
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
s e tu p()
have been processed by
your KosmoDuino, the next thing to be invoked is the
lo o p()
function. Let’s take a look at what this function
contains:
!
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
void
, meaning “empty.” In other words, this function returns no value at all! An example
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.
variable
ledPin
. The way you say it is: “The value 13 is
assigned to the variable
ledPin
.”
Wherever you want to use the number 13 in your program,
you can now write “ledPin” instead of “13.
BUT WHAT DOES
int
MEAN?
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
int
. That means that the variable can take a so-
called integer value.
For your KosmoDuino, those values are the whole numbers
from -32768 to 32767.

PROJECT 1
CodeGamer manual inside english.indd 12 7/19/16 12:31 PM