2014-11-03 69 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你会溢出回到一个让你的原始代码将工作

会发生什么