![Thames & Kosmos Code Gamer Скачать руководство пользователя страница 40](http://html1.mh-extra.com/html/thames-and-kosmos/code-gamer/code-gamer_experiment-manual_1099224040.webp)
</>
Cheep!
You can also use the interaction board to produce sounds.
This little project will show you how it works.
YOU WILL NEED
› KosmoDuino in the interaction board
THE PLAN
So far, the KosmoDuino just beeps at regular intervals.
Don’t worry — you will learn how to turn it into a siren or
even a musical instrument.
THE PROGRAM
Start by including the
KosmoBits_Pins.h
file and
defining the
buzzerPin
constant in order to address the
pin to which the “buzzer” (the miniature speaker) is
connected.
#include <KosmoBits_Pins.h>
const int buzzerPin = KOSMOBITS_BUZZER_PIN;
void setup() {
// Nothing to do.
}
void loop() {
tone(buzzerPin, 440);
delay(500);
noTone(buzzerPin);
delay(500);
}
This time, you won’t have to do anything in
setup()
.
The composition of the
loop()
will probably look
familiar to you. It matches the “Blink” program, which
was the very first program that you uploaded to your
KosmoDuino. But this time, instead of making an LED
blink, you will be making sounds. These two commands
will help:
tone(buzzerPin, 440);
produces a tone with a
frequency of 440 Hz. This is the standard pitch of A. The
tone stays on until you switch it off again with
noTone(buzzerPin);
To switch the tone on and off again
at regular intervals, use
delay(500);
to attach a half-
second pause to each of these commands.
C
h
e
ep
!
C
h
e
ep
!
38
PROJECT 10
CodeGamer manual inside english.indd 38
7/19/16 12:32 PM