/* Macro Define */
#define BUZZER_PIN 39 /* sig pin of the Grove Buzzer */
#define LED RED_LED
#define LED1 GREEN_LED
#define LED2 BLUE_LED
#define PIR_MOTION_SENSOR 40 /* sig pin of the PIR sensor */
#include "TM1637.h"
#define CLK 38 //pins definitions for TM1637 and can be changed to other ports
#define DIO 37
TM1637 tm1637(CLK,DIO);
// Song Number
int song = 1; // 1 mega // 2 fur // 3 tot
int length = 126 + 35 + 36 + 24 + 42 + 46 + 43 + 82 + 46; // mega
//int length = 41; // fur
// int length = 80; // tot
// Initializing the sounds of the notes
// Higher tone value corresponds to lower note
int as0 = 4290;
int b0 = 4049;
int c1 = 3822;
int cs1 = 3607;
int d1 = 3405;
int ds1 = 3214;
int e1 = 3033;
int f1 = 2863;
int fs1 = 2703;
int g1 = 2551;
int gs1 = 2408;
int a1 = 2272;
int as1 = 2145;
int b1 = 2025;
int c2 = 1915;
int cs2 = 1803;
int d2 = 1700;
int ds2 = 1607;
int e2 = 1519;
int f2 = 1432;
int fs2 = 1351;
int g2 = 1275;
int gs2 = 1203;
int a2 = 1136;
int as2 = 1072;
int b2 = 1014;
int c3 = 956;
int cs3 = 902;
int d3 = 851;
int ds3 = 804;
int e3 = 758;
int f3 = 716;
int M = d1;
// initilaizing notes and tones to play megalovania
int mega[] = {
// Riff 1
d2, d2, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
c2, c2, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
b1, b1, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
as1, as1, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
// Riff 2
d2, d2, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
c2, c2, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
b1, b1, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
as1, as1, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
// Melody 1
f2, f2, f2, 0, f2, f2, d2, d2,
f2, f2, f2, 0, g2, gs2, g2, gs2, g2, f2, d2, f2, g2, 0,
f2, f2, f2, 0, g2, gs2, 0, a2, c3, a2,
d3, d3, d3, a2, d3, c3, 0,
// Melody 2
a2, a2, a2, 0, a2, a2, g2, g2,
a2, a2, a2, 0, a2, g2, 0, a2, d3, 0, a2, g2,
d3, a2, g2, f2, c3, g2, f2, e2,
b1, c2, d2, 0, f2, c3, 0,
// Melody 3
0, f2,d2,f2,g2,gs2,g2,f2,d2,
gs2,g2,f2,d2,f2,g2,gs2,a2,
c3,a2,gs2,g2,f2,d2,e2,f2,g2,a2,c3,
cs3,gs2,gs2,g2,f2,g2,0,
// Melody 4
f1, g1, a1, f2, e2, d2, //6
e2, f2, g2, e2, //4
a2, 0, a2, gs2, g2, fs2, f2, e2, ds2, d2, //10
cs2, 0, ds2, 0, //4
// Bottom 1
as1, 0, f2,
e2, 0, d2, 0,
f2, 0,
M, M, M, M, M, M, M, M, M, M, //10
as1, 0, f2,
e2, 0, d2, 0,
d2, 0, d2, cs2, c2, b1, as1, a1, gs1, g1, fs1, f1, e1, ds1,
d1,0,
// Bottom 2
d1, d1, d2, a1, gs1, 0, g1, f1, d1, f1, g1, // 11
c1, c1, d2, a1, gs1, 0, g1, f1, d1, f1, g1, // 11
b0, b0, d2, a1, gs1, 0, g1, f1, d1, f1, g1, // 11
as0, g1, f2, d2, g2, f2, c2, d2, f2, a1, g1, a1, c2, // 13
// Bottom 3
d1, d1, d2, a1, gs1, 0, g1, f1, d1, f1, g1, // 11
c1, c1, d2, a1, gs1, 0, g1, f1, d1, f1, g1, // 11
d3, f3, e3, c3, 0, e3, d3, g2, a2, c3, // 10
d2, d2, f3, e3, c3, 0, e3, d3, g2, a2, c3, // 11
// Bottom 4
as1, as1, as0, as1, as1, as1, as0, as0, as0, as1, // 10 all
c2, c2, c1, c2, c2, c2, c1, c1, c1, c2,
d2, d2, d1, d2, cs2, cs2, cs1, cs1, cs1, cs2,
c2, c2, c1, c2, b1, b1, b0, b0, b0, b1,
as1, as1, as0, as1, as1, as1, as0, as0, as0, as1,
c2, c2, c1, c2, c2, c2, c1, c1, c1, c2,
d1, d1, d2, a1, gs1, 0, g1, f1, d1, f1, g1, // 10
d1, d1, d2, a1, gs1, 0, g1, f1, d1, f1, g1, // 10
d2, d2, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
c2, c2, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
b1, b1, d3, a2, gs2, 0, g2, f2, d2, f2, g2,
as1, as1, d3, a2, gs2, 0, g2, f2, d2, f2, g2,d2, 0,
};
int mega_beats[] = {
// Riff 1 (44)
2,2,4,6,2,2,4,4,2,2,2,
2,2,4,6,2,2,4,4,2,2,2,
2,2,4,6,2,2,4,4,2,2,2,
2,2,4,6,2,2,4,4,2,2,2,
// Riff 2 (44)
2,2,4,6,2,2,4,4,2,2,2,
2,2,4,6,2,2,4,4,2,2,2,
2,2,4,6,2,2,4,4,2,2,2,
2,2,4,6,2,2,4,4,2,2,2,
// Melody 1 (38)
4,2,2,2,4,4,4,10, // 8
4,2,2,2,4,4,1,1,1,2,2,2,2,4, // 14
4,2,2,2,4,2,2,4,4,6, // 10
4,4,2,2,2,12,4, // 6
// Melody 2 (36)
4,2,2,2,2,4,4,10, // 8
4,2,2,2,4,2,2,4,2,2,2,4, // 12
4,4,4,4,4,4,4,4, // 8
4,2,2,2,4,12,4, // 7
// Melody 3 (36)
16,2,2,2,2,2,2,2,2, // 9
1,1,1,1,4,18,4,2, // 8
4,2,2,2,2,2,2,4,4,4,4, // 11
4,4,2,2,2,12,4, // 7
// Melody 4 (24)
4,4,4,4,8,8, // 6
8,8,8,8, // 4
12,4,2,2,2,2,2,2,2,2, // 10
12, 4, 12, 4, // 4
// Bottom 1 (42)
20, 4, 8,
12, 4, 12, 4,
28, 4,
4, 4, 2, 4, 4, 4, 2, 2, 2, 4, // 10
20, 4, 8,
12, 4, 12, 4,
12, 4, 1,1,1,1,1,1,1,1,1,1,1,1, // 14
20, 4,
// Bottom 2 (46)
2, 2, 4, 6, 2, 2, 4, 4, 2, 2, 2, // 11
2, 2, 4, 6, 2, 2, 4, 4, 2, 2, 2, // 11
2, 2, 4, 6, 2, 2, 4, 4, 2, 2, 2, // 11
2, 2, 2, 2, 4, 2, 2, 2, 4, 4, 2, 2, 2, // 13
// Bottom 3 (43)
2, 2, 4, 6, 2, 2, 4, 4, 2, 2, 2, // 11
2, 2, 4, 6, 2, 2, 4, 4, 2, 2, 2, // 11
2, 4, 6, 2, 2, 4, 4, 2, 2, 2, // 10
2, 2, 4, 6, 2, 2, 4, 4, 2, 2, 2, // 11
// Bottom 4 (82)
4, 4, 2, 4, 4, 4, 2, 2, 2, 4, // 10
4, 4, 2, 4, 4, 4, 2, 2, 2, 4, // 10
4, 4, 2, 4, 4, 4, 2, 2, 2, 4, // 10
4, 4, 2, 4, 4, 4, 2, 2, 2, 4, // 10
4, 4, 2, 4, 4, 4, 2, 2, 2, 4, // 10
4, 4, 2, 4, 4, 4, 2, 2, 2, 4, // 10
2, 2, 4, 6, 2, 2, 4, 4, 2, 2, 2, // 11
2, 2, 4, 6, 2, 2, 4, 4, 2, 2, 2, // 11
// Riff 1 (44)
2,2,4,6,2,2,4,4,2,2,2,
2,2,4,6,2,2,4,4,2,2,2,
2,2,4,6,2,2,4,4,2,2,2,
2,2,4,6,2,2,4,4,2,2,2,16,1000,
};
int mega_tempo = 53;
int fur[] = {
e2, ds2,
e2, ds2, e2, b1, d2, c2,
a1, 0, c1, e1, a1,
b1, 0, e1, gs1, b1,
c2, 0, e1, e2, ds2,
e2, ds2, e2, b1, d2, c2,
a1, 0, c1, e1, a1,
b1, 0, e1, c2, b1,
a1,0,
};
int fur_beats [] = {
2,2,
2,2,2,2,2,2,
4,2,2,2,2,
4,2,2,2,2,
4,2,2,2,2,
2,2,2,2,2,2,
4,2,2,2,2,
4,2,2,2,2,
8,1000,
};
int fur_tempo = 120;
int tot[] = {ds2, 0, ds2, ds2, 0, ds2, ds2, 0, ds2, ds2, 0, ds2, ds2, 0, ds2,
gs2, as2, b2, as2, gs1, 0,
b2, as2, gs2, fs2,
ds2, 0, ds2, gs2, as2, b2, as2, gs1, 0,
gs2, as2, b2, as2, gs2,
ds2, 0, ds2, gs2, as2, b2, as2, gs1, 0,
b2, as2, gs2, fs2,
ds2, 0, ds2, gs2, as2, b2, as2, gs1, 0,
gs2, as2, b2, as2, gs2,
ds2, 0, ds2, gs2, as2, b2, as2, gs1, 0,
b2, as2, gs2, fs2,0,
};
int tot_beats[] = {4,2,2,4,2,2,4,2,2,4,2,2,4,2,2, //15
4,4,4,4,20,4, //6
4,4,4,4, //4
4,2,2,4,4,4,4,20,4, //9
2,4,4,4,4, //5
4,2,2,4,4,4,4,20,4, //9
4,4,4,4, //4
4,2,2,4,4,4,4,20,4, //9
2,4,4,4,4, //5
4,2,2,4,4,4,4,20,4, //9
4,4,4,4,1000, //4
};
int tot_tempo = 60;
/* the setup() method runs once, when the sketch starts */
void setup()
{
/* set buzzer pin as output */
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(PIR_MOTION_SENSOR, INPUT); /* declare the sig pin as an INPUT */
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
}
void loop()
{
// song only plays when motion is detected
if (hasMotion()){
{
int8_t NumTab[] = {13,10,11,16};//0~9,A,b,C,d,E,F
int8_t ListDisp[4];
unsigned char i = 0;
unsigned char count = 0;
delay(150);
{
i = count;
count ++;
if(count == sizeof(NumTab)) count = 0;
for(unsigned char BitSelect = 0;BitSelect < 4;BitSelect ++)
{
ListDisp[BitSelect] = NumTab[i];
i ++;
if(i == sizeof(NumTab)) i = 0;
}
tm1637.display(0,ListDisp[0]);
tm1637.display(1,ListDisp[1]);
tm1637.display(2,ListDisp[2]);
tm1637.display(3,ListDisp[3]);
delay(300);
}
}
//Loop through each note and play them to the tone of Megalovania
for(int i = 0; i < length; i++)
{
if (song == 1){
// 0 indicates a pause
if(mega[i] == 0)
{
delay(mega_beats[i] * mega_tempo);
}
else
{
playTone(mega[i], mega_beats[i] * mega_tempo);
}
delay(mega_tempo / 2); /* delay between notes */
}else if (song == 2){
if(fur[i] == 0)
{
delay(fur_beats[i] * fur_tempo);
}
else
{
playTone(fur[i], fur_beats[i] * fur_tempo);
}
delay(fur_tempo / 2); /* delay between notes */
}else if (song == 3){
if(tot[i] == 0)
{
delay(tot_beats[i] * tot_tempo);
}
else
{
playTone(tot[i], tot_beats[i] * tot_tempo);
}
delay(tot_tempo / 2); /* delay between notes */
}
}
}
}
// implements motion sensor to detect if people are around
boolean hasMotion() {
int sensor_val = digitalRead(PIR_MOTION_SENSOR); /* read sig pin */
if(HIGH == sensor_val) {
return true; /* people detected */
} else {
return false; /* people un-detected */
}
}
/* play tone */
void playTone(int tone, int duration)
{
// A diffent color LED blinks based on the octave from which the note was playing
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
if (tone > 2600){
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED, HIGH);
delayMicroseconds(tone);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED, LOW);
delayMicroseconds(tone);
}else if (tone > 1950){
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED2, HIGH);
delayMicroseconds(tone);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED2, LOW);
delayMicroseconds(tone);
}else if (tone > 1000){
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(LED, HIGH);
delayMicroseconds(tone);
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED, LOW);
delayMicroseconds(tone);
}else{
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(tone);
digitalWrite(BUZZER_PIN, LOW);
delayMicroseconds(tone);
}
}
}
Comments