Lab 6: Vibrating Alarm Clock

dheinzerling's picture

Description

  • For this lab, I altered my alarm clock code of last week to include a vibrating element when the alarm goes off. The user is presented with a potentiometer and an FSR. 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 pressing the FSR. 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, the motor spins a wobbly cork that causes vibration, 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. The user can press the FSR when the alarm is going off to run off the alarm.

Components Used

  • Resistors
  • Potentiometer
  • FSR
  • LED
  • Motor
  • Cork
  • Diode
  • Piezo buzzer

Arduino Code

//Program to emulate an alarm clock

//David Heinzerling
//10.11.2011
 
#include <Time.h>
#include <TimeAlarms.h>
 
 
int potIn = 0;    // Potentiometers connected to analog pins 0
int speakerOut = 7;
int ledOut = 8;   // LEDs connected to digital pins 9, 10 and 11
int fsrIn = 1;
int motorOut = 9;
 
//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 fsrVal = 0;
 
void setup()
{
  pinMode(ledOut, OUTPUT);   // sets the digital pins as output
  pinMode(speakerOut, OUTPUT);
  Serial.begin(9600);       // use the serial port
  setTime(6,59,50,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
  // create the alarms 
}
 
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 (fsrVal < 500){
  //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;
  fsrVal = analogRead(fsrIn);
  if (fsrVal > 10) {
    Alarm.alarmOnce(hr,min,00, MorningAlarm);  // set alarm
    Serial.print("Alarm set!");
    fsrVal = 1000;
    break;
  }
  Serial.print(fsrVal);
  Serial.print("   ");
  digitalClockDisplay(hr, min, 0);
  Alarm.delay(500); // wait one second between clock display
  }
}
 
void MorningAlarm(){
  fsrVal = 0;
  Serial.println("Alarm!!!!!!!!");
  digitalWrite(speakerOut, LOW);  
  analogWrite(motorOut, 100);
while (fsrVal < 10) {
 
      for (count = 0; count < MAX_COUNT; count++) {
        fsrVal = analogRead(fsrIn);
    Serial.print(fsrVal);  
    if (fsrVal > 10) {
       analogWrite(motorOut, 0); 
       fsrVal = 500;
       break;
    }
        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);
}
 
 

 

 

photo.JPG
photo (2).JPG
photo (1).JPG
0
Your rating: None