![Thames & Kosmos Code Gamer Скачать руководство пользователя страница 25](http://html1.mh-extra.com/html/thames-and-kosmos/code-gamer/code-gamer_experiment-manual_1099224025.webp)
</>
In
setup()
, you use
pinMode()
to set the operating
mode for the button pins. Since the buttons are connected
to the pins via a resistor, you will use the
INPUT
operating type.
In the first
if
instruction
digitalRead()
is used to read
out the pin to which button 1 is connected. When the
button is pressed, it yields the value
LOW
: The value
255
,
i.e., the largest possible value, is assigned to
red
.
If the button is not pressed,
red
is set to
0
.
The entire sequence is repeated for button 2 in the second
if
instruction. The only difference is that the value for
blue
is given.
Finally,
pixel.setColor(red, green, blue, brightness);
sets the new color value for the NeoPixel.
At the end of the main loop, you make the controller wait
another 50 milliseconds.
void setup() {
pinMode(button1, INPUT);
pinMode(button2, INPUT);
}
void loop() {
if (digitalRead(button1) == LOW) {
red = 255;
} else {
red = 0;
}
if (digitalRead(button2) == LOW) {
blue = 255;
} else {
blue = 0;
}
pixel.setColor(red, green, blue, brightness);
delay(50);
}
!
WHAT ACTUALLY HAPPENS WHEN YOU UPLOAD SOMETHING?
When you upload a program to your KosmoDuino, actually, a whole lot of things happen. The microcontroller does not
really understand the program as you have written it. It simply doesn’t speak the programming language. That’s why
the program first has to be translated into
machine language. That is something handled by a so-called compiler.
The programming language that you use, in other words, is just an intermediate language that is not only easy for you to
understand and write, but most important of all, is one that the compiler can easily translate into machine language.
23
CodeGamer
CodeGamer manual inside english.indd 23
7/19/16 12:32 PM