Project 2: Off Switch - Thames & Kosmos Code Gamer Experiment Manual

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

PROJECT 2
Off switch
In your first "blink" sketch, you learned how to use a
program to make an LED blink on your KosmoDuino. To do
that, you used a pin on your controller as an output pin to
make the LED switch on and off.
But you can also use a pin as an input pin. In this case, the
pin is not turning voltage on or off. Instead, the program
can tell you whether or not an electrical voltage is
supplied to the pin.
You can then use that to turn the LED on and off with a
simple switch.
YOU WILL NEED
› KosmoDuino
› 2 male-male jumper wires (see explanation on p. 16)
PREPARATION
Attach one jumper wire to pin 9 and the other to one of the
GND pins. GND stands for ground or grounding. These pins
have no actual function beyond conveying current.
THE PROGRAM
So what does the program do?
You will first have to define three variables:
for the pin to which the LED is connected.
ledPin
should be assigned the value 9, because you
switchPin
will be connecting your "switch" to pin 9.
will start by getting the value
switchValue
Later on, you will "read out" pin 9 and save the readout
value in
switchValue
14
CodeGamer manual inside english.indd 14
.
HIGH
.
THE PLAN
int ledPin = 13;
int switchPin = 9;
int switchValue = HIGH;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
}
void loop() {
switchValue = digitalRead(switchPin);
digitalWrite(ledPin, switchValue);
delay(50);
}
Upload the sketch to your KosmoDuino as described on
page 10. After uploading, the LED will light up. If you then
bring the two free jumper wire contacts together and
make them touch, the LED will stop shining.
int ledPin = 13;
int switchPin = 9;
int switchValue = HIGH;
7/19/16 12:31 PM

Advertisement

Table of Contents
loading

Table of Contents