Advertisement
safwan092

Untitled

Aug 31st, 2022
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. int melody[] = {
  2.  
  3. 262, 196, 196, 220, 196, 0, 247, 262
  4. };
  5.  
  6. // note durations: 4 = quarter note, 8 = eighth note, etc.:
  7. int noteDurations[] = {
  8.  
  9. 4, 8, 8, 4, 4, 4, 4, 4
  10. };
  11.  
  12. void setup() {
  13.  
  14. // iterate over the notes of the melody:
  15.  
  16. for (int thisNote = 0; thisNote < 8; thisNote++) {
  17.  
  18. // to calculate the note duration, take one second divided by the note type.
  19.  
  20. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  21.  
  22. int noteDuration = 1000 / noteDurations[thisNote];
  23.  
  24. tone(4, melody[thisNote], noteDuration);
  25.  
  26. // to distinguish the notes, set a minimum time between them.
  27.  
  28. // the note's duration + 30% seems to work well:
  29.  
  30. int pauseBetweenNotes = noteDuration * 1.30;
  31.  
  32. delay(pauseBetweenNotes);
  33.  
  34. // stop the tone playing:
  35.  
  36. noTone(4);
  37.  
  38. }
  39. }
  40.  
  41. void loop() {
  42.  
  43. // no need to repeat the melody.
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement