EvilZone
Programming and Scripting => Assembly - Embedded => Topic started by: Kulverstukas on December 09, 2015, 09:01:17 AM
-
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
-
Hi, correct me if I am wrong,
I can see from the code, that the song is played as soon as button is pressed (or you have HI at input pin). And then, no matter pin's voltage HI or LO the complete song will be played. Press again and the next one, after the previous one is complete.
As an option, it would be more user responsive if instead of this kind of voltage poll at a deticated pin, to implement interrept based approach. It will simulate then the user swith between the songs. It is easily done in Arduino.
Funny, little project :)
-
Awesome, and how do you detect motion? I mean, how does the hardware realization look like?
I like that library, I played with it recently to make a sound step sequencer, haven't finished coding it yet, still have the phototransistor unused. Here it is: https://youtu.be/d_obeP2-EoM
-
This is adorable, and will drive my poor husband nuts. Thank you! Cookie time~
-
<...>
Funny, little project :)
how does hardware work derp
There is a motion sensor attached to the Arduino, the same you see in every tutorial. It is connected to GND, 5V and pin 2 for data input. Piezzo buzzer is connected to GND and pin 13 for output. Later I connected a 1kohm resistor to the buzzer to control the volume and 1kohm was perfect. All connected through a UTP cable :P
Software is simple, at first I made it select a song randomly, but there were no point since there is only 2 melodies, so I made them play one after another. When the first melody ends, script pauses for 5 seconds before starting to detect motion again. When motion is detected, the melody is being played while there is motion, I did that by counting the notes and pauses between the notes, when the amount is over 5000 (5 seconds?) and there is no motion, the music then stops.
I wanted to make it use the time lib to exactly count 5 seconds, but somehow it didn't compile for me, so whatever.
-
You should send it in to hackaday. You never know, they might feature it. For the audio, you should look into midi.
I seen the PIR sensor on it, I've used them before and it looks like the one you got is different than the ones I had worked with. They are simple enough though.
The code I noticed could use some work, but if it works, that's the only thing that matters. I'm just glad people are getting more into hardware stuff. Next, after your comfortable with Arduino and it's bootloader, try out bare metal C. You can program the arduino with C and use the PORTS as you could with plain AVRs.
Great project, again send a tip to HackADay.
-
Not bad for a little fun.
This:
playNum += 1;
if (playNum >= 2) {
playNum = 0;
}
Can be cleanly re-written as this too:
playNum ^= 1;