Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. /*
  2. toneDelay Example:
  3.  
  4. Based on toneMelody by Tom Igoe.
  5. The toneDelay function is an alternative to the tone function.
  6. Basically is the same except it is a blocking function, i.e. it does not use a timer for frequency generation
  7.  
  8. Author: Federico Lanza
  9.  
  10. */
  11.  
  12. #include "Arduino.h"
  13. #include "pitches.h"
  14.  
  15. // notes in the melody:
  16. int melody[] = {
  17. NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
  18.  
  19. // note durations: 4 = quarter note, 8 = eighth note, etc.:
  20. int noteDurations[] = {
  21. 4, 8, 8, 4,4,4,4,4 };
  22.  
  23. void setup() {
  24. pinMode(SPEAKER, OUTPUT);
  25. }
  26.  
  27. void loop() {
  28. // iterate over the notes of the melody:
  29. for (int thisNote = 0; thisNote < 8; thisNote++) {
  30.  
  31. // to calculate the note duration, take one second
  32. // divided by the note type.
  33. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  34. int noteDuration = 1000/noteDurations[thisNote];
  35. toneDelay(SPEAKER, melody[thisNote],noteDuration);
  36.  
  37. // to distinguish the notes, set a minimum time between them.
  38. int pauseBetweenNotes = noteDuration * 0.8;
  39. delay(pauseBetweenNotes);
  40. }
  41.  
  42. delay(1000);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement