Project 11: Random Sounds - Thames & Kosmos Code Gamer Experiment Manual

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

PROJECT 11
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
random generator a starting value just as you did in the
die-rolling project.
Then, in the main loop,
randomly determines the frequency (
tone, and then its duration in the same manner.
Finally, the tone is output with
At the end,
delay(dauer);
for the duration indicated by
Infrasound
CodeGamer manual inside english.indd 39
setup()
, you give the
int freq = random(110,1000);
freq
) of the next
tone(buzzerPin,freq);
ensures that the tone is played
.
duration
Audible
Ultrasound
Frequencies
#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.
CodeGamer
</>
!
39
7/19/16 12:32 PM

Advertisement

Table of Contents
loading

Table of Contents