EasyManua.ls Logo

Thames & Kosmos Code Gamer - PROJECT 12: Siren; THE PROGRAM

Thames & Kosmos Code Gamer
66 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
</>
Siren
Now it’s time to bring some order to the acoustic chaos.
This project will let you make your KosmoDuino howl like
a siren.
YOU WILL NEED
› KosmoDuino
in the interaction board
Here’s where you specify the frequency range within
which the sound pitch is to move.
FREQ_MIN
corresponds
to the deepest sound of the siren, and
FREQ_MAX
the
highest. You can also experiment with these parameters
by changing the values, and find a siren noise to suit your
taste.
DELAY
determines how long any given sound will be held.
The variable
freq
finally, saves the current frequency.
The siren starts at
FREQ_MIN
.
#include <KosmoBits_Pins.h>
const int buzzerPin = KOSMOBITS_BUZZER_PIN;
const int FREQ_MIN = 440;
const int FREQ_MAX = 660;
const int DELAY = 4;
int freq = FREQ_MIN; // Frequency of the sound
// that is output.
void setup() {
// Nothing to do here.
}
void loop() {
tone(buzzerPin, freq); // (1)
del ay(DEL AY);
++freq; // Short for: freq = freq + 1;
if (freq > FREQ_MAX) {
freq = FREQ_MIN;
}
}
This time, you won’t have to do anything in
s e tu p()
.
The main loop starts by outputting a sound with the
frequency
freq
(1). After a pause of length
DELAY
, the
frequency
freq
is raised by 1 with
++freq;
. The
if
instruction then checks whether the maximum frequency
has been exceeded. If so, the frequency
freq
is returned to
the starting value
FREQ_MIN
.
In the next project, you will be producing an entire musical
scale. But first you will have to learn about an important
programming tool: Arrays.
THE PLAN
With every pass through the main loop, a new sound will
be output. Each time, the pitch will be raised a little. Once
a certain frequency, or sound pitch, is attained, the pitch
drops back to its starting value and the whole thing starts
over from the beginning.
THE PROGRAM

PROJECT 12
CodeGamer manual inside english.indd 40 7/19/16 12:32 PM