add tone happy birthday example
this is mainly for testing functionality when refactoring tone()
This commit is contained in:
1 parent
ac9063395d
commit
3aed8f784b
1 file changed
+146
+146
@@ -0,0 +1,146 @@
|
||||
// Circuit Playground Birthday Candles
|
||||
//
|
||||
// All the NeoPixels will flicker like candles and the speaker will
|
||||
// play the tune to Happy Birthday continuously. Blow on the board
|
||||
// to slowly blow out all the candles and hear a victory song!
|
||||
// Controls:
|
||||
// - Slide switch: Move this to +/on to hear music or -/off to disable sound.
|
||||
// - Left button: Hold this down and press reset to use a rainbow flicker color.
|
||||
// - Right button: Hold this down and press reset to use a simple solid color (no flicker).
|
||||
// - If neither left or right button are pressed at startup then a flame-like
|
||||
// flickering animation will be used.
|
||||
// After all the candles are blown out press reset to reset and start over!
|
||||
//
|
||||
// Author: Tony DiCola
|
||||
// License: MIT (https://opensource.org/licenses/MIT)
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
// PIN_BUZZER should be defined by the supported variant e.g CPlay Bluefruit or CLUE.
|
||||
// Otherwise please define the pin you would like to use for tone output
|
||||
#ifndef PIN_BUZZER
|
||||
#define PIN_BUZZER 5
|
||||
#endif
|
||||
|
||||
|
||||
// A few music note frequencies as defined in this tone example:
|
||||
// https://www.arduino.cc/en/Tutorial/toneMelody
|
||||
#define NOTE_C4 262
|
||||
#define NOTE_CS4 277
|
||||
#define NOTE_D4 294
|
||||
#define NOTE_DS4 311
|
||||
#define NOTE_E4 330
|
||||
#define NOTE_F4 349
|
||||
#define NOTE_FS4 370
|
||||
#define NOTE_G4 392
|
||||
#define NOTE_GS4 415
|
||||
#define NOTE_A4 440
|
||||
#define NOTE_AS4 466
|
||||
#define NOTE_B4 494
|
||||
#define NOTE_C5 523
|
||||
#define NOTE_CS5 554
|
||||
#define NOTE_D5 587
|
||||
#define NOTE_DS5 622
|
||||
#define NOTE_E5 659
|
||||
#define NOTE_F5 698
|
||||
#define NOTE_FS5 740
|
||||
#define NOTE_G5 784
|
||||
#define NOTE_GS5 831
|
||||
#define NOTE_A5 880
|
||||
#define NOTE_AS5 932
|
||||
#define NOTE_B5 988
|
||||
|
||||
// Define note durations. You only need to adjust the whole note
|
||||
// time and other notes will be subdivided from it directly.
|
||||
#define WHOLE 2200 // Length of time in milliseconds of a whole note (i.e. a full bar).
|
||||
#define HALF WHOLE/2
|
||||
#define QUARTER HALF/2
|
||||
#define EIGHTH QUARTER/2
|
||||
#define EIGHTH_TRIPLE QUARTER/3
|
||||
#define SIXTEENTH EIGHTH/2
|
||||
|
||||
// Play a note of the specified frequency and for the specified duration.
|
||||
// Hold is an optional bool that specifies if this note should be held a
|
||||
// little longer, i.e. for eigth notes that are tied together.
|
||||
// While waiting for a note to play the waitBreath delay function is used
|
||||
// so breath detection and pixel animation continues to run. No tones
|
||||
// will play if the slide switch is in the -/off position or all the
|
||||
// candles have been blown out.
|
||||
void playNote(int frequency, int duration, bool hold=false, bool measure=true) {
|
||||
(void) measure;
|
||||
|
||||
if (hold) {
|
||||
// For a note that's held play it a little longer than the specified duration
|
||||
// so it blends into the next tone (but there's still a small delay to
|
||||
// hear the next note).
|
||||
tone(PIN_BUZZER, frequency, duration + duration/32);
|
||||
}
|
||||
else {
|
||||
// For a note that isn't held just play it for the specified duration.
|
||||
tone(PIN_BUZZER, frequency, duration);
|
||||
}
|
||||
|
||||
delay(duration + duration/16);
|
||||
}
|
||||
|
||||
// Song to play when the candles are blown out.
|
||||
void celebrateSong() {
|
||||
// Play a little charge melody, from:
|
||||
// https://en.wikipedia.org/wiki/Charge_(fanfare)
|
||||
// Note the explicit boolean parameters in particular the measure=false
|
||||
// at the end. This means the notes will play without any breath measurement
|
||||
// logic. Without this false value playNote will try to keep waiting for candles
|
||||
// to blow out during the celebration song!
|
||||
playNote(NOTE_G4, EIGHTH_TRIPLE, true, false);
|
||||
playNote(NOTE_C5, EIGHTH_TRIPLE, true, false);
|
||||
playNote(NOTE_E5, EIGHTH_TRIPLE, false, false);
|
||||
playNote(NOTE_G5, EIGHTH, true, false);
|
||||
playNote(NOTE_E5, SIXTEENTH, false);
|
||||
playNote(NOTE_G5, HALF, false);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// Initialize serial output and Circuit Playground library.
|
||||
Serial.begin(115200);
|
||||
|
||||
pinMode(PIN_BUZZER, OUTPUT);
|
||||
digitalWrite(PIN_BUZZER, LOW);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Play happy birthday tune, from:
|
||||
// http://www.irish-folk-songs.com/happy-birthday-tin-whistle-sheet-music.html#.WXFJMtPytBw
|
||||
// Inside each playNote call it will play a note and drive the NeoPixel animation
|
||||
// and check for a breath against the sound sensor. Once all the candles are blown out
|
||||
// the playNote calls will stop playing music.
|
||||
playNote(NOTE_D4, EIGHTH, true);
|
||||
playNote(NOTE_D4, EIGHTH);
|
||||
playNote(NOTE_E4, QUARTER); // Bar 1
|
||||
playNote(NOTE_D4, QUARTER);
|
||||
playNote(NOTE_G4, QUARTER);
|
||||
playNote(NOTE_FS4, HALF); // Bar 2
|
||||
playNote(NOTE_D4, EIGHTH, true);
|
||||
playNote(NOTE_D4, EIGHTH);
|
||||
playNote(NOTE_E4, QUARTER); // Bar 3
|
||||
playNote(NOTE_D4, QUARTER);
|
||||
playNote(NOTE_A4, QUARTER);
|
||||
playNote(NOTE_G4, HALF); // Bar 4
|
||||
playNote(NOTE_D4, EIGHTH, true);
|
||||
playNote(NOTE_D4, EIGHTH);
|
||||
playNote(NOTE_D5, QUARTER); // Bar 5
|
||||
playNote(NOTE_B4, QUARTER);
|
||||
playNote(NOTE_G4, QUARTER);
|
||||
playNote(NOTE_FS4, QUARTER); // Bar 6
|
||||
playNote(NOTE_E4, QUARTER);
|
||||
playNote(NOTE_C5, EIGHTH, true);
|
||||
playNote(NOTE_C5, EIGHTH);
|
||||
playNote(NOTE_B4, QUARTER); // Bar 7
|
||||
playNote(NOTE_G4, QUARTER);
|
||||
playNote(NOTE_A4, QUARTER);
|
||||
playNote(NOTE_G4, HALF); // Bar 8
|
||||
|
||||
celebrateSong();
|
||||
|
||||
// One second pause before repeating the loop and playing
|
||||
delay(1000);
|
||||
}
|
||||
Reference in new issue
Block a user