Cooler_

arduino tones

May 5th, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.82 KB | None | 0 0
  1. // A fun sketch to demonstrate the use of the Tone library.
  2.  
  3. // To mix the output of the signals to output to a small speaker (i.e. 8 Ohms or higher),
  4. // simply use 1K Ohm resistors from each output pin and tie them together at the speaker.
  5. // Don't forget to connect the other side of the speaker to ground!
  6.  
  7. // You can get more RTTTL (RingTone Text Transfer Language) songs from
  8. // http://code.google.com/p/rogue-code/wiki/ToneLibraryDocumentation
  9.  
  10. #include <Tone.h>
  11.  
  12. Tone tone1;
  13.  
  14. #define OCTAVE_OFFSET 0
  15.  
  16. int notes[] = { 0,
  17. NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
  18. NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
  19. NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
  20. NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
  21. };
  22.  
  23. //char *song = "The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6";
  24. //char *song = "Indiana:d=4,o=5,b=250:e,8p,8f,8g,8p,1c6,8p.,d,8p,8e,1f,p.,g,8p,8a,8b,8p,1f6,p,a,8p,8b,2c6,2d6,2e6,e,8p,8f,8g,8p,1c6,p,d6,8p,8e6,1f.6,g,8p,8g,e.6,8p,d6,8p,8g,e.6,8p,d6,8p,8g,f.6,8p,e6,8p,8d6,2c6";
  25. //char *song = "TakeOnMe:d=4,o=4,b=160:8f#5,8f#5,8f#5,8d5,8p,8b,8p,8e5,8p,8e5,8p,8e5,8g#5,8g#5,8a5,8b5,8a5,8a5,8a5,8e5,8p,8d5,8p,8f#5,8p,8f#5,8p,8f#5,8e5,8e5,8f#5,8e5,8f#5,8f#5,8f#5,8d5,8p,8b,8p,8e5,8p,8e5,8p,8e5,8g#5,8g#5,8a5,8b5,8a5,8a5,8a5,8e5,8p,8d5,8p,8f#5,8p,8f#5,8p,8f#5,8e5,8e5";
  26. //char *song = "Entertainer:d=4,o=5,b=140:8d,8d#,8e,c6,8e,c6,8e,2c.6,8c6,8d6,8d#6,8e6,8c6,8d6,e6,8b,d6,2c6,p,8d,8d#,8e,c6,8e,c6,8e,2c.6,8p,8a,8g,8f#,8a,8c6,e6,8d6,8c6,8a,2d6";
  27. //char *song = "Muppets:d=4,o=5,b=250:c6,c6,a,b,8a,b,g,p,c6,c6,a,8b,8a,8p,g.,p,e,e,g,f,8e,f,8c6,8c,8d,e,8e,8e,8p,8e,g,2p,c6,c6,a,b,8a,b,g,p,c6,c6,a,8b,a,g.,p,e,e,g,f,8e,f,8c6,8c,8d,e,8e,d,8d,c";
  28. //char *song = "Xfiles:d=4,o=5,b=125:e,b,a,b,d6,2b.,1p,e,b,a,b,e6,2b.,1p,g6,f#6,e6,d6,e6,2b.,1p,g6,f#6,e6,d6,f#6,2b.,1p,e,b,a,b,d6,2b.,1p,e,b,a,b,e6,2b.,1p,e6,2b.";
  29. //char *song = "Looney:d=4,o=5,b=140:32p,c6,8f6,8e6,8d6,8c6,a.,8c6,8f6,8e6,8d6,8d#6,e.6,8e6,8e6,8c6,8d6,8c6,8e6,8c6,8d6,8a,8c6,8g,8a#,8a,8f";
  30. //char *song = "20thCenFox:d=16,o=5,b=140:b,8p,b,b,2b,p,c6,32p,b,32p,c6,32p,b,32p,c6,32p,b,8p,b,b,b,32p,b,32p,b,32p,b,32p,b,32p,b,32p,b,32p,g#,32p,a,32p,b,8p,b,b,2b,4p,8e,8g#,8b,1c#6,8f#,8a,8c#6,1e6,8a,8c#6,8e6,1e6,8b,8g#,8a,2b";
  31. //char *song = "Bond:d=4,o=5,b=80:32p,16c#6,32d#6,32d#6,16d#6,8d#6,16c#6,16c#6,16c#6,16c#6,32e6,32e6,16e6,8e6,16d#6,16d#6,16d#6,16c#6,32d#6,32d#6,16d#6,8d#6,16c#6,16c#6,16c#6,16c#6,32e6,32e6,16e6,8e6,16d#6,16d6,16c#6,16c#7,c.7,16g#6,16f#6,g#.6";
  32. //char *song = "MASH:d=8,o=5,b=140:4a,4g,f#,g,p,f#,p,g,p,f#,p,2e.,p,f#,e,4f#,e,f#,p,e,p,4d.,p,f#,4e,d,e,p,d,p,e,p,d,p,2c#.,p,d,c#,4d,c#,d,p,e,p,4f#,p,a,p,4b,a,b,p,a,p,b,p,2a.,4p,a,b,a,4b,a,b,p,2a.,a,4f#,a,b,p,d6,p,4e.6,d6,b,p,a,p,2b";
  33. //char *song = "StarWars:d=4,o=5,b=45:32p,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#.6,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#6";
  34. //char *song = "GoodBad:d=4,o=5,b=56:32p,32a#,32d#6,32a#,32d#6,8a#.,16f#.,16g#.,d#,32a#,32d#6,32a#,32d#6,8a#.,16f#.,16g#.,c#6,32a#,32d#6,32a#,32d#6,8a#.,16f#.,32f.,32d#.,c#,32a#,32d#6,32a#,32d#6,8a#.,16g#.,d#";
  35. //char *song = "TopGun:d=4,o=4,b=31:32p,16c#,16g#,16g#,32f#,32f,32f#,32f,16d#,16d#,32c#,32d#,16f,32d#,32f,16f#,32f,32c#,16f,d#,16c#,16g#,16g#,32f#,32f,32f#,32f,16d#,16d#,32c#,32d#,16f,32d#,32f,16f#,32f,32c#,g#";
  36. //char *song = "A-Team:d=8,o=5,b=125:4d#6,a#,2d#6,16p,g#,4a#,4d#.,p,16g,16a#,d#6,a#,f6,2d#6,16p,c#.6,16c6,16a#,g#.,2a#";
  37. //char *song = "Flinstones:d=4,o=5,b=40:32p,16f6,16a#,16a#6,32g6,16f6,16a#.,16f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c6,d6,16f6,16a#.,16a#6,32g6,16f6,16a#.,32f6,32f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c6,a#,16a6,16d.6,16a#6,32a6,32a6,32g6,32f#6,32a6,8g6,16g6,16c.6,32a6,32a6,32g6,32g6,32f6,32e6,32g6,8f6,16f6,16a#.,16a#6,32g6,16f6,16a#.,16f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c.6,32d6,32d#6,32f6,16a#,16c.6,32d6,32d#6,32f6,16a#6,16c7,8a#.6";
  38. //char *song = "Jeopardy:d=4,o=6,b=125:c,f,c,f5,c,f,2c,c,f,c,f,a.,8g,8f,8e,8d,8c#,c,f,c,f5,c,f,2c,f.,8d,c,a#5,a5,g5,f5,p,d#,g#,d#,g#5,d#,g#,2d#,d#,g#,d#,g#,c.7,8a#,8g#,8g,8f,8e,d#,g#,d#,g#5,d#,g#,2d#,g#.,8f,d#,c#,c,p,a#5,p,g#.5,d#,g#";
  39. //char *song = "Gadget:d=16,o=5,b=50:32d#,32f,32f#,32g#,a#,f#,a,f,g#,f#,32d#,32f,32f#,32g#,a#,d#6,4d6,32d#,32f,32f#,32g#,a#,f#,a,f,g#,f#,8d#";
  40. //char *song = "Smurfs:d=32,o=5,b=200:4c#6,16p,4f#6,p,16c#6,p,8d#6,p,8b,p,4g#,16p,4c#6,p,16a#,p,8f#,p,8a#,p,4g#,4p,g#,p,a#,p,b,p,c6,p,4c#6,16p,4f#6,p,16c#6,p,8d#6,p,8b,p,4g#,16p,4c#6,p,16a#,p,8b,p,8f,p,4f#";
  41. //char *song = "MahnaMahna:d=16,o=6,b=125:c#,c.,b5,8a#.5,8f.,4g#,a#,g.,4d#,8p,c#,c.,b5,8a#.5,8f.,g#.,8a#.,4g,8p,c#,c.,b5,8a#.5,8f.,4g#,f,g.,8d#.,f,g.,8d#.,f,8g,8d#.,f,8g,d#,8c,a#5,8d#.,8d#.,4d#,8d#.";
  42. //char *song = "LeisureSuit:d=16,o=6,b=56:f.5,f#.5,g.5,g#5,32a#5,f5,g#.5,a#.5,32f5,g#5,32a#5,g#5,8c#.,a#5,32c#,a5,a#.5,c#.,32a5,a#5,32c#,d#,8e,c#.,f.,f.,f.,f.,f,32e,d#,8d,a#.5,e,32f,e,32f,c#,d#.,c#";
  43. //char *song = "MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d";
  44. //char *song = "SMBUndergr:d=16,o=6,b=100:c,c5,a5,a,a#5,a#,2p,8p,c,c5,a5,a,a#5,a#,2p,8p,f5,f,d5,d,d#5,d#,2p,8p,f5,f,d5,d,d#5,d#,2p,32d#,d,32c#,c,p,d#,p,d,p,g#5,p,g5,p,c#,p,32c,f#,32f,32e,a#,32a,g#,32p,d#,b5,32p,a#5,32p,a5,g#5";
  45. //char *song = "SMBWater:d=8,o=6,b=225:4d5,4e5,4f#5,4g5,4a5,4a#5,b5,b5,b5,p,b5,p,2b5,p,g5,2e.,2d#.,2e.,p,g5,a5,b5,c,d,2e.,2d#,4f,2e.,2p,p,g5,2d.,2c#.,2d.,p,g5,a5,b5,c,c#,2d.,2g5,4f,2e.,2p,p,g5,2g.,2g.,2g.,4g,4a,p,g,2f.,2f.,2f.,4f,4g,p,f,2e.,4a5,4b5,4f,e,e,4e.,b5,2c.";
  46. char *song = "smb:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6";
  47.  
  48. void setup(void)
  49. {
  50.   Serial.begin(9600);
  51.   tone1.begin(13);
  52. }
  53.  
  54. #define isdigit(n) (n >= '0' && n <= '9')
  55.  
  56. void play_rtttl(char *p)
  57. {
  58.   // Absolutely no error checking in here
  59.  
  60.   byte default_dur = 4;
  61.   byte default_oct = 6;
  62.   int bpm = 63;
  63.   int num;
  64.   long wholenote;
  65.   long duration;
  66.   byte note;
  67.   byte scale;
  68.  
  69.   // format: d=N,o=N,b=NNN:
  70.   // find the start (skip name, etc)
  71.  
  72.   while(*p != ':') p++;    // ignore name
  73.   p++;                     // skip ':'
  74.  
  75.   // get default duration
  76.   if(*p == 'd')
  77.   {
  78.     p++; p++;              // skip "d="
  79.     num = 0;
  80.     while(isdigit(*p))
  81.     {
  82.       num = (num * 10) + (*p++ - '0');
  83.     }
  84.     if(num > 0) default_dur = num;
  85.     p++;                   // skip comma
  86.   }
  87.  
  88.   Serial.print("ddur: "); Serial.println(default_dur, 10);
  89.  
  90.   // get default octave
  91.   if(*p == 'o')
  92.   {
  93.     p++; p++;              // skip "o="
  94.     num = *p++ - '0';
  95.     if(num >= 3 && num <=7) default_oct = num;
  96.     p++;                   // skip comma
  97.   }
  98.  
  99.   Serial.print("doct: "); Serial.println(default_oct, 10);
  100.  
  101.   // get BPM
  102.   if(*p == 'b')
  103.   {
  104.     p++; p++;              // skip "b="
  105.     num = 0;
  106.     while(isdigit(*p))
  107.     {
  108.       num = (num * 10) + (*p++ - '0');
  109.     }
  110.     bpm = num;
  111.     p++;                   // skip colon
  112.   }
  113.  
  114.   Serial.print("bpm: "); Serial.println(bpm, 10);
  115.  
  116.   // BPM usually expresses the number of quarter notes per minute
  117.   wholenote = (60 * 1000L / bpm) * 4;  // this is the time for whole note (in milliseconds)
  118.  
  119.   Serial.print("wn: "); Serial.println(wholenote, 10);
  120.  
  121.  
  122.   // now begin note loop
  123.   while(*p)
  124.   {
  125.     // first, get note duration, if available
  126.     num = 0;
  127.     while(isdigit(*p))
  128.     {
  129.       num = (num * 10) + (*p++ - '0');
  130.     }
  131.    
  132.     if(num) duration = wholenote / num;
  133.     else duration = wholenote / default_dur;  // we will need to check if we are a dotted note after
  134.  
  135.     // now get the note
  136.     note = 0;
  137.  
  138.     switch(*p)
  139.     {
  140.       case 'c':
  141.         note = 1;
  142.         break;
  143.       case 'd':
  144.         note = 3;
  145.         break;
  146.       case 'e':
  147.         note = 5;
  148.         break;
  149.       case 'f':
  150.         note = 6;
  151.         break;
  152.       case 'g':
  153.         note = 8;
  154.         break;
  155.       case 'a':
  156.         note = 10;
  157.         break;
  158.       case 'b':
  159.         note = 12;
  160.         break;
  161.       case 'p':
  162.       default:
  163.         note = 0;
  164.     }
  165.     p++;
  166.  
  167.     // now, get optional '#' sharp
  168.     if(*p == '#')
  169.     {
  170.       note++;
  171.       p++;
  172.     }
  173.  
  174.     // now, get optional '.' dotted note
  175.     if(*p == '.')
  176.     {
  177.       duration += duration/2;
  178.       p++;
  179.     }
  180.  
  181.     // now, get scale
  182.     if(isdigit(*p))
  183.     {
  184.       scale = *p - '0';
  185.       p++;
  186.     }
  187.     else
  188.     {
  189.       scale = default_oct;
  190.     }
  191.  
  192.     scale += OCTAVE_OFFSET;
  193.  
  194.     if(*p == ',')
  195.       p++;       // skip comma for next note (or we may be at the end)
  196.  
  197.     // now play the note
  198.  
  199.     if(note)
  200.     {
  201.       Serial.print("Playing: ");
  202.       Serial.print(scale, 10); Serial.print(' ');
  203.       Serial.print(note, 10); Serial.print(" (");
  204.       Serial.print(notes[(scale - 4) * 12 + note], 10);
  205.       Serial.print(") ");
  206.       Serial.println(duration, 10);
  207.       tone1.play(notes[(scale - 4) * 12 + note]);
  208.       delay(duration);
  209.       tone1.stop();
  210.     }
  211.     else
  212.     {
  213.       Serial.print("Pausing: ");
  214.       Serial.println(duration, 10);
  215.       delay(duration);
  216.     }
  217.   }
  218. }
  219.  
  220. void loop(void)
  221. {
  222.   play_rtttl(song);
  223.   Serial.println("Done.");
  224.   while(1);
  225. }
Advertisement
Add Comment
Please, Sign In to add comment