Alarm Clock

dheinzerling's picture

 

Description

 

  • For this lab, I created an alarm clock. The user is presented with a potentiometer and a light sensor. When the program is run and the serial port monitor is started, the user is provided a running list of values of the position of the potentiometer converted to a time in 24hr format. The user can adjust the pot to set the alarm to the desired time. When the pot is set, the user can activate the alarm by covering the photocell with their finger. After the alarm is set, the current time is updated as a running value in the serial monitor so that the user can monitor what time it is and how close the alarm is. When the alarm goes off at the specified time, an LED light flashes and a tune plays on the piezo buzzer. The code requires using the time and timeAlarm arduino libraries, which allow for easy alarm setting and time setting.

 

Components Used

  • Resistors
  • Potentiometer
  • Photocell
  • LED

Arduino Code

 

//Program to emulate an alarm clock
//David Heinzerling
//10.04.2010
 
#include <Time.h>
#include <TimeAlarms.h>
 
 
int potIn = 0;    // Potentiometers connected to analog pins 0
int speakerOut = 7;
int ledOut = 9;   // LEDs connected to digital pins 9, 10 and 11
int photoIn = 1;
 
//variable setup
int potVal = 0;   // Variables to store the input from the potentiometers
int potVal2 = 0;
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = HIGH;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
int photoVal = 500;
 
void setup()
{
  pinMode(ledOut, OUTPUT);   // sets the digital pins as output
  pinMode(speakerOut, OUTPUT);
  Serial.begin(9600);       // use the serial port
  setTime(9,9,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
}
 
void loop()
{
 AlarmSet();
 CurrentTime();
}
 
void CurrentTime() {
  int(hrc) = hour();
  int(minc) = minute();
  int(secc) = second();
  digitalClockDisplay(hrc, minc, secc);
  Alarm.delay(2000);
}
 
void AlarmSet() {
   while (photoVal > 5){
  //potVal = analogRead(potIn);
  potVal = analogRead(potIn);  // read input pins, convert to 0-288 scale which is 5 minute intervals for 24 hour period 
  potVal2 = potVal/3.552083333; // scale time to 5 minute intervals
  int(hr) = potVal2/12;
  int(min) = (potVal2 % 12)*5;
  photoVal = analogRead(photoIn);
  if (photoVal < 150) {
    Alarm.alarmOnce(hr,min,00, MorningAlarm);  // set alarm
    Serial.print("Alarm set!");
    photoVal = 0;
    break;
  }
  Serial.print(photoVal);
  Serial.print("   ");
  digitalClockDisplay(hr, min, 0);
  Alarm.delay(2000); // wait one second between clock display
  }
}
 
void MorningAlarm(){
  Serial.println("Alarm!!!!!!!!");
  digitalWrite(speakerOut, LOW);     
      for (count = 0; count < MAX_COUNT; count++) {
          digitalWrite(ledOut, HIGH);
          delay(500);
          digitalWrite(ledOut, LOW);
        for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
          for (count2=0;count2<8;count2++) {
            if (names[count2] == melody[count*2 + 1]) {       
              digitalWrite(speakerOut,HIGH);
              delayMicroseconds(tones[count2]);
              digitalWrite(speakerOut, LOW);
              delayMicroseconds(tones[count2]);
            } 
            if (melody[count*2 + 1] == 'p') {
              // make a pause of a certain size
              digitalWrite(speakerOut, 0);
              delayMicroseconds(500);
            }
          }
        }
      }  
}
 
void digitalClockDisplay(int hour, int minutes, int seconds)
{
  // digital clock display of the time
  Serial.print(hour);
  printDigits(minutes);
  printDigits(seconds);
  Serial.println(); 
}
 
void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

 

0
Your rating: None