quinta-feira, 16 de setembro de 2010

Wanna know what time it is? (Part 2)

A couple of days ago a friend of mine created a solution for that two-digit problem i posted before. He used the new data type that came along with the 0019, String, it really helps getting the job done. Some pictures below of the before and after and for those interested, the code.

before
after
And here's the Arduino sketch.

#include
#include "RTClib.h"
#include


RTC_DS1307 RTC;

LiquidCrystal lcd(6, 7, 8, 9, 14, 15);

void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();

  if (!RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
 
  lcd.begin(16, 2);
}

void loop () {
    DateTime now = RTC.now();
   
    lcd.clear();
    lcd.setCursor(4, 0);
    lcd.print(format(now.hour()));
    lcd.print(':');
    lcd.print(format(now.minute()));
    lcd.print(':');
    lcd.print(format(now.second()));
   
    lcd.setCursor(3, 1);
    lcd.print(format(now.day()));
    lcd.print('/');
    lcd.print(format(now.month()));
    lcd.print('/');
    lcd.print(now.year(), DEC);   
    
    delay(1000);
}

String format(int num) {
  String addZero = "0";
  String d = "";
 
  if (String(num).length() < 2){
    return addZero + String(num);
  }
 
  return String(num);
}

0 comments:

Postar um comentário