Passive Buzzer Duel

Le concept reste le même, sauf que cette fois on utilise un buzzer passive pour jouer de la musique!

Code:

 // SOURCE http://www.instructables.com/id/How-to-easily-play-music-with-buzzer-on-arduino-Th/
 #define C0 16.35
 #define Db0 17.32
 #define D0  18.35
 #define Eb0 19.45
 #define E0  20.60
 #define F0  21.83
 #define Gb0 23.12
 #define G0  24.50
 #define Ab0 25.96
 #define LA0 27.50
 #define Bb0 29.14
 #define LB0  30.87
 #define C1  32.70
 #define Db1 34.65
 #define D1  36.71
 #define Eb1 38.89
 #define E1  41.20
 #define F1  43.65
 #define Gb1 46.25
 #define G1  49.00
 #define Ab1 51.91
 #define LA1 55.00
 #define Bb1 58.27
 #define LB1  61.74
 #define C2  65.41
 #define Db2 69.30
 #define D2  73.42
 #define Eb2 77.78
 #define E2  82.41
 #define F2  87.31
 #define Gb2 92.50
 #define G2  98.00
 #define Ab2 103.83
 #define LA2 110.00
 #define Bb2 116.54
 #define B2  123.47
 #define C3  130.81
 #define Db3 138.59
 #define D3  146.83
 #define Eb3 155.56
 #define E3  164.81
 #define F3  174.61
 #define Gb3 185.00
 #define G3  196.00
 #define Ab3 207.65
 #define LA3 220.00
 #define Bb3 233.08
 #define B3  246.94
 #define C4  261.63
 #define Db4 277.18
 #define D4  293.66
 #define Eb4 311.13
 #define E4  329.63
 #define F4  349.23
 #define Gb4 369.99
 #define G4  392.00
 #define Ab4 415.30
 #define LA4 440.00
 #define Bb4 466.16
 #define B4  493.88
 #define C5  523.25
 #define Db5 554.37
 #define D5  587.33
 #define Eb5 622.25
 #define E5  659.26
 #define F5  698.46
 #define Gb5 739.99
 #define G5  783.99
 #define Ab5 830.61
 #define LA5 880.00
 #define Bb5 932.33
 #define B5  987.77
 #define C6  1046.50
 #define Db6 1108.73
 #define D6  1174.66
 #define Eb6 1244.51
 #define E6  1318.51
 #define F6  1396.91
 #define Gb6 1479.98
 #define G6  1567.98
 #define Ab6 1661.22
 #define LA6 1760.00
 #define Bb6 1864.66
 #define B6  1975.53
 #define C7  2093.00
 #define Db7 2217.46
 #define D7  2349.32
 #define Eb7 2489.02
 #define E7  2637.02
 #define F7  2793.83
 #define Gb7 2959.96
 #define G7  3135.96
 #define Ab7 3322.44
 #define LA7 3520.01
 #define Bb7 3729.31
 #define B7  3951.07
 #define C8  4186.01
 #define Db8 4434.92
 #define D8  4698.64
 #define Eb8 4978.03
 // DURATION OF THE NOTES
 #define BPM 120    //  you can change this value changing all the others
 #define H 2*Q //half 2/4
 #define Q 60000/BPM //quarter 1/4
 #define E Q/2   //eighth 1/8
 #define S Q/4 // sixteenth 1/16
 #define W 4*Q // whole 4/4
  
 double imperialMarch[][4] = {
   {LA3, Q, 2, 1+Q},
   {LA3, Q, 2, 1+Q},
   {LA3, Q, 2, 1+Q},
   {F3, E+S, 6, 1+E+S},
   {C4, S, 3, 1+S},
   {LA3, Q, 2, 1+Q},
   {F3, E+S, 6, 1+E+S},
   {C4, S, 3, 1+S},
   {LA3, H, 2, 1+H},
   {E4, Q, 5, 1+Q},
   {E4, Q, 5, 1+Q},
   {E4, Q, 5, 1+Q},
   {F4, E+S, 6, 1+E+S},
   {C4, S, 3, 1+S},
   {Ab3, Q, 2, 1+Q},
   {F3, E+S, 6, 1+E+S},
   {C4, S, 3, 1+S},
   {LA3, H, 2, 1+H},
   {LA4, Q, 3, 1+Q},
   {LA3, E+S, 2, 1+E+S},
   {LA3, S, 2, 1+S},
   {LA4, Q, 3, 1+Q},
   {Ab4, E+S, 2, 1+E+S},
   {G4, S, 2, 1+S},
   {E4, S, 5, 1+S},
   {F4, E, 6, 2+E+E},
   {Bb3, E, 2, 1+E},
   {Eb4, Q, 3, 1+Q},
   {D4, Q, 3, 1+Q},
   {Db4, S, 2, 1+S},
   {C4, S, 3, 1+S},
   {B3, S, 2, 1+S},
   {C4, E, 3, 2+E+E},
   {F3, E, 6, 1+E},
   {Ab3, Q, 2, 1+Q},
   {F3, E+S, 6, 1+E+S},
   {LA3, S, 2, 1+S},
   {C4, Q, 3, 1+Q},
   {LA3, E+S, 2, 1+E+S},
   {C4, S, 3, 1+S},
   {E4, H, 5, 1+H},
   {LA4, Q, 3, 1+Q},
   {LA3, E+S, 2, 1+E+S},
   {LA3, S, 2, 1+S},
   {LA4, Q, 3, 1+Q},
   {Ab4, E+S, 2, 1+E+S},
   {G4, S, 6, 1+S},
   {Gb4, S, 2, 1+S},
   {E4, S, 5, 1+S},
   {F4, E, 6, 2+E+E},
   {Bb3, E, 3, 1+E},
   {Eb4, Q, 2, 1+Q},
   {D4, E+S, 4, 1+E+S},
   {Db4, S, 2, 1+S},
   {C4, S, 3, 1+S},
   {B3, S, 2, 1+S},
   {C4, E, 3, 2+E+E},
   {F3, E, 6, 1+E},
   {Ab3, Q, 2, 1+Q},
   {F3, E+S, 6, 1+E+S},
   {C4, S, 3, 1+S},
   {LA3, Q, 2, 1+Q},
   {F3, E+S, 6, 1+E+S},
   {C4, S, 3, 1+S},
   {LA3, H, 2, 1+H+H+H}
 };
  
 // Boutons Joueurs
 int buttonApin = 8;
 int buttonBpin = 9;
  
 int active_buzzer = 10;//the pin of the active buzzer
 int passive_buzzer = 11;
  
 int lastLed = 0;
  
 int nbLoop = 0; // N° de la loop actuelle
 int stopAtLoop = 0;  // N° de la loop de fin
 int stopGame = 0;  // Fin du jeu
  
 void setup() {
   // Initialisation des LED
   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);
   
   // Initialisation des boutons
   pinMode(buttonApin, INPUT_PULLUP);  
   pinMode(buttonBpin, INPUT_PULLUP);  
  
   pinMode(active_buzzer,OUTPUT);//initialize the buzzer pin as an output
  
   // Randomiser le numéro de la loop de fin
   randomSeed(analogRead(0));
   stopAtLoop = random(10, sizeof(imperialMarch) / sizeof(imperialMarch)[0]);
  
   // Initialiser l'output sur le moniteur série
   Serial.begin(9600);
  
 }
  
 void allumerLed(int led) {
   if(lastLed != 0)
     digitalWrite(lastLed, LOW);
  
   digitalWrite(led, HIGH);
   lastLed = led;
 }
  
 void checkClick() {
     // Arrêter le jeu si qq appuie sur un bouton
     if(digitalRead(buttonApin) == LOW) {
       Serial.println("Joueur 1 a perdu");
       stopGame = 1;
       return;
     } else if(digitalRead(buttonBpin) == LOW) {
       Serial.println("Joueur 2 a perdu");
       stopGame = 1;
       return;
     }
 }
  
 void playTone(int index) {
     checkClick();
     
     tone(passive_buzzer,imperialMarch[index][0],imperialMarch[index][1]);
     allumerLed(imperialMarch[index][2]);
  
     checkClick();
     
     delay(imperialMarch[index][3] / 2);
  
     checkClick();
     
     delay(imperialMarch[index][3] / 2);
  
     checkClick();
 }
  
 void loop() {
     // Jeu terminé, appuyer sur reset pour redémarrer!
     if(stopGame == 1)
         return;
  
     // Dernière loop atteinte
     if(nbLoop == stopAtLoop) {
       if(lastLed != 0)
         digitalWrite(lastLed, LOW);
  
       // Attendre le vainqueur
       if(digitalRead(buttonApin) == LOW) {
         Serial.println("Joueur 1 a gagné !");
         stopGame = 1;
         return;
       }
       
       if(digitalRead(buttonBpin) == LOW) {
         Serial.println("Joueur 2 a gagné !");
         stopGame = 1;
         return;
       }
       
     } else { // Lancer le buzzer
  
       playTone(nbLoop);
       
       // Incrémenter compteur de loop
       nbLoop++;
       
     }
 }

Source du code de la musique: http://www.instructables.com/id/How-to-easily-play-music-with-buzzer-on-arduino-Th/

Les montages ont été faits en utilisant le Starter Kit du Débutant d'Elegoo, acheté sur Amazon.

Lien: https://www.amazon.fr/gp/product/B01JD2Z5XW/ 

Cet article a été mis à jour le 18/02/2018

Commentaires