daily pastebin goal
2%
SHARE
TWEET

Melody for Arduino

a guest Oct 12th, 2015 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Melody
  2.  
  3. #include "pitches.h"
  4.  
  5. // notes in the melody:
  6. int melody[] = {
  7. NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
  8. };
  9.  
  10. // note durations: 4 = quarter note, 8 = eighth note, etc.:
  11. int noteDurations[] = {
  12. 4, 8, 8, 4, 4, 4, 4, 4
  13. };
  14.  
  15. void setup() {
  16. // iterate over the notes of the melody:
  17. for (int thisNote = 0; thisNote < 8; thisNote++) {
  18.  
  19. // to calculate the note duration, take one second
  20. // divided by the note type.
  21. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  22. int noteDuration = 1000 / noteDurations[thisNote];
  23. tone(8, melody[thisNote], noteDuration);
  24.  
  25. // to distinguish the notes, set a minimum time between them.
  26. // the note's duration + 30% seems to work well:
  27. int pauseBetweenNotes = noteDuration * 1.30;
  28. delay(pauseBetweenNotes);
  29. // stop the tone playing:
  30. noTone(8);
  31. }
  32. }
  33.  
  34. void loop() {
  35. // no need to repeat the melody.
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top