// Alarm music related code is inspired by https://www.arduino.cc/en/Tutorial/ToneMelody
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
// Notes in the melody:
int melody[] = {
NOTE_C4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C4, 0
};
// Note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4,8,8,8,8,4,4,4
};
// Define a pin for FSR
// The FSR will monitor if anyone is on the pillow, so we can track the sleeping time
int fsrPin = A0;
// Create a variable to hold the FSR reading
int fsrReading = 0;
// Create a variable to hold the last FSR reading, so we can compare the current reading to it
int lastFsrReading = 0;
// Define a pin for LED
// the LED will light up when someone is on the pillow
int ledPin = D2;
// Deine a flag that denotes whether to alarm to wake up someone
// 0 means no need to alarm, and 1 means need to alarm
int needAlarm = 0;
void setup() {
pinMode(ledPin, OUTPUT);
// Create a cloud variable so that we can monitor the change in console
Particle.variable("force", &fsrReading, INT);
// Create a cloud function so that IFTTT can call it
Particle.function("alarm", alarmPlease);
}
void loop() {
//If there is an alarm and someone is on the pillow, then play music, otherwise don't
if (needAlarm == 1 && lastFsrReading > 100) {
playMusic();
} else {
needAlarm = 0;
}
// Use analogRead to read the force
// This gives us a value from 0 to 4095
fsrReading = analogRead(fsrPin);
// Set a rough 100 threshold to detect if anyone is on the pillow
if (fsrReading > 100 && lastFsrReading <= 100) {
// Sleep starts
Particle.publish("sleepStart");
// Light up LED
analogWrite(ledPin, 255);
} else if (fsrReading <= 100 && lastFsrReading > 100) {
// Sleep ends
Particle.publish("sleepEnd");
// Turn off LED
analogWrite(ledPin, 0);
}
lastFsrReading = fsrReading;
// Wait a second and then loop
delay(1000);
}
// This function is meant to be call by IFTTT
// Users can set an alarm on IFTTT, and the applet will call this function to make the pillow play music
int alarmPlease(String command) {
// Set flag to 1 to play alarm music
needAlarm = 1;
return 1;
}
void playMusic() {
// 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(8, 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(8);
}
}
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. .