arduino에서 바로 시간 정보를 사용하려고 하면 다 짜야 한다.
그러나, arduino는 웹을 통해서 쉽고 사용할 수 있는 Time Library 파일을 제공하고 있다.

바로 Time Library이다.
http://www.arduino.cc/playground/Code/Time


http://www.arduino.cc/playground/uploads/Code/Time.zip 를 다운받아.
arduino 설치 디렉토리의 libraries 디렉토리 밑에 바로 풀면 된다.
이렇게 하면, DS1307RTC, Time, TimeAlarms 이런 디렉토리가 생긴다.
Time 디렉토리에 가보면, Time.cpp, Time.h 파일과 함께 Readme.txt 파일이 보일 것이다. 디렉토리 밑에는 Examples가 있다.

arduino 실행파일을 종료하고 다시 실행하면,  time library에 대한 example을 실행해볼 수 있다.
아두이노의 큰 장점이기도 하다.




시간은 약간 이슈가 있다.
현재의 시간(NTP)에 대한 정보를 받기 위해서는 이더넷 카드를 쓰거나 수동으로 현재 시간을 입력(setTime함수) 해줘야 한다. 아.. 테스트해보고 나서.. 알았다.

다음 예제를 가지고 만들었다.

http://arduino.cc/en/Tutorial/LiquidCrystal



똑같이 만들어 보았다.



기존 소스를 응용해서 LCD에 출력해보았다.

// include the library code:
#include <LiquidCrystal.h>
#include <Time.h> 

#define TIME_MSG_LEN  11
#define TIME_HEADER  'T'
#define TIME_REQUEST  7

 

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 3);
  lcd.print("Time");
}

void loop() {
   lcd.setCursor(1, 2);
   digitalClockDisplay(); 
   delay(1000);
}


void digitalClockDisplay(){
  // digital clock display of the time
  lcd.setCursor(0, 1);
  lcd.print(year());
  lcd.print(".");
  lcd.print(month());
  lcd.print(".");
  lcd.print(day());
  lcd.print(" ");
  lcd.print(hour());
  lcd.print(":");
  lcd.print(minute());
  lcd.print(":");
  lcd.print(second());

}






이더넷 쉴드를 사용해서 NTP를 쓸 수 없어서 간단한 테스트 정도로 해야겠다.
setTime() 함수를 이용해서 현재시간을 주고, 매 15분마다 alarm을 주는 예제..
간단하게 이벤트 처리로 사용할 수 있다는 점을 배움


#include <Time.h>
#include <TimeAlarms.h>
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
  setTime(14,29,0,1,11,11);
  Alarm.timerRepeat(15, Repeats);            // timer for every 15 seconds   
}

void Repeats() {
    lcd.blink();
    delay(2000);
}

void  loop(){ 
  digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
}

void digitalClockDisplay()
{
 
  lcd.begin(16, 3);
 lcd.print(year());
  lcd.print(".");
  lcd.print(month());
  lcd.print(".");
  lcd.print(day());

  lcd.setCursor(0, 1);
  lcd.print(hour());
  printDigits(minute());
  printDigits(second());
}

void printDigits(int digits)
{
  lcd.print(":");
  if(digits < 10)
    lcd.print('0');
  lcd.print(digits);
}



결과 화면



그리 대단하지는 않지만, character lcd를 새로 사서 어떻게 써먹어야 할지 고민중..





Posted by '김용환'
,