Advertisement
zvizvi

ESP8266 ToneMelody

Jul 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. #include "pitches.h"
  2.  
  3. int melody[] = {
  4.   NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
  5. };
  6.  
  7. int noteDurations[] = {
  8.   4, 8, 8, 4, 4, 4, 4, 4
  9. };
  10.  
  11. void setup() {
  12.   pinMode(D2, OUTPUT);
  13.   for (int thisNote = 0; thisNote < 8; thisNote++) {
  14.     int noteDuration = 1000 / noteDurations[thisNote];
  15.     tone(D2, melody[thisNote], noteDuration);
  16.     int pauseBetweenNotes = noteDuration * 1.30;
  17.     delay(pauseBetweenNotes);
  18.     noTone(D2);
  19.   }
  20. }
  21.  
  22. void loop() {
  23.   // no need to repeat the melody.
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement