Project 1: Blink - Thames & Kosmos Code Gamer Experiment Manual

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

PROJECT 1
PROGRAM CODE EXPLAINED:
This is where the explanation for the program code is
written. Portions taken from the code are always
highlighted in
orange
Your first program: Blink!
To write your first program, launch the Arduino
environment on your computer. A window will open into
which you can enter your program code. A few lines have
already been entered for you:
That is the basic skeleton for every Arduino program:
1. Your KosmoDuino starts by processing all instructions
function, i.e., all functions written in the
in the
setup()
curly brackets following
2. Then, the
loop()
function is invoked. That means that
your microcontroller executes all instructions written in
the curly brackets following
instruction has been processed, your controller once
again invokes the
loop()
instructions in
loop()
endless loop. That is why it's called a loop function.
You can visualize this idea a little more clearly in a flow
diagram. The program always proceeds in the direction of
the arrows: from switching on, to
then back to
loop()
again and again.
You could upload this program right away to your
KosmoDuino if you wanted. But since there are no
instructions in either
wouldn't do anything.
So why not give your KosmoDuino something to do? Try
changing the program code like this:
CodeGamer manual inside english.indd 9
.
setup()
.
loop()
. Once the final
function. In other words, the
are invoked over and over in an
setup()
, to
loop()
or
, the program
setup()
loop()
Here is where the program code is written
// Comments on the code are always in gray.
// This text is not a functional part of the
// code. It provides explanation and clarity.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
ARDUINO SWITCHED ON
and
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
CodeGamer
</>
setup()
loop()
▲ Flow diagram
!
9
7/19/16 12:31 PM

Advertisement

Table of Contents
loading

Table of Contents