Advertisement
RuiViana

Relogio_Com_Arduino

Mar 5th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include <Time.h>
  2. #include <LiquidCrystal.h>
  3. #define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
  4. #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
  5. #define TIME_HEADER 'T' // Header tag for serial time sync message
  6. //LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
  7. LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
  8.  
  9. long previousmillis=0;
  10. int hr=0;
  11. int minut=0;
  12. int dia=0;
  13. int mes=0;
  14. int ano=0;
  15. int count=0;
  16. void setup() {
  17. lcd.begin(16, 2);
  18.  
  19. pinMode(2,INPUT);
  20. pinMode(3,INPUT);
  21. pinMode(4,INPUT);
  22. pinMode(5,INPUT);
  23. pinMode(6,INPUT);
  24. pinMode(7,INPUT);
  25. Serial.begin(9600);
  26. Serial.println("Waiting for sync message");
  27. lcd.print("Waiting");
  28. lcd.setCursor(0,1);
  29. lcd.print("for sync message");
  30. setTime(0,0,0,1,1,0);
  31. }
  32. void loop(){
  33.  
  34. if(Serial.available() )
  35. {
  36. processSyncMessage();
  37. }
  38.  
  39.  
  40. if((digitalRead(7)==HIGH)) //Change button
  41. {
  42. count=count+1;
  43. Serial.println("OK");
  44. if(count>1)
  45. {
  46. count=0;
  47. }
  48. }
  49. if((digitalRead(3)==HIGH) && count==1) //Hour button
  50. {
  51. hr=3600;
  52. adjustTime(hr);
  53. }
  54. if((digitalRead(2)==HIGH) && count==1) //Minute button
  55. {
  56. minut=60;
  57. adjustTime(minut);
  58. }
  59. if((digitalRead(6)==HIGH) && count==1) //Day Button
  60. {
  61. dia++;
  62. if(dia+day()>31)
  63. {
  64. dia=1-day();
  65. }
  66. }
  67. if((digitalRead(5)==HIGH) && count==1) //Month button
  68. {
  69. mes++;
  70. if(mes+month()>12)
  71. {
  72. mes=1-month();
  73. }
  74. }
  75. if((digitalRead(4)==HIGH) && count==1) //Year Button
  76. {
  77. ano++;
  78.  
  79. }
  80.  
  81. if(timeStatus()!= timeNotSet)
  82. {
  83. digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
  84. digitalClockDisplay();
  85. }
  86. delay(1000);
  87.  
  88. }
  89. void digitalClockDisplay(){
  90. // digital clock display of the time
  91. lcd.clear();
  92. lcd.print("Hora: ");
  93. lcd.print(hour());
  94. Serial.print(hour());
  95. printDigits(minute());
  96. printDigits(second());
  97. lcd.setCursor(0,1);
  98. Serial.print(" ");
  99. Serial.print(dia+day());
  100. lcd.print("Data: ");
  101. lcd.print(dia+day());
  102. lcd.print(".");
  103. Serial.print(" ");
  104. Serial.print(mes+month());
  105. lcd.print(mes+month());
  106. lcd.print(".");
  107. Serial.print(" ");
  108. Serial.print(ano+year());
  109. lcd.print(ano+year());
  110. Serial.println();
  111. delay(500);
  112. }
  113. void printDigits(int digits){
  114. // utility function for digital clock display: prints preceding colon and leading 0
  115. Serial.print(":");
  116. lcd.print(":");
  117. if(digits < 10){
  118. Serial.print('0');
  119. lcd.print("0");
  120. }
  121. Serial.print(digits);
  122. lcd.print(digits);
  123. }
  124. void processSyncMessage() {
  125. // if time sync available from serial port, update time and return true
  126. while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
  127. char c = Serial.read() ;
  128. Serial.print(c);
  129. if( c == TIME_HEADER ) {
  130. time_t pctime = 0;
  131. for(int i=0; i < TIME_MSG_LEN -1; i++){
  132. c = Serial.read();
  133. if( c >= '0' && c <= '9'){
  134. pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
  135. }
  136. }
  137. setTime(pctime); // Sync Arduino clock to the time received on the serial port
  138. }
  139. }
  140. }
  141. time_t requestSync()
  142. {
  143. Serial.write(TIME_REQUEST);
  144. return 0; // the time will be sent later in response to serial mesg
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement