Advertisement
Guest User

Untitled

a guest
Feb 10th, 2012
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #include <Time.h>
  2. #include <OneWire.h>
  3. #include <DallasTemperature.h>
  4.  
  5.  
  6. #define ONE_WIRE_BUS 12 // ds18b20 pin 12
  7.  
  8.  
  9. OneWire oneWire(ONE_WIRE_BUS);
  10. DallasTemperature sensors(&oneWire);
  11. #include <LiquidCrystal.h>
  12. LiquidCrystal lcd(22, 24, 31, 30, 33, 32); // выводы lcd 4 6 11 12 13 14
  13. int d, m, w, d1, y1, dmax, m1, a = 1;
  14. int y = 2011; // год
  15. char* weekd[]= {"Sun", "Mon","Tue","Wed","Thu","Fri","Sat"};
  16.  
  17. char* mon[]={"Jan", "Feb" ,"Mar","Apr","May","Jun","Jul","Aug",
  18. "Sep","Oct","Nov","Dec"};
  19. #define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
  20. #define TIME_HEADER 'T' // Header tag for serial time sync message
  21. #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
  22.  
  23. void setup() {
  24. Serial.begin(9600);
  25. lcd.begin(20, 2);
  26. setSyncProvider( requestSync); //set function to call when sync required
  27. Serial.println("Waiting for sync message");
  28. lcd.print("Waiting for sync ");
  29. sensors.begin();
  30. }
  31.  
  32. void loop(){
  33. if(Serial.available() )
  34. {
  35. processSyncMessage();
  36. }
  37. if(timeStatus()!= timeNotSet)
  38. {
  39. digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
  40. digitalClockDisplay();
  41. }
  42. delay(1000);
  43.  
  44. }
  45. void digitalClockDisplay(){
  46. // digital clock display of the time
  47. lcd.clear();
  48. lcd.setCursor(6,0);
  49.  
  50.  
  51. lcd.print(hour());
  52.  
  53. printDigits(minute());
  54. printDigits(second());
  55. lcd.setCursor(2,1);
  56. lcd.print(day());
  57. lcd.print(" " );
  58. m = month();
  59. a = m - 1;
  60. w = weekday();
  61. d1 = w - 1;
  62. lcd.print(mon[a]);
  63. lcd.print(" ");
  64.  
  65. lcd.print(weekd[d1]) ;
  66.  
  67. lcd.print(" ");
  68.  
  69. lcd.print("T: ");
  70. sensors.requestTemperatures();
  71. lcd.print(sensors.getTempCByIndex(0));
  72.  
  73. }
  74.  
  75.  
  76. void printDigits(int digits){
  77. // utility function for digital clock display: prints preceding colon and leading 0
  78. lcd.print(":");
  79. if(digits < 10)
  80. lcd.print('0');
  81. lcd.print(digits);
  82. }
  83.  
  84. void processSyncMessage() {
  85. // if time sync available from serial port, update time and return true
  86. while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
  87. char c = Serial.read() ;
  88. Serial.print(c);
  89. if( c == TIME_HEADER ) {
  90. time_t pctime = 0;
  91. for(int i=0; i < TIME_MSG_LEN -1; i++){
  92. c = Serial.read();
  93. if( c >= '0' && c <= '9'){
  94. pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
  95. }
  96. }
  97. setTime(pctime); // Sync Arduino clock to the time received on the serial port
  98. }
  99. }
  100. }
  101.  
  102. time_t requestSync()
  103. {
  104. //Serial.print(TIME_REQUEST,byte);
  105. return 0; // the time will be sent later in response to serial mesg
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement