Thames & Kosmos Code Gamer Experiment Manual page 18

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

PROJECT 3
EXPLANATION
Let's just take a look at the
place where anything was changed relative to the "off switch" program.
Just as with the "off switch," you start by reading out the
and saving the current value, i.e.,
switchPin
in the
LOW
switchValue
But you do not want to write the read value into the LED
pin — just the opposite: When
value of
, you want to switch on the LED
LOW
(
digitalWrite(ledPin, HIGH)
switch off the LED (
digitalWrite(ledPin, LOW)
In the program text, you express that with
if(...){...} else{...}
guessed it — this lets you use the program to respond to
different conditions.
The condition to which you are responding here is
switchValue == LOW
question: "Is the switch value equal to LOW?" You can
evaluate the answer to that with the
the
is equal to
switchValue
(
if(switchValue == LOW)
pair of curly brackets is carried out:
digitalWrite(ledPin, HIGH)
Otherwise (
) the part in the second pair of curly
else
brackets is carried out:
LED is switched off.
Finally,
delay(50)
will make you wait briefly before the
entire sequence repeats.
!
JUMPER WIRES
Jumper wires serve as an easy way to connect electronic
components to each other. In this experiment kit, you will
find two different types: male-male and male-female.
They really are called that! "Male-male" means that there
is a wire on both ends. While "male-female," on the other
hand, there is a wire on one end and a socket on the other.
Jumper wires are sometimes also called patch cables.
16
CodeGamer manual inside english.indd 16
loop()
function, because that's the only
HIGH
variable.
has the
switchValue
); otherwise, you want to
).
. Note the "if" and "else." You
. You can understand that as a
instruction: If
if()
LOW
), the program code in the first
. The LED is switched on.
digitalWrite(ledPin, LOW)
void loop() {
or
switchValue = digitalRead(switchPin);
if(switchValue == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(50);
}
. The
» Female End«
</>
» Male End«
7/19/16 12:31 PM

Advertisement

Table of Contents
loading

Table of Contents