Project 12: Siren - Thames & Kosmos Code Gamer Experiment Manual

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

PROJECT 12
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
THE PROGRAM
Here's where you specify the frequency range within
which the sound pitch is to move.
to the deepest sound of the siren, and
highest. You can also experiment with these parameters
by changing the values, and find a siren noise to suit your
taste.
determines how long any given sound will be held.
DELAY
The variable
finally, saves the current frequency.
freq
The siren starts at
FREQ_MIN
This time, you won't have to do anything in
The main loop starts by outputting a sound with the
frequency
freq
(1). After a pause of length
frequency
freq
is raised by 1 with
instruction then checks whether the maximum frequency
has been exceeded. If so, the frequency
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.
40
CodeGamer manual inside english.indd 40
FREQ_MIN
corresponds
the
FREQ_MAX
.
setup()
.
, the
DELAY
++freq;
. The
if
freq
is returned to
.
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.
#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;
// that is output.
void setup() {
// Nothing to do here.
}
void loop() {
tone(buzzerPin, freq);
delay(DELAY);
++freq;
// Short for: freq = freq + 1;
if (freq > FREQ_MAX) {
freq = FREQ_MIN;
}
}
</>
// Frequency of the sound
// (1)
7/19/16 12:32 PM

Advertisement

Table of Contents
loading

Table of Contents