Advertisement
Villalba2006

POST_24

Nov 24th, 2016
13,616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.60 KB | None | 0 0
  1. /*  TITULO: Banda sonora de Star Wars con un zumbador.
  2.  
  3.     AUTOR:
  4.    
  5.     MARIANO DEL CAMPO GARCÍA (@2016) --> INGENIERO TÉCNICO INDUSTRIAL ESPECIALIDAD ELECTRÓNICA
  6.     - FACEBOOK: https://www.facebook.com/mariano.delcampogarcia
  7.     - TWITTER: https://twitter.com/MarianoCampoGa
  8.     - CORREO: marianodc83@gmail.com
  9.    
  10.    
  11.     DESCRIPCIÓN DEL PROGRAMA
  12.    
  13.     Con este programa vamos a ser capaces de reproducir la banda sonora de "Star Wars" con un zumbador.
  14.     Esto se consigue aplicando tonos de una determinada frecuencia y duración. Alternamente se iluminarán
  15.     2 LEDS, cada vez que suene un nuevo tono.
  16.    
  17.    
  18.     ESQUEMA DE CONEXION
  19.    
  20.                                       +-----+
  21.          +----[PWR]-------------------| USB |--+
  22.          |                            +-----+  |
  23.          |         GND/RST2  [ ][ ]            |
  24.          |       MOSI2/SCK2  [ ][ ]  A5/SCL[ ] |    
  25.          |          5V/MISO2 [ ][ ]  A4/SDA[ ] |    
  26.          |                             AREF[ ] |
  27.          |                              GND[ ] |
  28.          | [ ]N/C                    SCK/13[ ] |  
  29.          | [ ]IOREF                 MISO/12[ ] |   LED_2(+)
  30.          | [ ]RST                   MOSI/11[ ]~|   LED_1(+)
  31.          | [ ]3V3    +---+               10[ ]~|  
  32.          | [ ]5v    -| A |-               9[ ]~|  
  33.          | [ ]GND   -| R |-               8[ ] |   Zumbador(+)
  34.          | [ ]GND   -| D |-                    |
  35.          | [ ]Vin   -| U |-               7[ ] |  
  36.          |          -| I |-               6[ ]~|  
  37.          | [ ]A0    -| N |-               5[ ]~|  
  38.          | [ ]A1    -| O |-               4[ ] |  
  39.          | [ ]A2     +---+           INT1/3[ ]~|  
  40.          | [ ]A3                     INT0/2[ ] |  
  41.          | [ ]A4/SDA  RST SCK MISO     TX>1[ ] |  
  42.          | [ ]A5/SCL  [ ] [ ] [ ]      RX<0[ ] |  
  43.          |            [ ] [ ] [ ]              |
  44.          |  UNO_R3    GND MOSI 5V  ____________/
  45.           \_______________________/
  46.  
  47.   NOTAS:
  48.  
  49.    - Cátodo(-) de LED_1 y LED_2 a GND (pata corta) a través de una R=220 omhs (difrente para cada LED).
  50.    - Zumbador(-) a GND.
  51.  
  52. */
  53.  
  54.  
  55.   // Declaración de las frecuencias de las notas musicales
  56.   const float c = 261.63; // Do (Octava 0)
  57.   const float d = 293.66; // Re (Octava 0)
  58.   const float e = 329.63; // Mi (Octava 0)
  59.   const float f = 349.23; // Fa (Octava 0)
  60.   const float g = 392.00; // Sol (Octava 0)
  61.   const float gS = 415.30;  // Sol# (Octava 0)
  62.   const float a = 440.00; // La (Octava 0)
  63.   const float b = 466.16; // La# (Octava 0)
  64.   const float cH = 523.25;   // Do (Octava 1)
  65.   const float cSH = 554.37;  // Do# (Octava 1)
  66.   const float dH = 587.33; // Re (Octava 1)
  67.   const float dSH = 622.25; // Re# (Octava 1)
  68.   const float eH = 659.26; // Mi (Octava 1)
  69.   const float fH = 698.46; // Fa (Octava 1)
  70.   const float fSH = 739.99; // Fa# (Octava 1)
  71.   const float gH = 783.99;  // Sol (Octava 1)
  72.   const float gSH = 830.61; // Sol# (Octava 1)
  73.   const float aH = 880.00; // La (Octava 1)
  74.    
  75.   const int zumbador = 8; // Pin digital para el zumbador
  76.   const int LED_1 = 11; // Pin digital para el LED 1
  77.   const int LED_2 = 12; // Pin digital para el LED 2
  78.    
  79.   int contador = 0;
  80.    
  81.   void setup()
  82.   {
  83.     pinMode(zumbador, OUTPUT); // Pin digital 8 como salida
  84.     pinMode(LED_1, OUTPUT); // Pin digital 12 como salida
  85.     pinMode(LED_2, OUTPUT); // Pin digital 13 como salida
  86.   }
  87.    
  88.   void loop()
  89.   {
  90.     // Suena la primera sección
  91.     primeraSeccion();
  92.    
  93.     // Suena la segunda sección
  94.     segundaSeccion();
  95.    
  96.     // Variante 1
  97.     tono(f, 250);  
  98.     tono(gS, 500);  
  99.     tono(f, 350);  
  100.     tono(a, 125);
  101.     tono(cH, 500);
  102.     tono(a, 375);  
  103.     tono(cH, 125);
  104.     tono(eH, 650);
  105.    
  106.     delay(500);
  107.    
  108.     // Se repite la segunda sección
  109.     segundaSeccion();
  110.    
  111.     // Variante 2
  112.     tono(f, 250);  
  113.     tono(gS, 500);  
  114.     tono(f, 375);  
  115.     tono(cH, 125);
  116.     tono(a, 500);  
  117.     tono(f, 375);  
  118.     tono(cH, 125);
  119.     tono(a, 650);  
  120.    
  121.     delay(650);
  122.   }
  123.  
  124.   // Función que ejecuta cada tono
  125.   void tono(int frecuencia, int duracion)
  126.   {
  127.     // Suena el tono en el zumbador
  128.     tone(zumbador, frecuencia, duracion);
  129.    
  130.     // Se enciende LED_1 o LED_2 alternamente cada vez que suena un nuevo tono
  131.     if(contador % 2 == 0)
  132.     {
  133.       digitalWrite(LED_1, HIGH);
  134.       delay(duracion);
  135.       digitalWrite(LED_1, LOW);
  136.     }
  137.     else
  138.     {
  139.       digitalWrite(LED_2, HIGH);
  140.       delay(duracion);
  141.       digitalWrite(LED_2, LOW);
  142.     }
  143.    
  144.     // Para de sonar el tono en el zumbador
  145.     noTone(zumbador);
  146.    
  147.     delay(50);
  148.    
  149.     // Se incrementa el contador
  150.     contador++;
  151.   }
  152.  
  153.   // Función de la primera sección
  154.   void primeraSeccion()
  155.   {
  156.     tono(a, 500);
  157.     tono(a, 500);    
  158.     tono(a, 500);
  159.     tono(f, 350);
  160.     tono(cH, 150);  
  161.     tono(a, 500);
  162.     tono(f, 350);
  163.     tono(cH, 150);
  164.     tono(a, 650);
  165.    
  166.     delay(500);
  167.    
  168.     tono(eH, 500);
  169.     tono(eH, 500);
  170.     tono(eH, 500);  
  171.     tono(fH, 350);
  172.     tono(cH, 150);
  173.     tono(gS, 500);
  174.     tono(f, 350);
  175.     tono(cH, 150);
  176.     tono(a, 650);
  177.    
  178.     delay(500);
  179.   }
  180.  
  181.   // Función de la segunda sección  
  182.   void segundaSeccion()
  183.   {
  184.     tono(aH, 500);
  185.     tono(a, 300);
  186.     tono(a, 150);
  187.     tono(aH, 500);
  188.     tono(gSH, 325);
  189.     tono(gH, 175);
  190.     tono(fSH, 125);
  191.     tono(fH, 125);    
  192.     tono(fSH, 250);
  193.    
  194.     delay(325);
  195.    
  196.     tono(a, 250);
  197.     tono(dSH, 500);
  198.     tono(dH, 325);  
  199.     tono(cSH, 175);  
  200.     tono(cH, 125);  
  201.     tono(b, 125);  
  202.     tono(cH, 250);  
  203.    
  204.     delay(350);
  205.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement