int speakerPin = D2; //Speaker as OUTPUT at D2
int inputPin = D0; // PIR sensor = INPUT at D0
int ledPin = D1; // LED at D1
int val = 0; // inputPin status
int count = 0; // Counter Variable
// create an array for the notes in the melody:
//C4,G3,G3,A3,G3,0,B3,C4
int melody[] = {1908,2551,2551,2273,2551,0,2024,1908};
// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,8,8,4,4,4,4,4 };
void setup() {
pinMode( speakerPin, OUTPUT ); // Speaker as OUTPUT
pinMode( ledPin, OUTPUT ); // LED as OUTPUT
pinMode(inputPin, INPUT); // Motion detection with PIR as INPUT
}
void loop() {
val = digitalRead(inputPin);
if (val == HIGH) {
digitalWrite(ledPin, HIGH);
delay(1000);
count++;
if (count == 10) { // if door has been opened for 5 times
count = 0;
playNotes();
Particle.publish("Close the door"); // Send roommate a SMS alerting him about his snacking habits
}
}
digitalWrite(ledPin, LOW);
delay(1000);
}
void playNotes()
{
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(speakerPin, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(speakerPin);
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .