![Thames & Kosmos Code Gamer Скачать руководство пользователя страница 16](http://html1.mh-extra.com/html/thames-and-kosmos/code-gamer/code-gamer_experiment-manual_1099224016.webp)
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.
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);
}
THE PLAN
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.
THE PROGRAM
So what does the program do?
int ledPin = 13;
int switchPin = 9;
int switchValue = HIGH;
You will first have to define three variables:
•
ledPin
for the pin to which the LED is connected.
•
switchPin
should be assigned the value 9, because you
will be connecting your “switch” to pin 9.
•
switchValue
will start by getting the value
HIGH
.
Later on, you will “read out” pin 9 and save the readout
value in
switchValue
.
14
PROJECT 2
Warning! Not suitable for children under 8 y
ears.
There is a risk of hot surfaces of compon
ents on the
PCB (printed circuit board) when differe
nt polarities
are incorrectly short-circuited or the cap
acitor is
subject to fault conditions.
CodeGamer manual inside english.indd 14
7/19/16 12:31 PM