Photosensing Alarm Clock

Description

I wanted to make an alarm clock for people whose activities depend not on the 24 hr clock we use but the sunlight outside. A good example of this could be farmers who schedule taking the animals out, watering crops etc based on how sunny it is. My clock uses a photo sensor and can be programmed to play different alarms depending on the brightness outside. Pressing the off button lightly snoozes the alarm while presses it hard switches off the alarm altogether.

Component

  • Arduino
  • 1 photo sensor
  • 1 FSR
  • 2 10k ohm resistors
  • 1 piezo speaker
  • Bread board
  • Jumper cables

Arduino Code

#include "pitches.h"

int fsrPin = A0;
int pcPin = A1;
int speakerPin = 8;

int fsrVal;
int pcVal;

// alarm 1
int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};

// alarm 2
int melody2[] = {
NOTE_GS4, NOTE_GS4, NOTE_GS3, NOTE_GS3, NOTE_DS4, NOTE_DS4, NOTE_GS3, NOTE_GS3, NOTE_GS4, NOTE_GS4, NOTE_E4, NOTE_B4, NOTE_FS4, NOTE_FS4, NOTE_FS3, NOTE_FS3};

void setup() {
pinMode(fsrPin, INPUT);
pinMode(pcPin, INPUT);
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
}

void loop() {

fsrVal = analogRead(fsrPin);
pcVal = analogRead(pcPin);

Serial.print(“FSR Value: “);
Serial.println(fsrVal);
Serial.print(“PC Value: “);
Serial.println(pcVal);

// Afternoon alarm
if (pcVal > 200 && pcVal < 500 && fsrVal < 100 ) {
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); } } // Dusk alarm if (pcVal > 500 && fsrVal < 100 ) {
for (int thisNote2 = 0; thisNote2 < 16; thisNote2++) { tone(speakerPin, melody2[thisNote2]); delay(200); noTone(speakerPin); } } // Snooze. Have to wait a while for FSR to go back to 0 if (fsrVal > 30 && fsrVal < 800) { delay(800); } // Stop alarm if (fsrVal > 800) {
exit(0);
}

}