![Thames & Kosmos Code Gamer Скачать руководство пользователя страница 41](http://html1.mh-extra.com/html/thames-and-kosmos/code-gamer/code-gamer_experiment-manual_1099224041.webp)
Infrasound
Audible
Frequencies
Ultrasound
</>
Random sounds!
You just learned in the “Cheep!” project how to make your
KosmoDuino start beeping. But it can get a little boring
listening to the same tone over and over. Here’s where you
learn how to get random sounds out of your controller.
YOU WILL NEED
› KosmoDuino in the interaction board
THE PLAN
The random generator will determine the pitch of the
tones as well as their duration.
THE PROGRAM
The program is pretty simple. In
setup()
, you give the
random generator a starting value just as you did in the
die-rolling project.
Then, in the main loop,
int freq = random(110,1000);
randomly determines the frequency (
freq
) of the next
tone, and then its duration in the same manner.
Finally, the tone is output with
tone(buzzerPin,freq);
.
At the end,
delay(dauer);
ensures that the tone is played
for the duration indicated by
duration
.
#include <KosmoBits_Pins.h>
const int buzzerPin = KOSMOBITS_BUZZER_PIN;
void setup() {
randomSeed(analogRead(12));
}
void loop() {
int freq = random(110, 1000);
int duration = random(10, 150);
tone(buzzerPin, freq);
delay(duration);
}
!
SOUND
When the air pushing against our ear drums vibrates back
and forth, we perceive it as sound. The faster the air
vibrates, the higher the sound. Physicists measure that as
“vibrations per second,” or frequency. The unit of frequency
is known as a Hertz, abbreviated Hz. One Hertz means that
the air completes one full oscillation (vibration back and
forth) per second. The higher the frequency of a sound, the
higher the pitch of the sound we hear. Humans can perceive
sounds with frequencies between 16 and 20,000 Hertz. A
lot of animals, on the other hand, are able to perceive
sounds in the infrasonic and ultrasonic range.
39
CodeGamer
PROJECT 11
CodeGamer manual inside english.indd 39
7/19/16 12:32 PM