// Creative Project 1: Home Hack, Kedi Zhang, 30 Jan 2018
int speakerPin = D0;
int photoCellPin = A1;
int photoCellReading = 0;
int count = 0;
String body = "Close your fridge door!!!";
// create an array for the notes in the melody:
//C4,G3,G3,A3,G3,0,B3,C4
int melody[] = {0,2637,2349,1480,1661,2217,1976,1175,1319,1976,1760,1109,1319,1760};
// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,8,8,4,4,8,8,4,4,8,8,4,4,2};
void playNotes()
{
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 14; 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);
}
}
void setup(){
// Set up the LED for output
pinMode(speakerPin, OUTPUT);
// Create a cloud variable of type integer
// called 'light' mapped to photoCellReading
Particle.variable("light", &photoCellReading, INT);
Serial.begin(9600);
}
void loop() {
photoCellReading = analogRead(photoCellPin);
Serial.print("Analog reading = ");
Serial.print(photoCellReading); // the raw analog reading
// We'll have a few threshholds, qualitatively determined
if (photoCellReading < 3250) {
Serial.println(" - Dark");
delay(100);
count = 0;
} else {
Serial.println(" - Bright");
if (count == 0) {
delay(3000);
if (photoCellReading > 3249) {
count++;
}
} else if (count < 4) {
playNotes();
count++;
} else if (count == 4) {
count++;
Particle.publish("twilio_sms", body, PRIVATE);
} else {
delay(60000);
count = 0;
}
}
}
Click to Expand