Harry Potter music box con Arduino

Harry Potter music box con Arduino- Arduino, Proyectos
5/5 - (1 voto)

Mi hija es una enamorada de la saga de Harry Potter, así que decidí hacerle esta pequeña caja de música. Los materiales empleados fueron los siguientes:

  • Caja de madera de un bazar
  • Bote pintura spray color cobre
  • Arduino nano
  • Buzzer
  • 2 Leds con sus dos resistencias de 220 Ω
  • Pila y portapilas de 9 Voltios
  • Final de carrera.
  • Adornos con los logos de harry Potter impresos con la impresora 3D

Este es el resultado

En primer lugar imprimí con la impresora3D los adornos que os dejo al final de la entrada, pinté la caja, y los adornos impresos con el spray, por último montamos el circuito y lo ajustamos todo a la caja.

La conexión del final de carrera lo hacemos con los terminales NC (normalmente cerrado, ver foto adjunta), es decir,  cuando está pulsado (caja cerrada), hace de interruptor abierto. Cuando abrimos la caja, el interruptor se cerraría y haría funcionar el circuito. En la foto se aprecia en la tapa de arriba un taquito de madera que presionará sobre el final de carrera cuando la caja está cerrada.

Con respecto a la conexión con Arduino, adjunto a continuación el esquema de conexión y el sketch, puedes mirar también mi entrada sobre como usar un buzzer con Arduino

Harry Potter music box con Arduino- Arduino, Proyectos
// www.tecnosalva.com
 
//Harry Potter Theme arranged by Dodo le Guitariste

// TONES  ========================================== 
#define  Bb    2146
#define  b     2024 
#define  c     1912     
#define  db    1805  
#define  d     1703   
#define  eb    1608 
#define  e     1517      
#define  f     1432     
#define  gb    1351      
#define  g     1276     
#define  ab    1233 
#define  a     1136
#define  bb    1073  
#define  B     1012
#define  C     956 
#define  Db    902 
#define  D     851


// Define a special note, 'R', to represent a rest
#define  R     0

int speakerOut = 9;
int ledPin1=12;
int ledPin2=13;

void setup() { 
  pinMode(speakerOut, OUTPUT);
  pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
  } 


// MELODY and TIMING  =======================================
//  melody[] is an array of notes, accompanied by beats[], 
//  which sets each note's relative length (higher #, longer note) 
int melody[] = {  b,  e,  g, gb,  e,  B,  a, gb,  e,  g, gb, eb,  f,  b,  b,  e,  g,  gb,  e,  B,  D,  Db,  C,  ab,  C,  B,  bb,  gb,  g,  e,   g,  B,  g,  B,  g,  C,  B,  bb,  gb,  g,  B,  bb,  Bb,  c,   B,  g,  B,  g,  B,  g,  D,  Db,  C,  ab,  C,  B,  bb,  gb,  g,   e, R };
int beats[]  = { 32, 64, 16, 32, 64, 32, 64, 64, 64, 16, 32, 64, 32, 64, 32, 64, 16,  32, 64, 32, 64,  32, 64,  32, 64, 32,  64,  32, 32, 128, 32, 64, 32, 64, 32, 64, 32,  64,  32, 64, 16,  32,  64, 32, 128, 32, 64, 32, 64, 32, 64,  32, 64,  32, 64, 32,  64,  32, 32, 128, 8 }; 
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables
int tone_ = 0;
int beat = 0;
long duration  = 0;

// PLAY TONE  ==============================================
// Pulse the speaker to play a tone for a particular duration
void playTone() {
  long elapsed_time = 0;
  if (tone_ > 0) { // if this isn't a Rest beat, while the tone has 
    //  played less long than 'duration', pulse speaker HIGH and LOW
    while (elapsed_time < duration) {

      digitalWrite(speakerOut,HIGH);
      delayMicroseconds(tone_ / 2);

      // DOWN
      digitalWrite(speakerOut, LOW);
      delayMicroseconds(tone_ / 2);

      // Keep track of how long we pulsed
      elapsed_time += (tone_);
    } 
  }
  
  else { // Rest beat; loop times delay
    for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
      delayMicroseconds(duration);  
    }                                
  }                                 
}

// LET THE WILD RUMPUS BEGIN =============================
void loop() {
  // Set up a counter to pull from melody[] and beats[]
  for (int i=0; i<MAX_COUNT; i++) {
    tone_ = melody[i];
    beat = beats[i];

    duration = beat * tempo; // Set up timing

if (duration>=340000){
    digitalWrite(ledPin1,HIGH);
    digitalWrite(ledPin2,LOW);
  }else{
digitalWrite(ledPin1,LOW);
    digitalWrite(ledPin2,HIGH);
  }
    playTone(); 
    // A pause between notes...
    delayMicroseconds(pause);
    }
  }

Archivos adjuntos

3 comentarios en «Harry Potter music box con Arduino»

Deja un comentario