Feliz cumpleaños o un villancico navideño cuando abres un regalo

Hice un proyecto con Arduino y me gustaría compartirlo con ustedes ahora que se aproximan las fiestas navideñas.

El proyecto consiste en montar un circuito que incluya un buzzer pasivo (speaker) y un fotosensor LDR conectados a Arduino REV Uno (en mi caso). En este caso, es necesario alimentar el proyecto con una pila de 9V (es lo que usé) o con una bateria LIPO o similar. Todo estaría metido en un tuperware, yo utilicé el envase de ensaladilla rusa de una cadena de supermercados, parte de ese tuperware es traslucido, lo que permite la entrada de luz.

La idea es introducir todo el circuito con Arduino incluido dento de una caja de cartón que incluye una serie de regalos para un ser querido. La oscuridad que hay en el interior de la caja de cartón, hace que la melodía no suene por falta de luz.

Cuando el fotosensor recibe cierta cantidad de luz, el código registra su valor y dependiendo de éste, Arduino hace sonar la melodía a través del speaker que ha subido previamente a la placa. Yo escogí la melodía "Cumpleaños feliz", aunque ahora que están próximas las navidades, podrían elegir un villancico navideño.

En este proyecto usé

1 resistencia de 100 Kohm
1 fotorresistencia LDR
1 speaker
1 placa PCB
cables de protoboard
1 Arduino REV UNO
Un taladro para perforar en la placa PCB
Tuercas y tornillos

Las conexiones son las siguientes:

PLACA PCB ARDUINO DESCRIPCIÓN
CABLE ROJO (+) CABLE ROJO >> PIN 5V ALIMENTACION
CABLE NEGRO (-) CABLE NEGRO >>PIN GND ALIMENTACION
CABLE AMARILLO (+) CABLE AMARILLO >> PIN 10 BUZZER PASIVO (SPEAKER)
CABLE VIOLETA (-) CABLE VIOLETA >> PIN GND BUZZER PASIVO (SPEAKER)
CABLE VERDE (DATOS) CABLE VERDE >> PIN A0 FOTOSENSOR LDR

Les dejo 2 códigos que han sido tomados de Internet y que han sido adaptados por mí para satisfacer mis necesidades para este proyecto.

Happy Birthday code

// * Happy Birthday to You
// * Original Composition :
// * Composed by :
// * Coded By - http://forum.arduino.cc/index.php?topic=178460.0
// * Use BSD Clause 2 License for Distribution
// * Collection by GitHub User @abhishekghosh - https://github.com/AbhishekGhosh/Arduino-Buzzer-Tone-Codes

#define LDRPin A0
int BuzzerPin = 10;

// Variable donde se almacena el valor del LDR
int LDRValue = 0;

int length = 28; // the number of notes

char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";

int beats[] = { 2, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8, 8, 16, 1, 2, 2, 8, 8, 8, 8, 16, 1, 2, 2, 8, 8, 8, 16 };

int tempo = 150;

void playTone(int tone, int duration) {

  for (long i = 0; i < duration * 1000L; i += tone * 2) {

    digitalWrite(BuzzerPin, HIGH);

    delayMicroseconds(tone);

    digitalWrite(BuzzerPin, LOW);

    delayMicroseconds(tone);

  }

}

void playNote(char note, int duration) {

  char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B',

                  'c', 'd', 'e', 'f', 'g', 'a', 'b',

                  'x', 'y'
                 };

  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,

                  956,  834,  765,  593,  468,  346,  224,

                  655 , 715
                };

  int SPEE = 5;

  // play the tone corresponding to the note name

  for (int i = 0; i < 17; i++) {

    if (names[i] == note) {
      int newduration = duration / SPEE;
      playTone(tones[i], newduration);

    }

  }

}

void setup() {

  Serial.begin(9600);

  pinMode(LDRPin, INPUT);
  pinMode(BuzzerPin, OUTPUT);

}

void loop() {

  
LDRValue= analogRead(LDRPin);


  if (LDRValue > 512)
   {
    pinMode(BuzzerPin, HIGH);

    for (int i = 0; i < length; i++) {

      if (notes[i] == ' ') {

        delay(beats[i] * tempo); // rest

      } else {

        playNote(notes[i], beats[i] * tempo);

      }

      // pause between notes

      delay(tempo);

    }
  }
  if (LDRValue < 512)
  {
    pinMode(BuzzerPin, LOW);
  }
  delay (1000);
}

Merry christmas code

// * Jingle Bells
// * Collection by GitHub User @elubow - https://gist.github.com/elubow/7844436

#define LDRPin A0
int BuzzerPin = 10;

// Variable donde se almacena el valor del LDR
int LDRValue = 0;  

int length = 26;
char notes[] = "eeeeeeegcde fffffeeeeddedg";
int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};

int tempo = 300;
void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(BuzzerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(BuzzerPin, LOW);
    delayMicroseconds(tone);
  }
}
void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  
  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void setup() {

pinMode(LDRPin, INPUT);
pinMode(BuzzerPin, OUTPUT);

}

void loop() {

LDRValue= analogRead(LDRPin);

  if(LDRValue > 512)
  {   

   pinMode(BuzzerPin,HIGH);

       for (int i = 0; i < length; i++) {
          if (notes[i] == ' ') {
            delay(beats[i] * tempo); // rest
          } else {
            playNote(notes[i], beats[i] * tempo);
          }
          
          // pause between notes
          delay(tempo / 2); 
         }

  }
  
  if(LDRValue < 512)
        {
        pinMode(BuzzerPin,LOW);
        }
}