2012-08-31 3 views
0

I LCD 스크린의 첫 번째 줄에 시계를 표시해야이 코드 및 두 번째 줄의 텍스트에 "Hello World"가 다음아두 이노의 LCD 시계

#include <LiquidCrystal.h> 
int x=0; 
int a=0; 
int y=0; 
int z=0; 
int initialHours = 14;//set this to whatever 
int initialMins = 37; 
int initialSecs = 45 + 11; 

int secspassed() 
{ 
    x = initialHours*3600; 
    x = x+(initialMins*60); 
    x = x+initialSecs; 
    x = x+(millis()/1000); 
    return x; 
} 

int hours() 
{ 
    y = secspassed(); 
    y = y/3600; 
    y = y%24; 
    return y; 
} 

int mins() 
{ 
    z = secspassed(); 
    z = z/60; 
    z = z%60; 
    return z; 
} 

int secs() 
{ 
    a = secspassed(); 
    a = a%60; 
    return a; 
} 

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 

void setup(){ 
    lcd.print("load..."); 
    delay(1000); 
    lcd.begin(16, 2); 
    lcd.setCursor(0, 1); 
    lcd.print("Hello world"); 
} 

void loop(){ 
    digitalClockDisplay(); 
} 

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

char sep() 
{ 
    x = millis()/1000; 
    if(x%2==0) 
    { 
     lcd.print(":"); 
    } 
    else { 
     lcd.print(" "); 
    } 
} 

void digitalClockDisplay(){ 
    lcd.setCursor(0,0); 
    printDigits(
    hours()); 
    sep(); 
    printDigits(mins()); 
    sep(); 
    printDigits(secs()); 
} 

대신 인쇄를

12:35:15 
Hello World 

는이 대신 인쇄 :

253:255:243 
Hello World 

이유는 무엇입니까?

시간 라이브러리 BTW를 사용하고 싶지 않습니다.

+0

그냥 전화하면 millis()가 출력합니까? – CBredlow

+0

코드를 업로드 한 이후의 시간 (밀리 초)입니다. – Cinder

답변

3

코드는 여기 [아두 이노에 대해 그 값이 +/- 32767] capacity of an int 범람이 포인트 X에서

int secspassed() 
{ 
    x = initialHours*3600; 
    x = x+(initialMins*60); 

는 같아야

14 * 3600 * 60 = 3024000 

어느 int가 가질 수있는 +32767 값보다 훨씬 크며 오버플로가 제거되어 관련성없는 숫자를 나타내는 비트가 남습니다.

인쇄 루틴을 int로 호출하여 정리하고 바이트로 캐스팅해야합니다.

1

저는 왜 이것이 나쁜 아이디어 일지 생각해 냈습니다. rollovers과 같은 것들이 있습니다. 관심있는 사람들을위한 업데이트 된 코드는 다음과 같습니다.

#include <LiquidCrystal.h> 

int second=0, minute=0, hour=0; 
int x=0; 

LiquidCrystal lcd(8, 9, 4, 5, 6, 7); 

void setup(){ 
    lcd.print("load..."); 
    delay(1000); 
    lcd.begin(16, 2); 
    lcd.setCursor(0, 1); 
    lcd.print("Hello, world "); 
} 

void loop(){ 
    static unsigned long lastTick = 0; 
    if (millis() - lastTick >= 1000) { 
     lastTick = millis(); 
     second++; 
    } 

    // Move forward one minute every 60 seconds 
    if (second >= 60) { 
     minute++; 
     second = 0; // Reset seconds to zero 
    } 

    // Move forward one hour every 60 minutes 
    if (minute >=60) { 
     hour++; 
     minute = 0; // Reset minutes to zero 
    } 

    if (hour >=24) { 
     hour=0; 
     minute = 0; // Reset minutes to zero 
    } 
    digitalClockDisplay(); 
} 

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

char sep() 
{ 
    x = millis()/500; 
    if(x%2==0) 
    { 
     lcd.print(":"); 
    } 
    else{ 
     lcd.print(" "); 
    } 
} 

void digitalClockDisplay(){ 
    lcd.setCursor(0,0); 
    printDigits(hour); 
    sep(); 
    printDigits(minute); 
    sep(); 
    printDigits(second); 
} 

재미 있습니다!

추신 : 업데이트 된 코드는 my blog으로 이동하십시오.