Advertisement
Guest User

Water Splash Alarm Clock

a guest
Apr 16th, 2015
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include<Keypad.h>
  3. #include<Time.h>
  4. #include <Servo.h>
  5.  
  6. Servo myservo; //creates servo object to control servo
  7. LiquidCrystal lcd(8, 9, 10, 11, 12, 13); //set up pins to lcd
  8. const byte ROWS = 4; //four rows on keypad
  9. const byte COLS = 4; //three columns on keypad
  10. char keys[ROWS][COLS] = {
  11. {'1','2','3','A'},
  12. {'4','5','6','B'},
  13. {'7','8','9','C'},
  14. {'*','0','#','D'}
  15. };
  16. byte rowPins[ROWS] = {30, 32, 34, 36}; //connect to the row pinouts of the keypad
  17. byte colPins[COLS] = {26, 24, 22, 38}; //connect to the column pinouts of the keypad
  18. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  19. int place = 0;
  20. int hours = 99;//set hours to 99 so we know if it has been altered
  21. int minutes = 99;//set minutes to 99 so we know if it has been altered
  22. boolean keeplooping = true;
  23. int alarmhour = 99;//set to 99 so we know if it has been altered
  24. int alarmminute = 99;//set to 99 se we know if it has been altered
  25. int currenthour;
  26. int currentminute;
  27.  
  28.  
  29.  
  30.  
  31. void setup(){
  32. myservo.attach(51); // attaches the servo on pin 51 to the servo object
  33. myservo.write(0);//make sure servo starts at 0 degrees
  34. lcd.begin(16,2);// set up the LCD's number of columns and rows
  35. lcd.print("Current Time");//prompt user for current timne
  36. lcd.setCursor(0, 1);//set to print on row 1(second row)
  37. lcd.print("hours # minutes");//tell user the input format
  38. enter_time(); //have user enter current time
  39. setTime(hours, minutes, 30, 13, 4, 2015);//set time to user defined time, using the date I created this program as the days months and years
  40. lcd.clear();
  41. lcd.print("Enter alarm time");
  42. enter_alarm_hour(); //have user set alarm time
  43. enter_alarm_minute();
  44. }
  45. void loop (){
  46. delay(10000);//check time every ten seconds
  47. currenthour = hour();//get hour
  48. currentminute = minute();//get minute
  49. lcd.clear();//clear screen
  50. lcd.print(currenthour);//print hour:minute
  51. lcd.print(":");
  52. if(currentminute<10){lcd.print(0);}//make it say 11:01 instead of 11:1
  53. lcd.print(currentminute);
  54. lcd.setCursor(5, 1);//set cursor to partway through second row to print alarm time
  55. lcd.print("Alarm");//printAlarm hour:minute
  56. lcd.print(alarmhour);
  57. lcd.print(":");
  58. if(alarmminute<10){lcd.print(0);}//print 11:01 not 11:1
  59. lcd.print(alarmminute);
  60. if(currenthour==alarmhour&&currentminute==alarmminute){//if it is time for the alarm, perform the wake up function
  61. wakeup();
  62. }
  63. }
  64.  
  65. void enter_time(){ // has user set current time
  66. lcd.setCursor(place, 1);
  67. while(keeplooping){
  68. char key = keypad.getKey();
  69. if(key){
  70. if(hours==99){lcd.clear();}//if this is the first input, clear the screen of the prompt
  71. if(key!=65&&key!=66&&key!=67&&key!=68&&key!=42&&key!=35){//make sure key is a number
  72. lcd.print(key);
  73. place++;//go to next space on lcd
  74. int input = key - '0';//get value as int
  75. if(hours == 99){hours = input;//set first digit if we haven't altered it yet
  76. }else{hours = ((10*hours)+input);}//or set second digit if we have
  77. }
  78. if(key==35){//done receiving inputs
  79. lcd.print(":");
  80. place++;
  81. keeplooping = false;//stop the input loop
  82. }
  83. }
  84. }
  85.  
  86. keeplooping = true;
  87. while(keeplooping){
  88. char key = keypad.getKey();
  89. if(key){
  90. if(key!=65&&key!=66&&key!=67&&key!=68&&key!=42&&key!=35){//make sure key is a number
  91. lcd.print(key);
  92. place++;//go to next spot on lcd
  93. int input = key - '0';//get the input as an int
  94. if(minutes == 99){minutes = input;//set first digit of minutes if we haven't altered it yet
  95. }else{
  96. minutes = ((minutes*10)+input);//or set second digit if we have
  97. keeplooping = false;//leave the input loop
  98. }
  99. }
  100. }
  101. }
  102. }
  103. void enter_alarm_hour(){ //has user set alarm's hour
  104. lcd.setCursor(0, 1);
  105. keeplooping = true;
  106. while(keeplooping){
  107. char key = keypad.getKey();
  108. if(key){
  109. if(key!=65&&key!=66&&key!=67&&key!=68&&key!=42&&key!=35){//make sure key is a number
  110. lcd.print(key);
  111. place++;//go to next spot on lcd
  112. int input = key - '0';//get the input as an int
  113. if(alarmhour == 99){alarmhour = input;//set first digit if we haven't altered it yet
  114. }else{alarmhour = ((10*alarmhour)+input);}//or set second digit if we have
  115. }
  116. if(key==35){//done receiving inputs
  117. lcd.print(":");
  118. place++;
  119. keeplooping = false;//stop the input loop
  120. }
  121. }
  122. }
  123. }
  124. void enter_alarm_minute(){ // has user enter alarm's minute
  125. keeplooping = true;
  126. while(keeplooping){
  127. char key = keypad.getKey();
  128. if(key){
  129. if(key!=65&&key!=66&&key!=67&&key!=68&&key!=42&&key!=35){//make sure key is a number
  130. lcd.print(key);
  131. place++;//go to next spot on lcd
  132. int input = key - '0';//get the input as an int
  133. if(alarmminute == 99){alarmminute = input;//set first digit of minutes if we haven't altered it yet
  134. }else{
  135. alarmminute = ((alarmminute*10)+input);//or set second digit if we have
  136. keeplooping = false;//leave the input loop
  137. }
  138. }
  139. }
  140. }
  141. }
  142. void wakeup(){ //dump a cup of water(attached to the servo)
  143. for(int pos=0; pos<110; pos+=1){ //go from 0 degrees to 110 degrees
  144. myservo.write(pos); //put the servo at the new position
  145. delay(15); //give it 15 ms to reach new position
  146. }
  147. delay(5000); // give it 5 seconds for all of the water to drain out
  148. for(int pos=110; pos>0; pos-=1){ //go from 110 degrees back to 0 degrees
  149. myservo.write(pos); //put the servo at the new position
  150. delay(15); //give it 15 ms to reach new position
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement