Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. const int speakerPin = 9;// piezo sounder connected to pin9
  2. const int btn1 = 2;
  3. const int btn2 = 3;
  4. const int led1 = 5;
  5.  
  6. #define TUNE1 7
  7.  const int notes_tune1[TUNE1] = {262, 262, 294, 262,349, 330, 0};
  8.  
  9. const int beats_tune1[TUNE1] = {1, 1, 2, 2, 2, 4, 4 };
  10.  
  11. #define TUNE2 15
  12. const int notes_tune2[TUNE2] = { 262, 262, 392, 392,440, 440, 392, 349,349, 330, 330, 294,294, 262, 0};
  13. const int beats_tune2[TUNE2] = {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
  14.  
  15. const int tempo = 300;
  16.  
  17. //SETUP
  18. void setup()
  19. {
  20.  pinMode(speakerPin, OUTPUT);
  21.  pinMode(btn1, INPUT_PULLUP);
  22.  pinMode(btn2, INPUT_PULLUP);
  23.  pinMode(led1,OUTPUT);
  24. }
  25.  
  26. void playTune1(){
  27.  for (int i = 0; i < TUNE1; i++) {
  28.  
  29.   if (notes_tune1[i] == 0){
  30.     delay(beats_tune1[i] * tempo); // rest
  31.   }
  32.  
  33.   else {
  34.     ourTone(notes_tune1[i], beats_tune1[i] * tempo);
  35.   }
  36.  
  37.   delay(tempo / 2);
  38.   }
  39. }
  40.  
  41. void playTune2(){
  42.  for (int i = 0; i < TUNE2; i++) {
  43.   if (notes_tune2[i] == 0) {
  44.     delay(beats_tune2[i] * tempo); // rest
  45.   }
  46.   else {
  47.     ourTone(notes_tune2[i], beats_tune2[i] * tempo);
  48.   }
  49.  
  50.   delay(tempo / 2);
  51.   }
  52. }
  53.  
  54. void ourTone(int freq, int duration) {
  55.   tone(speakerPin, freq, duration);
  56.   digitalWrite(led1,HIGH); //lights on
  57.   delay(duration);
  58.   digitalWrite(led1,LOW); //lights off
  59.   noTone(speakerPin);
  60. }
  61.  
  62. //LOOP
  63. void loop()
  64. {
  65.  if(digitalRead(btn1) == LOW){
  66.   playTune1();
  67.  }
  68.  
  69.  if(digitalRead(btn2) == LOW){
  70.   playTune2();
  71.  }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement