diff options
| author | Sudar <sudar@sudarmuthu.com> | 2014-09-21 12:05:12 +0530 |
|---|---|---|
| committer | Sudar <sudar@sudarmuthu.com> | 2014-09-21 12:05:12 +0530 |
| commit | ee1855c6b1aabf4d50afcfba69cecd2417d27237 (patch) | |
| tree | 05db0503f3cb1ffbd41f077ecf6135a429d5b66b /examples/toneMelody/toneMelody.ino | |
| parent | 6f46722abb40c60dcaa0e40292342d015906c8bf (diff) | |
| parent | d092c14d7ec845246f6588704e22718615626982 (diff) | |
Merge pull request #268 from peplin/259-move-examples
Move tests back to 'examples', skip non-testable examples when testing.
Fix #259
Fix #260
Diffstat (limited to 'examples/toneMelody/toneMelody.ino')
| -rw-r--r-- | examples/toneMelody/toneMelody.ino | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/toneMelody/toneMelody.ino b/examples/toneMelody/toneMelody.ino new file mode 100644 index 0000000..8593ab7 --- /dev/null +++ b/examples/toneMelody/toneMelody.ino @@ -0,0 +1,49 @@ +/* + Melody + + Plays a melody + + circuit: + * 8-ohm speaker on digital pin 8 + + created 21 Jan 2010 + modified 30 Aug 2011 + by Tom Igoe + +This example code is in the public domain. + + http://arduino.cc/en/Tutorial/Tone + + */ + #include "pitches.h" + +// notes in the melody: +int melody[] = { + NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4}; + +// note durations: 4 = quarter note, 8 = eighth note, etc.: +int noteDurations[] = { + 4, 8, 8, 4,4,4,4,4 }; + +void setup() { + // iterate over the notes of the melody: + for (int thisNote = 0; thisNote < 8; thisNote++) { + + // to calculate the note duration, take one second + // divided by the note type. + //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. + int noteDuration = 1000/noteDurations[thisNote]; + tone(8, melody[thisNote],noteDuration); + + // to distinguish the notes, set a minimum time between them. + // the note's duration + 30% seems to work well: + int pauseBetweenNotes = noteDuration * 1.30; + delay(pauseBetweenNotes); + // stop the tone playing: + noTone(8); + } +} + +void loop() { + // no need to repeat the melody. +} |
