Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Time.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS 12 // ds18b20 pin 12
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(22, 24, 31, 30, 33, 32); // выводы lcd 4 6 11 12 13 14
- int d, m, w, d1, y1, dmax, m1, a = 1;
- int y = 2011; // год
- char* weekd[]= {"Sun", "Mon","Tue","Wed","Thu","Fri","Sat"};
- char* mon[]={"Jan", "Feb" ,"Mar","Apr","May","Jun","Jul","Aug",
- "Sep","Oct","Nov","Dec"};
- #define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
- #define TIME_HEADER 'T' // Header tag for serial time sync message
- #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
- void setup() {
- Serial.begin(9600);
- lcd.begin(20, 2);
- setSyncProvider( requestSync); //set function to call when sync required
- Serial.println("Waiting for sync message");
- lcd.print("Waiting for sync ");
- sensors.begin();
- }
- void loop(){
- if(Serial.available() )
- {
- processSyncMessage();
- }
- if(timeStatus()!= timeNotSet)
- {
- digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
- digitalClockDisplay();
- }
- delay(1000);
- }
- void digitalClockDisplay(){
- // digital clock display of the time
- lcd.clear();
- lcd.setCursor(6,0);
- lcd.print(hour());
- printDigits(minute());
- printDigits(second());
- lcd.setCursor(2,1);
- lcd.print(day());
- lcd.print(" " );
- m = month();
- a = m - 1;
- w = weekday();
- d1 = w - 1;
- lcd.print(mon[a]);
- lcd.print(" ");
- lcd.print(weekd[d1]) ;
- lcd.print(" ");
- lcd.print("T: ");
- sensors.requestTemperatures();
- lcd.print(sensors.getTempCByIndex(0));
- }
- void printDigits(int digits){
- // utility function for digital clock display: prints preceding colon and leading 0
- lcd.print(":");
- if(digits < 10)
- lcd.print('0');
- lcd.print(digits);
- }
- void processSyncMessage() {
- // if time sync available from serial port, update time and return true
- while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
- char c = Serial.read() ;
- Serial.print(c);
- if( c == TIME_HEADER ) {
- time_t pctime = 0;
- for(int i=0; i < TIME_MSG_LEN -1; i++){
- c = Serial.read();
- if( c >= '0' && c <= '9'){
- pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
- }
- }
- setTime(pctime); // Sync Arduino clock to the time received on the serial port
- }
- }
- }
- time_t requestSync()
- {
- //Serial.print(TIME_REQUEST,byte);
- return 0; // the time will be sent later in response to serial mesg
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement