Code that will play one of two christmas melodies through a speaker when motion is detected.
#include "pitches.h"
const int BEEPER_PIN = 13;
const int INPUT_PIN = 2;
int timeCounter = 0;
int playNum = 0;
void setup() {
randomSeed(42);
Serial.begin(9600);
pinMode(BEEPER_PIN, OUTPUT);
pinMode(INPUT_PIN, INPUT);
}
void loop() {
int value = digitalRead(INPUT_PIN);
if (value == HIGH) {
//Serial.print(random(1,5000)); Serial.println(" - eina");
Serial.println(playNum);
switch (playNum) {
case 0: jingleBell(); break;
case 1: weWishYouMerryChristmas(); break;
}
playNum += 1;
if (playNum >= 2) {
playNum = 0;
}
noTone(BEEPER_PIN);
delay(5000);
} else {
noTone(BEEPER_PIN);
}
}
void jingleBell() {
int melody[] = {
NG4,NE5,ND5,NC5,NG4,NG4,NE5,ND5,NC5,NA4,
NA4,NF5,NE5,ND5,NB4,NG5,NG5,NF5,ND5,NE5,
NG4,NE5,ND5,NC5,NG4,NG4,NE5,ND5,NC5,NA4,
NA4,NF5,NE5,ND5,NG5,NG5,NG5,NG5,NG5,NA5,NG5,NF5,ND5,NC5,NG5,
NE5,NE5,NE5,NE5,NE5,NE5,NE5,NG5,NC5,ND5,NE5,
NF5,NF5,NF5,NF5,NF5,NF5,NE5,NE5,NE5,NE5,NE5,ND5,ND5,NE5,ND5,NG5,
NE5,NE5,NE5,NE5,NE5,NE5,NE5,NG5,NC5,ND5,NE5,
NF5,NF5,NF5,NF5,NF5,NF5,NE5,NE5,NE5,NE5,NG5,NG5,NF5,ND5,NC5,
};
int noteDurations[] = {
8,8,8,8,2,8,8,8,8,2, // 10
8,8,8,8,2,8,8,8,8,2, // 10
8,8,8,8,2,8,8,8,8,2, // 10
8,8,8,8,8,8,8,16,16,8,8,8,8,4,4, // 15
8,8,4,8,8,4,8,8,8,8,2, // 11
8,8,8,16,16,8,8,8,16,16,8,8,8,8,4,4, // 16
8,8,4,8,8,4,8,8,8,8,2, // 11
8,8,8,16,16,8,8,8,16,16,8,8,8,8,2, // 15
};
//time_t tStart = now();
for (int thisNote = 0; thisNote < 98; thisNote++) {
int noteDuration = 1300/noteDurations[thisNote];
timeCounter += noteDuration;
tone(BEEPER_PIN, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
timeCounter += pauseBetweenNotes;
delay(pauseBetweenNotes);
noTone(BEEPER_PIN);
int value = digitalRead(INPUT_PIN);
//int tNow = second(now()) - second(tStart);
//if (tNow >= 5) {
//break;
//}
if ((timeCounter >= 5000) && (value == LOW)) {
timeCounter = 0;
break;
}
}
}
void weWishYouMerryChristmas() {
int melody[] = {
NG4,NC5,NC5,ND5,NC5,NB4,NA4,NA4,
NA4,ND5,ND5,NE5,ND5,NC5,NB4,NG4,
NG4,NE5,NE5,NF5,NE5,ND5,NC5,NA5,NG4,NG4,NA4,ND5,NB4,NC5,
NG4,NC5,NC5,ND5,NC5,NB4,NA4,NA4,
NA4,ND5,ND5,NE5,ND5,NC5,NB4,NG4,
NG4,NE5,NE5,NF5,NE5,ND5,NC5,NA5,NG4,NG4,NA4,ND5,NB4,NC5,
NG4,NC5,NC5,NC5,NB4,NB4,NC5,NB4,NA4,NG4,
ND5,NE5,ND5,ND5,NC5,NC5,NG5,NG4,NG4,NG4,NA4,ND5,NB4,NC5
};
int noteDurations[] = {
4,4,8,8,8,8,4,4,
4,4,8,8,8,8,4,4,
4,4,8,8,8,8,4,4,8,8,4,4,4,2,
4,4,8,8,8,8,4,4,
4,4,8,8,8,8,4,4,
4,4,8,8,8,8,4,4,8,8,4,4,4,2,
4,4,4,4,2,4,4,4,4,2,
4,4,8,8,8,8,4,4,8,8,4,4,4,2,
};
for (int thisNote = 0; thisNote < 98; thisNote++) {
int noteDuration = 1300/noteDurations[thisNote];
timeCounter += noteDuration;
tone(BEEPER_PIN, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
timeCounter += pauseBetweenNotes;
delay(pauseBetweenNotes);
noTone(BEEPER_PIN);
int value = digitalRead(INPUT_PIN);
if ((timeCounter >= 5000) && (value == LOW)) {
timeCounter = 0;
break;
}
}
}
You will also need this pitches.h file:
/*************************************************
* Public Constants
*************************************************/
#define NB0 31
#define NC1 33
#define NCS1 35
#define ND1 37
#define NDS1 39
#define NE1 41
#define NF1 44
#define NFS1 46
#define NG1 49
#define NGS1 52
#define NA1 55
#define NAS1 58
#define NB1 62
#define NC2 65
#define NCS2 69
#define ND2 73
#define NDS2 78
#define NE2 82
#define NF2 87
#define NFS2 93
#define NG2 98
#define NGS2 104
#define NA2 110
#define NAS2 117
#define NB2 123
#define NC3 131
#define NCS3 139
#define ND3 147
#define NDS3 156
#define NE3 165
#define NF3 175
#define NFS3 185
#define NG3 196
#define NGS3 208
#define NA3 220
#define NAS3 233
#define NB3 247
#define NC4 262
#define NCS4 277
#define ND4 294
#define NDS4 311
#define NE4 330
#define NF4 349
#define NFS4 370
#define NG4 392
#define NGS4 415
#define NA4 440
#define NAS4 466
#define NB4 494
#define NC5 523
#define NCS5 554
#define ND5 587
#define NDS5 622
#define NE5 659
#define NF5 698
#define NFS5 740
#define NG5 784
#define NGS5 831
#define NA5 880
#define NAS5 932
#define NB5 988
#define NC6 1047
#define NCS6 1109
#define ND6 1175
#define NDS6 1245
#define NE6 1319
#define NF6 1397
#define NFS6 1480
#define NG6 1568
#define NGS6 1661
#define NA6 1760
#define NAS6 1865
#define NB6 1976
#define NC7 2093
#define NCS7 2217
#define ND7 2349
#define NDS7 2489
#define NE7 2637
#define NF7 2794
#define NFS7 2960
#define NG7 3136
#define NGS7 3322
#define NA7 3520
#define NAS7 3729
#define NB7 3951
#define NC8 4186
#define NCS8 4435
#define ND8 4699
#define NDS8 4978