Pit Musica

Come fare "suonare" Arduino.

 

PitMusica è un "pittolo" programmino per "suonare" con Arduino

  • trasformazione delle note dalla notazione anglosassone alla notazione naturale
  • creazione di un unico array costituito dalla nota seguito dal suo tempo espresso come segue XYY

dove:

x è il numeratore

       yy è il denominatore del tempo

conseguentemente i tempi sono così definiti

101 = 4/4, 102 = 1/2, 104 = 1/4, 308 = 1/4

  • Viene inserito anche il parametro bpm (battiti per minuto) tenuto conto che un battito corrisponde a 1/4

    Andamento       Tempo

      Largo       40-60 bpm

    Larghetto       60 oppure-66 bpm

     Adagio         66-76 bpm

     Andante       76-108 bpm

    Moderato     108-120 bpm

    Allegro      120-168 bpm

     Presto       168-200 bpm

   Prestissimo   200-208 bpm

  • Viene calcolato il tempo in millisecondi

Fonte originale :    Melody Plays a melody

 

Cosa serve:

  • Arduino
  • Specker
  • Resistenza 100 ohm

 

Download:

 

 


/*

PitMusica è un pittolo programmino per "suonare" con Arduino
ZappocoS maggio 2015


1) trasformazione delle note dalla notazione anglosassone alla notazione naturale

2) creazione di un unico array costituito dalla nota seguito dal suo tempo espresso come segue XYY  

   dove  x è il numeratore
        yy è il denominatore del tempo 
   
   conseguentemente i tempi sono così definiti
    101 = 4/4, 102 = 1/2, 104 = 1/4, 308 = 1/4,

3) Viene inserito anche il parametro bpm (battiti per minuto) tenuto conto che un battito corrisponde a 1/4

    Andamento       Tempo
    Largo 	  40-60 bpm
    Larghetto 	  60 oppure-66 bpm
    Adagio  	  66-76 bpm
    Andante 	  76-108 bpm
    Moderato 	 108-120 bpm
    Allegro 	 120-168 bpm
    Presto  	 168-200 bpm
    Prestissimo  200-208 bpm

4) Viene calcolato il tempo in millisecondi


Fonte originale :
   Melody
   Plays a melody
 
   circuit:
   * 8-ohm speaker on digital pin 8
 
   created 21 Jan 2010
   modified 30 Aug 2011
   by Tom Igoe 
   This example code is in the public domain.
 
   http://arduino.cc/en/Tutorial/Tone

*/

#include "note.h"

int pinbuzz = 6; // pin a cui attaccare il buzzer con resistenza da 100 ohm
int pintasto = 2;


// Tanti Auguri
int melodia1[] = {NOTE_DO4 , 108, NOTE_DO4 , 108, 
                 NOTE_RE4 , 104, NOTE_DO4 , 104, NOTE_FA4 , 104,
                 NOTE_MI4 , 102, NOTE_DO4 , 108, NOTE_DO4 , 108,
                 NOTE_RE4 , 104, NOTE_DO4 , 104, NOTE_SO4 , 104,
                 NOTE_FA4 , 102, NOTE_DO4 , 108, NOTE_DO4 , 108,
                 NOTE_DO5 , 104, NOTE_LA4 , 104, NOTE_FA4 , 104,
                 NOTE_MI4 , 104, NOTE_RE4 , 104, NOTE_SI4b, 108, NOTE_SI4b, 108,
                 NOTE_LA4 , 104, NOTE_FA4 , 104, NOTE_SO4 , 104,
                 NOTE_FA4 , 101

                };
int nr1 = 25;
float bpm1 = 75;



// O tannenbaum
int melodia2[] = {NOTE_RE4 , 104,  
                 NOTE_SO4 , 316, NOTE_SO4 , 116, NOTE_SO4 , 104, NOTE_LA4 , 104,
                 NOTE_SI4 , 316, NOTE_SI4 , 116, NOTE_SI4 , 308, NOTE_SI4 , 116,
                 NOTE_LA4 , 108, NOTE_SI4 , 108, NOTE_DO4 , 104, NOTE_FA4d, 104,
                 NOTE_LA4 , 104, NOTE_SO4 , 104, NOTE_RE4 , 104,
                 
                 NOTE_SO4 , 316, NOTE_SO4 , 116, NOTE_SO4 , 104, NOTE_LA4 , 104,
                 NOTE_SI4 , 316, NOTE_SI4 , 116, NOTE_SI4 , 308, NOTE_SI4 , 116,
                 NOTE_LA4 , 108, NOTE_SI4 , 108, NOTE_DO4 , 104, NOTE_FA4d, 104,
                 NOTE_LA4 , 104, NOTE_SO4 , 104, NOTE_PSE , 108, NOTE_RE4 , 108,
                 
                 NOTE_RE5 , 108, NOTE_SI4 , 108, NOTE_MI5 , 308, NOTE_RE5 , 108,
                 NOTE_RE5 , 108, NOTE_DO5 , 108, NOTE_DO5 , 308, NOTE_DO5 , 108,
                 NOTE_DO5 , 108, NOTE_LA4 , 108, NOTE_RE5 , 308, NOTE_DO5 , 108,
                 NOTE_DO5 , 108, NOTE_SI4 , 108, NOTE_SI4 , 104, NOTE_RE4 , 104,
                 
                 NOTE_SO4 , 316, NOTE_SO4 , 116, NOTE_SO4 , 104, NOTE_LA4 , 104,
                 NOTE_SI4 , 316, NOTE_SI4 , 116, NOTE_SI4 , 308, NOTE_SI4 , 116,
                 NOTE_LA4 , 108, NOTE_SI4 , 108, NOTE_DO4 , 104, NOTE_FA4d, 104,
                 NOTE_LA4 , 104, NOTE_SO4 , 204, 

                };
int nr2 = 62;
float bpm2 = 60;

// Jingle Bells
int melodia3[] = {NOTE_RE4 , 104, NOTE_SI4 , 104, NOTE_LA4 , 104, NOTE_SO4 , 104,
                 NOTE_RE4 , 304, NOTE_RE4 , 108, NOTE_RE4 , 108,
                 NOTE_RE4 , 104, NOTE_SI4 , 104, NOTE_LA4 , 104, NOTE_SO4 , 104,
                 NOTE_MI4 , 304, NOTE_PSE , 104,
                 NOTE_MI4 , 104, NOTE_DO4 , 104, NOTE_SI4 , 104, NOTE_LA4 , 104,
                
                 NOTE_FA4d, 304, NOTE_PSE , 104,
                 NOTE_RE5 , 104, NOTE_RE5 , 104, NOTE_DO5 , 104, NOTE_LA4 , 104,
                 NOTE_SI4 , 304, NOTE_PSE , 104,
                 NOTE_RE4 , 104, NOTE_SI4 , 104, NOTE_LA4 , 104, NOTE_SO4 , 104,
                 NOTE_RE4 , 304, NOTE_RE4 , 108, NOTE_RE4 , 108,
                 
                 NOTE_RE4 , 104, NOTE_SI4 , 104, NOTE_LA4 , 104, NOTE_SO4 , 104,
                 NOTE_MI4 , 304, NOTE_MI4 , 104,
                 NOTE_MI4 , 104, NOTE_DO4 , 104, NOTE_SI4 , 104, NOTE_LA4 , 104,
                 NOTE_RE5 , 104, NOTE_RE5 , 104, NOTE_RE5 , 104, NOTE_RE5 , 104,
                 NOTE_MI5 , 104, NOTE_RE5 , 104, NOTE_DO5 , 104, NOTE_LA4 , 104,

                 NOTE_SO4 , 204, NOTE_RE5 , 204,
                 NOTE_SI4 , 104, NOTE_SI4 , 104, NOTE_SI4 , 204,
                 NOTE_SI4 , 104, NOTE_SI4 , 104, NOTE_SI4 , 204,
                 NOTE_SI4 , 104, NOTE_RE5 , 104, NOTE_SO4 , 308, NOTE_LA4 , 108,
                 NOTE_SI4 , 304, NOTE_PSE , 104,

                 NOTE_DO5 , 104, NOTE_DO5 , 104, NOTE_DO5 , 308, NOTE_DO5 , 108,
                 NOTE_DO5 , 104, NOTE_SI4 , 104, NOTE_SI4 , 104, NOTE_SI4 , 108, NOTE_SI4 , 108,
                 NOTE_SI4 , 104, NOTE_LA4 , 104, NOTE_LA4 , 104, NOTE_SI4 , 104,
                 NOTE_LA4 , 204, NOTE_RE5 , 204, 
                 NOTE_SI4 , 104, NOTE_SI4 , 104, NOTE_SI4 , 204,

                 NOTE_SI4 , 104, NOTE_SI4 , 104, NOTE_SI4 , 204,
                 NOTE_SI4 , 104, NOTE_RE5 , 104, NOTE_SO4 , 308, NOTE_LA4 , 108,
                 NOTE_SI4 , 304, NOTE_PSE , 104,
                 
                 NOTE_DO5 , 104, NOTE_DO5 , 104, NOTE_DO5 , 308, NOTE_DO5 , 108,
                 NOTE_DO5 , 104, NOTE_SI4 , 104, NOTE_SI4 , 104, NOTE_SI4 , 108, NOTE_SI4 , 108,

                 NOTE_RE5 , 104, NOTE_RE5 , 104, NOTE_DO5 , 104, NOTE_LA4 , 104,
                 NOTE_SO4 , 404

                };
int nr3 = 105;
float bpm3 = 75;


// dichiarazioni generali

int nr;
float bpm;

float minuto = 60;
float millesimo = 1000;

float tempo;
float duratanota;
float numeratore;
float denominatore;

int CodNota;
int CodTempo;



void setup() {
  //Serial.begin(9600);
  //Serial.println("PitMusica Running...");
  
  // pinMode(pintasto, INPUT);
}

void loop() {
   for (int i = 1; i <= 3; i++) {
       switch (i) {
          case 1:
            nr = nr1;
            bpm = bpm1;
            break;
          case 2:
            nr = nr2;
            bpm = bpm2;
            break;
          case 3:
            nr = nr3;
            bpm = bpm3;
            break;

          default: 
            break;
      }; 
      
      for (int numero = 0; numero < nr; numero++) {
         switch (i) {
           case 1:
              CodNota =melodia1[numero * 2];
              CodTempo = melodia1[numero * 2 + 1];
              break;
           case 2:
              CodNota =melodia2[numero * 2];
              CodTempo = melodia2[numero * 2 + 1];
              break;
           case 3:
              CodNota =melodia3[numero * 2];
              CodTempo = melodia3[numero * 2 + 1];
              break;
           default: 
              break;
         };

         numeratore = int (CodTempo/100);
         denominatore = CodTempo - numeratore * 100;
         tempo = numeratore/denominatore;
         duratanota = minuto * millesimo * numeratore / denominatore / bpm;
         tone(pinbuzz, CodNota, duratanota);
         delay(duratanota * 1.30);
         noTone(pinbuzz);
         
         /*
         Serial.print("numero: " );
         Serial.print(numero);
         Serial.print(" melodia[numero * 2]: " );
         Serial.print(melodia[numero * 2]);
         Serial.print(" melodia[numero * 2 +1]: " );
         Serial.print(melodia[numero * 2 +1]);


         Serial.print(" numeratore: " );
         Serial.print(numeratore);

         
         Serial.print(" denominatore: " );
         Serial.print(denominatore);
         Serial.print(" tempo: " );
         Serial.print(tempo);
         
         Serial.print(" bpm: " );
         Serial.print(bpm);
         
         Serial.print(" duratanota: " );
         Serial.println(duratanota);
         */
         
      }
      delay(2000);
   }
   
}

 


// A = LA
// B = SI
// C = DO
// D = RE
// E = MI
// F = FA
// G = SOL
//
// Conversione dalla notazione anglosassone a quella naturale
// ZappocoS 28/05/2015
//
#define NOTE_PSE   0
#define NOTE_SI0  31
//
#define NOTE_DO1  33
#define NOTE_DO1d 35
#define NOTE_RE1  37
#define NOTE_MI1b 39
#define NOTE_MI1  41
#define NOTE_FA1  44
#define NOTE_FA1d 46
#define NOTE_SO1  49
#define NOTE_SO1d 52
#define NOTE_LA1  55
#define NOTE_SI1b 58
#define NOTE_SI1  62
//
#define NOTE_DO2  65
#define NOTE_DO2d 69
#define NOTE_RE2  73
#define NOTE_MI2b 78
#define NOTE_MI2  82
#define NOTE_FA2  87
#define NOTE_FA2d 93
#define NOTE_SO2  98
#define NOTE_SO2d 104
#define NOTE_LA2  110
#define NOTE_SI2b 117
#define NOTE_SI2  123
//
#define NOTE_DO3  131
#define NOTE_DO3d 139
#define NOTE_RE3  147
#define NOTE_MI3b 156
#define NOTE_MI3  165
#define NOTE_FA3  175
#define NOTE_FA3d 185
#define NOTE_SO3  196
#define NOTE_SO3d 208
#define NOTE_LA3  220
#define NOTE_SI3b 233
#define NOTE_SI3  247
//
#define NOTE_DO4  262
#define NOTE_DO4d 277
#define NOTE_RE4  294
#define NOTE_MI4b 311
#define NOTE_MI4  330
#define NOTE_FA4  349
#define NOTE_FA4d 370
#define NOTE_SO4  392
#define NOTE_SO4d 415
#define NOTE_LA4  440
#define NOTE_SI4b 466
#define NOTE_SI4  494
//
#define NOTE_DO5  523
#define NOTE_DO5d 554
#define NOTE_RE5  587
#define NOTE_MI5b 622
#define NOTE_MI5  659
#define NOTE_FA5  698
#define NOTE_FA5d 740
#define NOTE_SO5  784
#define NOTE_SO5d 831
#define NOTE_LA5  880
#define NOTE_SI5b 932
#define NOTE_SI5  988
//
#define NOTE_DO6  1047
#define NOTE_DO6d 1109
#define NOTE_RE6  1175
#define NOTE_MI6b 1245
#define NOTE_MI6  1319
#define NOTE_FA6  1397
#define NOTE_FA6d 1480
#define NOTE_SO6  1568
#define NOTE_SO6d 1661
#define NOTE_LA6  1760
#define NOTE_SI6b 1865
#define NOTE_SI6  1976
//
#define NOTE_DO7  2093
#define NOTE_DO7d 2217
#define NOTE_RE7  2349
#define NOTE_MI7b 2489
#define NOTE_MI7  2637
#define NOTE_FA7  2794
#define NOTE_FA7d 2960
#define NOTE_SO7  3136
#define NOTE_SO7d 3322
#define NOTE_LA7  3520
#define NOTE_SI7b 3729
#define NOTE_SI7  3951
//
#define NOTE_DO8  4186
#define NOTE_DO8d 4435
#define NOTE_RE8  4699
#define NOTE_MI8b 4978



ZappocoS, 29 maggio 2015