Advertisement
Guest User

IT250 Play A Tune Lab

a guest
Nov 13th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include "application.h"
  2.  
  3. // name the pins
  4. #define BUTTONPIN D2
  5. #define BUZZERPIN D4
  6.  
  7. int press = 0;
  8. int oldPress = 0;
  9. int state = 0;
  10. int melody[] = {4186,2093,1047,523,262,131,65,33};  // notes in the melody
  11. int noteDurations[] = {4,8,8,4,4,4,4,4 };               // note durations
  12. int melody2[] = {33,131,65,262,1047,523,2093,4186};  // notes in the melody
  13. int noteDurations2[] = {2,4,4,3,3,2,1,1 };               // note durations
  14.  
  15. // This routine runs only once upon reset
  16. void setup() {
  17.  
  18.     pinMode(BUTTONPIN, INPUT);
  19.     pinMode(BUTTONPIN, OUTPUT);
  20. }
  21.  
  22. void loop() {
  23.  
  24.     press = digitalRead(BUTTONPIN);
  25.     if ( press == HIGH && oldPress == LOW) {
  26.         state = 1 - state;
  27.         if (state == 1) {
  28.             tune1();
  29.         }
  30.         else {
  31.             tune2();
  32.         }
  33.     }
  34.     oldPress = press;
  35. }
  36.  
  37. int tune1() {
  38.     for (int thisNote = 0; thisNote < 8; thisNote++) {    // ergodic all notes
  39.         int noteDuration = 1000/noteDurations[thisNote];  // calculate the note duration
  40.         tone(BUZZERPIN, melody[thisNote], noteDuration);  // let speaker sonds
  41.         int pauseBetweenNotes = noteDuration * 1.30;      // set a minimum time between notes
  42.         delay(pauseBetweenNotes);                         // delay for the while
  43.         digitalWrite(BUZZERPIN,HIGH);                     // stop the tone playing/read button
  44.     }
  45. }
  46.  
  47. int tune2() {
  48.     for (int thisNote = 0; thisNote < 8; thisNote++) {     // ergodic all notes
  49.         int noteDuration = 1000/noteDurations2[thisNote];  // calculate the note duration
  50.         tone(BUZZERPIN, melody2[thisNote], noteDuration);  // let speaker sonds
  51.         int pauseBetweenNotes = noteDuration * 1.30;       // set a minimum time between notes
  52.         delay(pauseBetweenNotes);                          // delay for the while
  53.         digitalWrite(BUZZERPIN,LOW);                       // stop the tone playing/read button
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement