/* Song creator! Just change: shortest, notes[], durations[], and setup()! Currently, "Flight of the Bumblebee" will play, though the real song with harmony is better. Then, "Imperial March" will play. I was inspired by the code found here: https://gist.github.com/tagliati/1804108 I like that that it makes built-in LED light up. I like that it does not use the tone() function. I like that it puts a pause between notes. Things I did better... - Fixed large errors with timing and pitch. - For creating low-frequency tones when not using tone(), delayMicroseconds() is messy, so I use micros(). - I have more frequencies so that you can make your own song! The compiler ignores frequencies that are never used, so just keep the whole list! - To make it easier to edit and to allow for much longer and complex songs, I put the song into byte arrays stored using PROGMEM. (c) 2018 Bradley Knockel */ // select speaker pin const byte pin = 10; // LED pin const byte LEDpin = 13; // built-in LED // ms of shortest note (all notes are integer multiples of this) float shortest; // a sixteenth note for "Imperial March" and "Flight of the Bumblebee". /* If the sixteenth note is the shortest note and dotted sixteenth notes exist, you should use the time of a 32nd note as the shortest so that a sixteenth note could be 2x this time, and a dotted sixteenth can be 3x this time. All notes must be INTEGER multiples of the shortest. */ // frequency in Hz is... // 440 * 2^((n-58)/12) float frequency(byte n) {return 440.0*pow(2.0,(n-58.0)/12.0);} // a4 is 440 Hz const byte c0 = 1; // 16.35 Hz const byte cs0 = 2; const byte d0 = 3; const byte ds0 = 4; const byte e0 = 5; // lowest above 20 Hz const byte f0 = 6; const byte fs0 = 7; const byte g0 = 8; const byte gs0 = 9; const byte a0 = 10; const byte as0 = 11; const byte b0 = 12; // lowest that works if using tone() const byte c1 = 13; const byte cs1 = 14; const byte d1 = 15; const byte ds1 = 16; const byte e1 = 17; const byte f1 = 18; const byte fs1 = 19; const byte g1 = 20; const byte gs1 = 21; const byte a1 = 22; const byte as1 = 23; const byte b1 = 24; const byte c2 = 25; const byte cs2 = 26; const byte d2 = 27; const byte ds2 = 28; const byte e2 = 29; const byte f2 = 30; const byte fs2 = 31; const byte g2 = 32; const byte gs2 = 33; const byte a2 = 34; const byte as2 = 35; const byte b2 = 36; const byte c3 = 37; const byte cs3 = 38; const byte d3 = 39; const byte ds3 = 40; const byte e3 = 41; const byte f3 = 42; const byte fs3 = 43; const byte g3 = 44; const byte gs3 = 45; const byte a3 = 46; const byte as3 = 47; const byte b3 = 48; const byte c4 = 49; const byte cs4 = 50; const byte d4 = 51; const byte ds4 = 52; const byte e4 = 53; const byte f4 = 54; const byte fs4 = 55; const byte g4 = 56; const byte gs4 = 57; const byte a4 = 58; const byte as4 = 59; const byte b4 = 60; const byte c5 = 61; const byte cs5 = 62; const byte d5 = 63; const byte ds5 = 64; const byte e5 = 65; const byte f5 = 66; const byte fs5 = 67; const byte g5 = 68; const byte gs5 = 69; const byte a5 = 70; const byte as5 = 71; const byte b5 = 72; const byte c6 = 73; const byte cs6 = 74; const byte d6 = 75; const byte ds6 = 76; const byte e6 = 77; const byte f6 = 78; const byte fs6 = 79; const byte g6 = 80; const byte gs6 = 81; const byte a6 = 82; const byte as6 = 83; const byte b6 = 84; const byte c7 = 85; const byte cs7 = 86; const byte d7 = 87; const byte ds7 = 88; const byte e7 = 89; const byte f7 = 90; const byte fs7 = 91; const byte g7 = 92; const byte gs7 = 93; const byte a7 = 94; const byte as7 = 95; const byte b7 = 96; const byte c8 = 97; const byte cs8 = 98; const byte d8 = 99; const byte ds8 = 100; const byte e8 = 101; const byte f8 = 102; const byte fs8 = 103; const byte g8 = 104; const byte gs8 = 105; const byte a8 = 106; const byte as8 = 107; const byte b8 = 108; const byte c9 = 109; const byte cs9 = 110; const byte d9 = 111; const byte ds9 = 112; const byte e9 = 113; const byte f9 = 114; const byte fs9 = 115; const byte g9 = 116; const byte gs9 = 117; const byte a9 = 118; const byte as9 = 119; const byte b9 = 120; // 15804.3 Hz const byte c10 = 121; const byte cs10 = 122; const byte d10 = 123; const byte ds10 = 124; // last one to be below 20000 Hz const byte e10 = 125; const byte f10 = 126; const byte fs10 = 127; const byte g10 = 128; const byte gs10 = 129; const byte a10 = 130; const byte as10 = 131; const byte b10 = 132; // 31608.5 Hz /////////// Imperial March // 0 is a pause. // C++ limits an array to 2^15 = 32768 indices. const byte notes[] PROGMEM = { a4,a4,a4,f4,c5,a4,f4,c5,a4,e5,e5,e5,f5,c5,gs4,f4,c5,a4, // 1st section a5,a4,a4,a5,gs5,g5,fs5,f5,fs5,0,as4,ds5,d5,cs5,c5,b4,c5,0, // 2nd section f4,gs4,f4,a4,c5,a4,c5,e5, // variant 1 a5,a4,a4,a5,gs5,g5,fs5,f5,fs5,0,as4,ds5,d5,cs5,c5,b4,c5,0, // repeat 2nd section f4,gs4,f4,c5,a4,f4,c5,a4 // variant 2 }; // as multiples of shortest const byte durations[] PROGMEM = { 4,4,4,3,1,4,3,1,8,4,4,4,3,1,4,3,1,8, 4,3,1,4,3,1,1,1,2,2,2,4,3,1,1,1,2,2, 2,4,3,1,4,3,1,8, 4,3,1,4,3,1,1,1,2,2,2,4,3,1,1,1,2,2, 2,4,3,1,4,3,1,8 }; /////////// Flight of the Bumblebee const byte notes0[] PROGMEM = { a6,gs6,g6,fs6, g6,fs6,f6,e6, f6,e6,ds6,d6, cs6,c6,b5,as5, a5,gs5,g5,fs5, g5,fs5,f5,e5, f5,e5,ds5,d5, cs5,c5,b4,as4, a4,gs4,g4,fs4, g4,fs4,f4,e4, a4,gs4,g4,fs4, g4,fs4,f4,e4 }; const byte notes1[] PROGMEM = { // should be played twice a4,gs4,g4,fs4, f4,as4,a4,gs4, a4,gs4,g4,fs4, f4,fs4,g4,gs4, a4,gs4,g4,fs4, f4,as4,a4,gs4, a4,gs4,g4,fs4, f4,fs4,g4,gs4, a4,gs4,g4,fs4, g4,fs4,f4,e4, f4,fs4,g4,gs4, a4,as4,a4,gs4, a4,gs4,g4,fs4, g4,fs4,f4,e4, f4,fs4,g4,gs4, a4,b4,c5,cs5, d5,cs5,c5,b4, as4,ds5,d5,cs5, d5,cs5,c5,b4, as4,b4,c5,cs5, d5,cs5,c5,b4, as4,ds5,d5,cs5, d5,cs5,c5,b4, as4,b4,c5,cs5, d5,cs5,c5,b4, c5,b4,as4,a4, as4,b4,c5,cs5, d5,ds5,d5,cs5, d5,cs5,c5,b4, c5,b4,as4,a4, as4,b4,c5,cs5, d5,ds5,d5,cs5, d5,d4,d4,d4, d4,d4,d4,d4, ds4,d4,ds4,ds5, ds4,d4,ds4,ds5, d5,d5,d5,d5, d5,d5,d5,d5, ds5,d5,ds5,ds6, ds5,d5,ds5,ds6, d6,ds5,d5,cs5, d5,ds5,d5,cs5, d5,ds5,d5,cs5, d5,ds5,d5,cs5, d5,ds5,e5,f5, fs5,f5,e5,ds5, d5,ds5,e5,f5, fs5,f5,e5,ds5, d5,g4,g4,g4, g4,g4,g4,g4, gs4,g4,gs4,gs5, gs4,g4,gs4,gs5, g4,g5,g5,g5, g5,g5,g5,g5, gs5,g5,gs5,gs6, gs5,g5,gs5,gs6, g6,gs5,g5,fs5, g5,gs5,g5,fs5, g5,gs5,g5,fs5, g5,gs5,g5,fs5, g5,gs5,a5,as5, b5,as5,a5,gs5, g5,gs5,a5,as5, b5,as5,a5,gs5, g5,fs5,f5,e5, ds5,gs5,g5,fs5, g5,fs5,f5,e5, ds5,e5,f5,fs5, g5,fs5,f5,e5, f5,e5,ds5,d5, ds5,e5,f5,fs5, f5,fs5,g5,gs5, a5,gs5,g5,fs5, g5,fs5,f5,e5, f5,e5,ds5,d5, cs5,c5,b4,as4, a4,as4,a4,gs4, a4,as4,a4,gs4, a4,as4,a4,gs4, a4,as4,a4,gs4, a4,as4,a4,gs4, a4,as4,a4,gs4, a4,as4,a4,gs4, a4,as4,a4,gs4, a4,gs4,g4,fs4, g4,fs4,f4,e4, f4,e4,ds4,d4, cs4,c4,b3,as3, a3,as3,a3,gs3, a3,as3,a3,gs3, a3,as3,a3,gs3, a3,as3,a3,gs3, a3,as3,a3,gs3, a3,as3,a3,gs3, a3,as3,a3,gs3, a3,as3,a3,gs3, a3,as3,b3,c4, cs4,d4,ds4,e4, f4,fs4,g4,gs4, a4,as4,b4,c5, cs5,d5,ds5,e5, f5,fs5,g5,gs5, a5,as5,a5,gs5, a5,as5,a5,gs5 }; const byte notes2[] PROGMEM = { a5,gs5,g5,fs5, f5,as5,a5,gs5, a5,gs5,g5,fs5, f5,fs5,g5,gs5, a5,gs5,g5,fs5, f5,as5,a5,gs5, a5,gs5,g5,fs5, f5,fs5,g5,gs5, a5,gs5,g5,fs5, g5,fs5,f5,e5, f5,fs5,g5,gs5, a5,as5,a5,gs5, a5,gs5,g5,fs5, g5,fs5,f5,e5, f5,fs5,g5,gs5, a5,b5,c6,cs6, d6,cs6,c6,b5, as5,ds6,d6,cs6, d6,cs6,c6,b5, as5,b5,c6,cs6, d6,cs6,c6,b5, as5,ds6,d6,cs6, d6,cs6,c6,b5, as5,b5,c6,cs6, d6,cs6,c6,b5, c6,b5,as5,a5, as5,b5,c6,cs6, d6,ds6,d6,cs6, d6,cs6,c6,b5, as5,b5,c6,cs6, d6,e6,f6,g6, a6,as6,a6,gs6, a6,gs6,g6,fs6, f6,as6,a6,gs6, a6,gs6,g6,fs6, f6,fs6,g6,gs6, a6,gs6,g6,fs6, f6,as6,a6,gs6, a6,gs6,g6,fs6, f6,fs6,g6,gs6 }; const byte notes3[] PROGMEM = { // do only the harmony for the first bit of this a6,0, d5,0,as4,0, g4,0,e4,0, g4,0,as4,0, d5,0, 0, a4,0, 0, a7,0, d7,0,as6,0, g6,0,e6,0, g6,0,as6,0, d7,0, 0, a4,0, 0, a4,0, d5,0, as4,0, 0, a4,0, a4,0, as4,0, 0, d4,ds4,d4,cs4, ds4,d4,ds4,ds5, d4,ds4,d4,e4, d4,f4,d4,g4, d4,as4,a4,gs4, as4,a5,as4,as5, a5,as4,a4,b5, a4,c6,a4,cs6, d6,0, a3,as3,b3,c4, cs4,d4,ds4,e4, f4,fs4,g4,gs4, a4,as4,b4,c5, cs5,d5,ds5,e5, f5,fs5,g5,gs5, a5,b5,c6,cs6, d6,0,0, d7,0, d5,0 }; const byte durations3[] PROGMEM = { 2,2, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,3, 4, 1,3, 4, 2,2, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,3, 4, 1,3, 4, 2,2, 2,2, 1,3, 4, 2,2, 2,2, 1,3, 4, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 2,2, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 2,2,4, 4,4, 4,4 }; float onTime; float offTime; void beep(float freq, float duration, bool Staccato=false) { if (freq>16.0){ digitalWrite(LEDpin, HIGH); if (Staccato) { onTime = shortest/3.0; // feel free to change the 3.0 ! offTime = duration - onTime; } else { offTime = duration/3.0; // feel free to change this! onTime = duration - offTime; } if (freq<39.0) { // make the sound without using tone() int t = round(500000.0/freq); // half period in microseconds unsigned long int timee=micros(); for (int i=0;i