Thames & Kosmos Code Gamer Experiment Manual page 15

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

PROJECT 1
What happens when
loop()
instruction in
loop()
digitalWrite(ledPin, HIGH);
You can use
digitalWrite()
is applied to an output pin or not. When issuing this
request, there are two things that you will have to
communicate to the
digitalWrite()
1. Which pin is intended?
2. Should the voltage at the pin be switch on (
off (
)?
LOW
In this case, voltage is switched on at pin number 13
(
), and the LED lights up!
ledPin
If you want to switch on another pin, let's say pin
number 9, you would write
The next instruction is:
delay(500);
.
You can use the
delay()
sequence for a specific period of time — in other words, to
make it wait. The amount of time is determined by the
number that you assign with the request, in this case
The time is indicated in milliseconds (thousandths of a
second). 500 milliseconds is half a second. So the program
waits and does nothing for half a second.
TYPES
A few common type specifiers and their meanings
TYPE
MEANING
Whole numbers or HIGH / LOW (on/off)
int
long
Whole numbers, large
Floating decimal numbers: numbers with a
float
decimal point
double
float
Like
Individual letters
char
Unchangeable value
const
CodeGamer manual inside english.indd 13
is invoked? The first
is:
to control whether voltage
function:
) or
HIGH
digitalWrite(9, HIGH);
instruction to delay the program
but with twice the precision
Then we have the instruction
digitalWrite(ledPin, LOW);
You can probably understand this one without any help.
Right — the LED is switched off again!
Then the KosmoDuino waits another half a second:
delay(500);
.
And then?
Then it starts all over again from the beginning!
a special function in the Arduino programming. It repeats
endlessly! All commands in
and over in an endless loop. This is known as the main
loop. This makes
loop()
any KosmoBits program. It controls what the KosmoDuino
actually does, while
.
preliminary steps.
You have already seen what happens when the main loop
is repeated: The LED is switched on and off, over and over,
in regular intervals of half a second. In other words — the
LED blinks.
.
500
VALUES
-32,768 to 32,767
-2,147,483,648 to 2,147,483,647
e.g., 1.5; 3.141; 2.678
e.g., 3.141964; 21.45873
a
A
c
C
e.g.,
;
;
;
Can take any values
CodeGamer
.
loop()
are carried out over
loop()
the most important function in
setup()
handles the required
is
!
13
7/19/16 12:31 PM

Advertisement

Table of Contents
loading

Table of Contents