safwan092

Untitled

Oct 30th, 2018
168
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2.  
  3. #define HOUR_BUTTON 6
  4. #define MINUTE_BUTTON 7
  5. #define TICK_LENGTH 60000
  6. #define CHECK_TIME 250
  7.  
  8.  
  9. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  10.  
  11. unsigned int minute = 0;
  12. unsigned int hour = 01;
  13. unsigned long previousMillis = 0;
  14. unsigned char am = 1;
  15.  
  16. void setup() {
  17. // put your setup code here, to run once:
  18. lcd.begin(16, 2);
  19. pinMode(HOUR_BUTTON, INPUT);
  20. pinMode(MINUTE_BUTTON, INPUT);
  21. }
  22.  
  23. void loop() {
  24. // put your main code here, to run repeatedly:
  25. checkTick();
  26. lcd.setCursor(0,0);
  27. lcd.print("Time:");
  28. lcd.setCursor(6,1);
  29. lcd.print(hour);
  30. lcd.print(":");
  31. lcd.print(minute);
  32. if(am == 1) lcd.print(" AM");
  33. else lcd.print(" PM");
  34. }
  35.  
  36. void checkTick() {
  37. unsigned long currentMillis = millis();
  38.  
  39. if ((unsigned long)(currentMillis - previousMillis) >= TICK_LENGTH) {
  40. tick();
  41. previousMillis = currentMillis;
  42. }
  43. if((unsigned long)(currentMillis - previousMillis) >= CHECK_TIME){
  44. readHourButton();
  45. readMinuteButton();
  46. }
  47. }
  48.  
  49. void tick() {
  50. minuteUp();
  51. }
  52.  
  53.  
  54. void readHourButton() {
  55. if (digitalRead(HOUR_BUTTON) == HIGH) {
  56. delay(200);
  57. hourUp();
  58. }
  59. }
  60.  
  61. void readMinuteButton() {
  62. if (digitalRead(MINUTE_BUTTON) == HIGH) {
  63. delay(200);
  64. minuteUp();
  65. }
  66. }
  67.  
  68. void hourUp() {
  69. hour = hour + 1;
  70. if (hour > 12) {
  71. lcd.clear();
  72. hour = 1;
  73. if(am == 1) am = 0;
  74. else am = 1;
  75. }
  76. }
  77.  
  78. void minuteUp() {
  79. minute = minute + 1;
  80. if (minute > 59) {
  81. minute = 0;
  82. hourUp();
  83. }
  84. }
RAW Paste Data