2014-11-03 67 views
0

我做了一個簡單的液晶顯示器示例從this guide。 它工作後,我想玩它。我寫了一個程序來計算這個屏幕的fps。最大的問題是Arduino有多慢。Arduino液晶屏fps奇怪的行爲

程序代碼是在這裏:

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

// initialize the library with the numbers of the interface pins 
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); 

int lastMillis = 0; 
long fps = 0; 

void setup() { 
    lcd.begin(16, 2); 

    lcd.print("seconds "); 
    lcd.setCursor(0, 1); 
    lcd.print("fps "); 
} 

void loop() { 
    if ((millis() - lastMillis) > 1000) { 
    lcd.setCursor(8, 0); 
    lcd.print(millis()/1000); 

    lcd.setCursor(4, 1); 
    lcd.print(fps); 
    fps = 0; 
    lastMillis = millis(); 
    } 
    fps = fps + 1; 
} 

和它的工作。我很高興知道Arduino可以在一個小型的16x2液晶顯示器上以超過300,000 fps的速度運行。

但秒數超過32秒(幻數)後,fps凍結在值124,185,之後永遠不會改變。

如果有人知道爲什麼會發生這種情況,請解釋一下。我不明白爲什麼fps(每秒設置爲0)可能會凍結,而秒數則會不斷變化。

我收到了一個視頻,顯示發生了什麼。 Video

然後,ahaltindis建議,我改變了代碼如下:

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

// initialize the library with the numbers of the interface pins 
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); 

int lastMillis = 0; 
long fps = 0; 

void setup() { 
    lcd.begin(16, 2); 
} 

void loop() { 
    if ((millis() - lastMillis) > 1000) { 
    lcd.clear(); 

    lcd.setCursor(0, 0); 
    lcd.print("seconds "); 
    lcd.setCursor(0, 1); 
    lcd.print("fps "); 

    lcd.setCursor(8, 0); 
    lcd.print(millis()/1000); 

    lcd.setCursor(4, 1); 
    lcd.print(fps); 
    fps = 0; 
    lastMillis = millis(); 
    } 
    fps = fps + 1; 
} 

而且它變得更糟糕:video

+0

這是視頻https://www.youtube.com/watch?v=HacKep-U_CY – 2014-11-03 13:19:16

+0

在將fps打印到顯示器之前,嘗試使用lcd.clear()清潔lcd。這可能會解決。 – ahaltindis 2014-11-03 14:05:17

+0

我做到了,但沒有幫助。視頻:[鏈接](https://www.youtube.com/watch?v=J9KwaYnJEus) – 2014-11-03 14:38:21

回答

2

我想你的代碼我的Arduino UNO。但我用Serial.print而不是lcd.print。它以相同的方式行事。當sec碰到32時,串口監視器變得瘋狂。

然後我發現你把lastMillis定義爲一個整數。在arduino(atmega)整數保持16位值,這意味着它可以存儲值範圍-32,768至32,767。當毫秒功能達到32,767(32秒)時,arduino會將您的lastMillis的值設置爲-32,768。

所以,你如果塊11返回32秒後總是如此,因爲millis()lastMillis差異,往往比1000這就是爲什麼你看到的唯一的價值是1,這也是爲何在你的LCD無法迴應以及打印請求更大32秒

您應該做的唯一更改是將您的lastMillis類型更改爲長。

long lastMillis = 0; 
+0

非常感謝。在你解釋它之後,我不敢相信它沒有想到它。 – 2014-11-04 08:06:22

0

嘗試改變

int lastMillis = 0; 

unsigned int lastMillis = 0; 

讓我知道,如果你使用一個unsigned int你會溢出回到一個讓你的原始代碼將工作

會發生什麼