# RedBoard Santa Trap

Pages
Contributors: Sarah Al-Mutlaq

## 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++)
{
}
for (t=0; t < 6; t++)
{
}

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!