/*
Final Project
Julie Street, Mayurika Gupta, Ben Firullo,
Michael Russo, Tanak Patel
Alarm system that plays music on the Piezo buzzer
and does a light show when you wave your hand over
the distance sensor. Song repeats until you wave your hand again.
Application purpose: secondary alarm.
Imagine placing it near your phone, so when you go to hit snooze
in the morning, you trigger this secondary alarm and light show
to encourage you to actually wake up.
*/
#include "Ultrasonic.h"
#include <stdio.h>
/* Macro Define */
#define BUZZER_PIN 39 /* pin of the Piezo Buzzer */
#define ULTRASONIC_PIN 24 /* pin of the Ultrasonic Ranger */
/* Global Variables */
int length = 61; /* the number of notes */
char notes[] = "cdececedeffedfefgegegfgaagfagcdefgaadefgabbefgabCCbafbgCgedc "; //notes in the song
int beats[] = {3,1,3,1,2,2,4,3,1,1,1,1,1,8,3,1,3,1,2,2,4,3,1,1,1,1,1,8,3,1,1,1,1,1,8,3,1,1,1,1,1,8,3,1,1,1,1,1,6,1,1,2,2,2,2,2,2,2,2,8,4}; //length of each note
int tempo = 100; // speed of song. Lower number = faster
int toggle = 0;
int distance = 0; // distance reading from ultrasonic ranger
Ultrasonic ultrasonic(ULTRASONIC_PIN); /* Ultrasonic Ranger object */
/*pin setup*/
void setup()
{
pinMode(BUZZER_PIN, OUTPUT);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(28, OUTPUT);
pinMode(29, OUTPUT);
pinMode(5, OUTPUT);
}
//main loop
void loop()
{
distance = ultrasonic.MeasureInCentimeters(); /* read the value from the sensor */
//check distance
if(distance < 20 and distance > 3 )
{
music();
}
}
/* play tone */
void playTone(int tone, int duration)
{
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(tone);
digitalWrite(BUZZER_PIN, LOW);
delayMicroseconds(tone);
}
}
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
//play note
void playNote(char note, int duration)
{
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++)
{
//if names at current index equals given note, play tone
if (names[i] == note)
{
playTone(tones[i], duration);
}
}
}
/*notes of Do-Re-Mi*/
void music() {
int pins[] = {25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5, 25, 26, 27, 28, 29, 5};
//Loop through each note
for(int i = 0; i < length; i++)
{
//space indicates a pause
if(notes[i] == ' ')
{
delay(beats[i] * tempo);
}
else
{
playNote(notes[i], beats[i] * tempo);
digitalWrite(pins[i], HIGH);
}
delay(tempo / 2); /* delay between notes */
digitalWrite(pins[i], LOW);
}
}
Comments