Debian Codegraph @

examples/02.Digital/toneMelody/toneMelody.ino

raw
1 /*
2 Melody
3
4 Plays a melody
5
6 circuit:
7 - 8 ohm speaker on digital pin 8
8
9 created 21 Jan 2010
10 modified 30 Aug 2011
11 by Tom Igoe
12
13 This example code is in the public domain.
14
15 https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody
16 */
17
18 #include "pitches.h"
19
20 // notes in the melody:
21 int melody[] = {
22 NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
23 };
24
25 // note durations: 4 = quarter note, 8 = eighth note, etc.:
26 int noteDurations[] = {
27 4, 8, 8, 4, 4, 4, 4, 4
28 };
29
30 void setup() {
31 // iterate over the notes of the melody:
32 for (int thisNote = 0; thisNote < 8; thisNote++) {
33
34 // to calculate the note duration, take one second divided by the note type.
35 //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
36 int noteDuration = 1000 / noteDurations[thisNote];
37 tone(8, melody[thisNote], noteDuration);
38
39 // to distinguish the notes, set a minimum time between them.
40 // the note's duration + 30% seems to work well:
41 int pauseBetweenNotes = noteDuration * 1.30;
42 delay(pauseBetweenNotes);
43 // stop the tone playing:
44 noTone(8);
45 }
46 }
47
48 void loop() {
49 // no need to repeat the melody.
50 }