RedBoard Santa Trap

Pages
Contributors: Sarah Al-Mutlaq
Favorited Favorite 2

Overview

A fun holiday project to try for anyone looking to catch Santa on Christmas!

alt text

Required Materials

Electronics

Tools:

Suggested Reading

Here are some other tutotials that may be helpful while completing this project.

Assembly

With this set up, you can try to catch Santa coming down the chimney, but even if he’s clever enough to get past motion sensor without being detected, you can catch him when he goes to drink the milk left out for him.

alt text

Click image for a larger view.

The circuit works as follows:

  • The LED is connected from ground to 5V through a resistor so it will be lit when there is power. This functions as an “ON” indicator, should the circuitry be far away from the RedBoard.
  • The motion sensor has an alarm pin that goes LOW when it detects motion, so we can check that by hooking it up to the RedBoard and using a resistor to pull it to HIGH (or 5V) when it isn’t detecting motion.
  • The tilt sensor acts like a closed circuit when it is upright. However, it acts like an open circuit if it is tilted past 15 degrees. This means that we can set up a circuit with two resistors and hook it up to the RedBoard so that when the tilt sensor is upright, the RedBoard pin will read HIGH (or 5V), and, when it is tilted, the RedBoard pin will read LOW (0V or Ground).
  • All that is left is to hook up the buzzer, which just needs to go from ground to an output pin from the RedBoard, where the RedBoard can give it power. In this fritzing diagram, it’s set up with a resistor, but that isn’t needed unless you want the buzzer to be quieter. The bigger the resister you put in series with the buzzer, the less noise the buzzer will make.

Code

With some programing to the RedBoard, which can be programmed with Arduino IDE, we can program the buzzer to play a song when motion or the cup tilting is detected. Upload the following code to your RedBoard.

language:c
int pirPin = 2; // alarm pin from motion sensor
int tilPin = 7; // alarm pin from tilt sensor
const int buzzerPin = 9; // output pin for buzzer

const int songLength = 18; // number of notes in the buzzer song
char notes[] = "e e e e e e egcde "; // notes and rests of the buzzer song
int beats[] = {1,0.2,1,0.2,2,0.2,1,0.2,1,0.2,2,0.2,1,1,1,1,4,1}; // length of each note and rest in the buzzer song
int tempo = 150; // speed of the buzzer song


int motionCheck[] = {1, 1, 1, 1, 1, 1}; // storing the last 6 alarms from the motion sensor
int j = 0; // counter
int tiltCheck[] = {1, 1, 1, 1, 1 ,1}; // storing the last 6 alarms from the tilt sensor
int t = 0; // counter




void setup(){
 Serial.begin(9600); 
 pinMode(pirPin, INPUT); // setting motion alarm pin to an input on the board
 pinMode(tilPin, INPUT); // setting tilt alarm pin to an input on the board
 pinMode(buzzerPin, OUTPUT); // setting the buzzer control pin to an output on the board
 }


void loop(){
  // We loop for 6 numbers to get the last 6 motion/tilt alarm values 
  // because they happen very fast and sometimes drop low when motion/tilt is not
  // actually detected, so storing the last 6 will let us check if motion was 
  // actaully deteced or if it was a false read. 
  for (j=0; j < 6; j++)
  {
    motionCheck[j] = digitalRead(pirPin);
  }
  for (t=0; t < 6; t++)
  {
    tiltCheck[t] = digitalRead(tilPin);
  }

  int i = 0; // counter for the buzzer song
  int duration; // value for the duration of the buzzer song


  // This if test is asking if motion or tilt has been sensed by
  // checking that the last 6 tests came back sensing motion or tilt.
  if(((motionCheck[0] == 0) && (motionCheck[1] == 0) && (motionCheck[2] == 0) && (motionCheck[3] == 0) && (motionCheck[4] == 0) && (motionCheck[5] == 0)) or ((tiltCheck[0] == 0) && (tiltCheck[1] == 0) && (tiltCheck[2] == 0) && (tiltCheck[3] == 0) && (tiltCheck[4] == 0) && (tiltCheck[5] == 0))){
    // This for loop reads the buzzer song
      for (i = 0; i < songLength; i++) // step through the song arrays
      {
        duration = beats[i] * tempo; // length of note/rest in ms

        if (notes[i] == ' ') // is this a rest?         
        {
          delay(duration);  // then pause for a moment         
        }
        else // otherwise, play the note                       
        {
          tone(buzzerPin, frequency(notes[i]), duration); 
          delay(duration);    // wait for tone to finish        
        }
        delay(tempo/10);     // brief pause between notes   
      }
    }
    delay(1000);   //wait a second before starting all the checks over again
}


int frequency(char note) 
{
  // This function takes a note character (a-g), and returns the
  // corresponding frequency in Hz for the tone() function.

  int i;
  const int numNotes = 8;  // number of notes we're storing

  // The following arrays hold the note characters and their
  // corresponding frequencies. The last "C" note is uppercase
  // to separate it from the first lowercase "c". If you want to
  // add more notes, you'll need to use unique characters.

  // For the "char" (character) type, we put single characters
  // in single quotes.

  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  /*
  note  frequency
  c     262 Hz
  d     294 Hz
  e     330 Hz
  f     349 Hz
  g     392 Hz
  a     440 Hz
  b     494 Hz
  C     523 Hz
  */

  // Now we'll search through the letters in the array, and if
  // we find it, we'll return the frequency for that note.

  for (i = 0; i < numNotes; i++)  // Step through the notes
  {
    if (names[i] == note)         // Is this the one?
    {
      return(frequencies[i]);     // Yes! Return the frequency
    }
  }
  return(0);  // We looked through everything and didn't find it,
              // but we still need to return a value, so return 0.
}

With this you should have everything you need to put it all together. Once you have everything hooked up and you upload the included code you should be ready for Santa!

Resources and Going Further

For more Christmas electronics fun, check out these other SparkFun projects.