Advertisement
makispaiktis

ArduinoBasics - 10. Smoke on the water - Master Version

Mar 24th, 2021 (edited)
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int buzzer = 11;
  2. const int button = 6;
  3. bool isPressed;
  4. // Hertz of each note ----> Riffs 1 and 2
  5. int G = 196;
  6. int B = 235;    // Yfesi (mesi A kai B)
  7. int C = 262;
  8. int D = 276;    // Yfesi (mesi C kai D)
  9. // Hertz of each note ----> Riffs 3 anf 4 (Katw octava, ara ta misa se Hz)
  10. int GG = 98;
  11. int BB = 117;    // Yfesi (mesi A kai B)
  12. int CC = 130;
  13. int DD = 138;    // Yfesi (mesi C kai D)
  14. // Times
  15. int olokliro = 2400;
  16. int miso = 1200;
  17. int tetarto = 600;
  18. int ogdoo = 300;
  19.  
  20. // Function 1
  21. void riff12(){
  22.     // 1st part
  23.     tone(buzzer, G);
  24.     Serial.print("G - ");
  25.     delay(tetarto);
  26.     tone(buzzer, B);
  27.     Serial.print("B - ");
  28.     delay(tetarto);
  29.     tone(buzzer, C);
  30.     Serial.print("C - ");
  31.     delay(1.5 * tetarto);
  32.     // 2nd part
  33.     tone(buzzer, G);
  34.     Serial.print("G - ");
  35.     delay(tetarto);
  36.     tone(buzzer, B);
  37.     Serial.print("B - ");
  38.     delay(tetarto);
  39.     tone(buzzer, D);
  40.     Serial.print("D - ");
  41.     delay(ogdoo);
  42.     tone(buzzer, C);
  43.     Serial.print("C - ");
  44.     delay(miso);
  45.     // 3rd part
  46.     tone(buzzer, G);
  47.     Serial.print("G - ");
  48.     delay(tetarto);
  49.     tone(buzzer, B);
  50.     Serial.print("B - ");
  51.     delay(tetarto);
  52.     tone(buzzer, C);
  53.     Serial.print("C - ");
  54.     delay(1.5 * tetarto);
  55.     // 4th part
  56.     tone(buzzer, B);
  57.     Serial.print("B - ");
  58.     delay(tetarto);
  59.     tone(buzzer, G);
  60.     Serial.print("G");
  61.     Serial.println();
  62.     delay(1.75 * tetarto);
  63. }
  64.  
  65. // Function 2
  66. void riff34(){
  67.   // 1st part
  68.     tone(buzzer, GG);
  69.     Serial.print("GG - ");
  70.     delay(tetarto);
  71.     tone(buzzer, BB);
  72.     Serial.print("BB - ");
  73.     delay(tetarto);
  74.     tone(buzzer, CC);
  75.     Serial.print("CC - ");
  76.     delay(1.5 * tetarto);
  77.     // 2nd part
  78.     tone(buzzer, GG);
  79.     Serial.print("GG - ");
  80.     delay(tetarto);
  81.     tone(buzzer, BB);
  82.     Serial.print("BB - ");
  83.     delay(tetarto);
  84.     tone(buzzer, DD);
  85.     Serial.print("DD - ");
  86.     delay(ogdoo);
  87.     tone(buzzer, CC);
  88.     Serial.print("CC - ");
  89.     delay(miso);
  90.     // 3rd part
  91.     tone(buzzer, GG);
  92.     Serial.print("GG - ");
  93.     delay(tetarto);
  94.     tone(buzzer, BB);
  95.     Serial.print("BB - ");
  96.     delay(tetarto);
  97.     tone(buzzer, CC);
  98.     Serial.print("CC - ");
  99.     delay(1.5 * tetarto);
  100.     // 4th part
  101.     tone(buzzer, BB);
  102.     Serial.print("BB - ");
  103.     delay(tetarto);
  104.     tone(buzzer, GG);
  105.     Serial.print("GG");
  106.     Serial.println();
  107.     delay(1.75 * tetarto);
  108. }
  109.  
  110. // Function 3
  111. void stopMusic(){
  112.   noTone(buzzer);
  113.   delay(tetarto);
  114. }
  115.  
  116.  
  117.  
  118. // =============================================================================================
  119. // ================================== ARDUINO MAIN FUNCTIONS  ==================================
  120. // =============================================================================================
  121.  
  122. void setup()
  123. {
  124.   Serial.begin(9600);
  125.   pinMode(button, INPUT);
  126.   pinMode(buzzer, OUTPUT);
  127. }
  128.  
  129. void loop()
  130. {
  131.   isPressed = digitalRead(button);
  132.   if(isPressed == true){
  133.     Serial.println("********  SMOKE ON THE WATER - DEEP PURPLE ********");
  134.     Serial.println();
  135.     // Riff 1
  136.     Serial.println("==== Riff 1 (G = 196Hz, B = 235Hz, C = 262Hz, D = 276Hz) ====");
  137.     riff12();
  138.     stopMusic();
  139.     // Riff 2
  140.     Serial.println("==== Riff 2 (G = 196Hz, B = 235Hz, C = 262Hz, D = 276Hz) ====");
  141.     riff12();
  142.     stopMusic();
  143.     // Riff 3
  144.     Serial.println("==== Riff 3 (GG = 98Hz, BB =123Hz, CC = 130Hz, DD = 138Hz) ====");
  145.     riff34();
  146.     stopMusic();
  147.     // Riff 4
  148.     Serial.println("==== Riff 4 (GG = 98Hz, BB =123Hz, CC = 130Hz, DD = 138Hz) ====");
  149.     riff34();
  150.     stopMusic();
  151.     // End of song
  152.     noTone(buzzer);
  153.   }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement