Christmas Tree with music

In this project we created an Christmas tree with infrared detection.
When someone passes by, the sensors detects movement and the Christmas tree begins to blink and plays Christmas songs.

Needed

  • Arduino
  • Arduino waveshield
  • Infrared movement sensor
  • SD card
  • Christmas songs


  • Setup

    Infrared Detection

    Code

    1. #include "kerstDetectie.h"
    2. #include <AF_Wave.h>
    3. #include <avr/pgmspace.h>
    4. #include "util.h"
    5. #include "wave.h"
    6.  
    7. AF_Wave card;
    8. File f;
    9. Wavefile wave;      // only one!
    10.  
    11. #define redled 9
    12.  
    13. uint16_t samplerate;
    14.  
    15. int inPin = 6;   // pushbutton connected to digital pin 7
    16. int val = 0;     // variable to store the read value
    17. int relay1Pin =7;//relay pin 1 to 7
    18. int relay2Pin =8;// relay pin to 8
    19. int ledPin = 11; // LED connected to digital pin 13
    20.  
    21.  
    22.  
    23. void ls() {
    24.   char name[13];
    25.   int ret;
    26.  
    27.   card.reset_dir();
    28.   putstring_nl("Files found:");
    29.   while (1) {
    30.     ret = card.get_next_name_in_dir(name);
    31.     if (!ret) {
    32.        card.reset_dir();
    33.        return;
    34.     }
    35.     Serial.println(name);
    36.   }
    37. }
    38.  
    39.  
    40. boolean playfile(char *name) {
    41.    f = card.open_file(name);
    42.    if (!f) {
    43.       putstring_nl(" Couldn’t open file"); return false;
    44.    }
    45.    if (!wave.create(f)) {
    46.      putstring_nl(" Not a valid WAV"); return false;
    47.    }
    48.    // ok time to play!
    49.    wave.play();
    50.    return true;
    51. }
    52. void playcomplete(char *name) {
    53.   uint16_t potval;
    54.   uint32_t newsamplerate;
    55.  
    56.   playfile(name);
    57.   samplerate = wave.dwSamplesPerSec;
    58.   while (wave.isplaying) {
    59.         // you can do stuff here!
    60.          //digitalWrite(ledPin,HIGH);
    61.          digitalWrite(relay1Pin,HIGH);
    62.          digitalWrite(relay2Pin,LOW);
    63.          delay(50);
    64.          //digitalWrite(ledPin,LOW);
    65.          digitalWrite(relay2Pin,HIGH);
    66.          digitalWrite(relay1Pin,LOW);
    67.         delay(50);
    68.  
    69.    }
    70.   digitalWrite(relay1Pin,HIGH);
    71.   digitalWrite(relay2Pin,HIGH);
    72.  
    73.   putstring_nl("klaar met spelen");
    74.   card.close_file(f);
    75. }
    76.  
    77. //————————–
    78.  
    79.  
    80. void setup()
    81. {
    82.   pinMode(ledPin, OUTPUT);
    83.   pinMode(relay1Pin,OUTPUT);
    84.   pinMode(relay2Pin,OUTPUT);
    85.   pinMode(inPin, INPUT);
    86.  
    87.   pinMode(2, OUTPUT);
    88.    pinMode(3, OUTPUT);
    89.    pinMode(4, OUTPUT);
    90.    pinMode(5, OUTPUT);
    91.    pinMode(redled, OUTPUT);
    92.  
    93.   Serial.begin(9600);           // set up Serial library at 9600 bps
    94.   Serial.println("Wave test!");
    95.  
    96.  
    97.   if (!card.init_card()) {
    98.  
    99.     putstring_nl("Card init. failed!"); return;
    100.   }
    101.   if (!card.open_partition()) {
    102.     putstring_nl("No partition!"); return;
    103.   }
    104.   if (!card.open_filesys()) {
    105.     putstring_nl("Couldn’t open filesys"); return;
    106.   }
    107.  
    108.  
    109.  
    110.   if (!card.open_rootdir()) {
    111.     putstring_nl("Couldn’t open dir"); return;
    112.   }
    113.  
    114.   putstring_nl("Files found:");
    115. //  ls();
    116.  
    117.   playcomplete("SHIP.WAV");
    118.  
    119.  
    120.   digitalWrite(relay1Pin,HIGH);
    121.   delay(500);
    122.   digitalWrite(relay2Pin,HIGH);
    123.   delay(1000);
    124.   digitalWrite(relay1Pin,LOW);
    125.   digitalWrite(relay2Pin,LOW);
    126.   delay(500);
    127.   digitalWrite(relay1Pin,HIGH);
    128.   digitalWrite(relay2Pin,HIGH);
    129. }
    130. //————————-
    131.  
    132. uint8_t tracknum = 0;
    133.  
    134.  
    135. void playSound(){
    136.    uint8_t i, r;
    137.    char c, name[15];
    138.    String fileName;
    139.  
    140.    card.reset_dir();
    141.    // scroll through the files in the directory
    142.    for (i=0; i<tracknum+1; i++) {
    143.          r = card.get_next_name_in_dir(name);
    144.          if (!r) {
    145.            // ran out of tracks! start over
    146.            tracknum = 0;
    147.            return;
    148.          }
    149.    }
    150.    // reset the directory so we can find the file
    151.    card.reset_dir();
    152.    fileName = name;
    153.    if (fileName == "SHIP.WAV"){
    154.            putstring("\n\rNOT Playing "); Serial.println(name);
    155.            tracknum++;
    156.    } else {
    157.            putstring("\n\rPlaying  :]]]"); Serial.println(name);
    158.            playcomplete(name);
    159.            tracknum++;
    160.    }
    161.  
    162. }
    163.  
    164.  
    165. void loop()
    166. {
    167.   val = digitalRead(inPin);   // read the input pin
    168.   digitalWrite(ledPin, val);    // sets the LED to the button’s value
    169.   if (val == HIGH){
    170.           playSound();
    171.  
    172.           delay(3000);
    173.  
    174.           putstring_nl("Check movment…");
    175.           val = digitalRead(inPin);
    176.  
    177.           //wait for pin to go low
    178.           while(val == HIGH){
    179.                   val = digitalRead(inPin);
    180.                   putstring_nl("Waiting, still movement…");
    181.                   delay(2000);
    182.  
    183.  
    184.           }
    185.  
    186.   }
    187.  
    188.  
    189. }

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    *


    − 6 = three

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>