Hello everyone,
I am currently working on building an alarm clock that will open my window blinds when the alarm goes off. I have everything wired up correctly so there is no problem there, but the problem is in my code. (At least I am pretty sure it is
) I am using the time.h library and the time alarms.h library to setup the clock portion and alarm portion. As you will see in my code..I have 4 push buttons in my alarm clock. The first push button opens the blinds and the second one closes them. The third sets the alarm hours and the fourth sets the alarm minutes.
The problem I ran into is I can set the alarm in the computer, but once I try to use the buttons to set the alarm hours and minutes, it no longer works. Can anyone help?? Here is my code!
#include <Time.h> //Include Time library
#include <TimeAlarms.h> //Include Time Alarms library
#include <LiquidCrystal.h> //Include Liquid Crystal library
#include <Servo.h> //Include Servo library
Servo servoMain; // Define Servo
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); //Initialize the LCD
const int buttonPin = 9; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
int ahours = 8; //Declare variable alarm hours
int amins = 31;
int asecs = 0;
int hours = 8; //Declare time variables
int mins = 30;
int secs = 50;
void setup()
{
servoMain.attach(12); //Servo on digital pin 10
servoMain.write(0); //Turn Servo back to center position (90 degrees)
pinMode(13, INPUT); //Initialize push button 13
digitalWrite(13, HIGH);
pinMode(11, INPUT); //Initialize push button 11
digitalWrite(11, HIGH);
pinMode(10, INPUT); //Initialize push button 10
digitalWrite(10, HIGH);
pinMode(8, INPUT); //Initialize push button 8
digitalWrite(8, HIGH);
pinMode(9, INPUT); //Initialize push button A0
digitalWrite(9, HIGH);
setTime(hours,mins,secs,8,8,15); //Set time to Saturday 8:29:00am Jan 1 2011
lcd.begin(16,2); //Setup LCD Screen
lcd.clear(); //Lcd clear
lcd.setCursor(0,0); //Lcd cursor (0,0)
Alarm.alarmRepeat(ahours,amins,asecs, MorningAlarm);// 8:30am every day
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("Time:"); //Lcd print "Time:"
lcd.setCursor(10,0); //Lcd set cursor (10,0)
lcd.print("Alarm:"); //Lcd print "Alarm:"
digitalClockDisplay(); //Digital clocl display
Alarm.delay(1000); //wait one second between clock display
if(digitalRead(11) == LOW) //Push button to change alarm hours
{
ahours++; //Add 1 to Alarm hours
delay(100); //Delay
}
if(digitalRead(13) == LOW) //Push button to change alarm mimutes
{
amins++; //Add 1 to alarm minutes
delay(100);
}
lcd.setCursor(10,1); //Lcd set cursor(12,1)
if(ahours < 10)
{
lcd.print("0"); //Lcd print "0"
lcd.print(ahours); //Lcd print alarm hours
}
else
{
lcd.print(ahours); //Lcd print alarm hours
}
lcd.print(":"); //Lcd print ":"
if (amins < 10)
{
lcd.print("0"); //Lcd Print "0"
lcd.print(amins); //Lcd print alarm minutes
}
else
{
lcd.print(amins); //Lcd print alarm minutes
}
if(amins > 59)
{
ahours++; //Alarm hours
amins = 0; //Alarm minutes = 0
}
if(ahours > 23)
{
ahours = 0; //Alarm hours = 0
}
}
void MorningAlarm() //Morning Alarm
{
servoMain.write(160); // Turn Servo back to center position
}
void digitalClockDisplay()
{
lcd.setCursor(0,1); //Lcd set cursor (0,1)
lcd.print(hour()); // digital clock display of the time
printDigits(minute()); //^
printDigits(second()); //^
}
void printDigits(int digits)
{
lcd.print(":"); //Lcd print ":"
if(digits < 10)
lcd.print('0'); //Lcd print "0"
lcd.print(digits); //Lcd print "digits"
if (digitalRead(8) == LOW) //Push button to move servo to close position
{
servoMain.write(0); //Turn Servo back to close
}
if (digitalRead(10) == LOW) //Push button to move servo to open position
{
servoMain.write(160); //Turn Servo back to open position
}
}