Arduino 写入屏幕的速度

Arduino writing speed to screen

本文关键字:速度 屏幕 Arduino      更新时间:2023-10-16

我无法让我的GPS更新速度。它显示文本,如果我将 GPS 速度从位置更新循环中取出,它会显示一次当前速度,然后无法更新。知道吗?

void loop() {
while (serial_connections.available()) {
gps.encode(serial_connections.read());
if (gps.location.isUpdated()) {
DText = Serial.println(gps.speed.mps());
DSat = Serial.println(gps.satellites.value());
}
display.clearDisplay();  // clears last number
display.display();  // writes clear to screen
display.setCursor(10, 5);  //Set drawing posision
display.print(DText);  // what to draw
display.setCursor(35, 5);
display.print(" MPS");
display.setCursor(10, 18);
display.print(DSat);
display.setCursor(35, 18);
display.print(" Sat");
display.display(); // writes to the screen
delay (50);
}
}

它显示当前速度一次,然后无法更新。知道吗?

您的草图花费所有时间更新显示并等待。 以下是正在发生的事情:

1)当一个字符可用时,它被读取并传递给encode

2)然后它更新显示,这需要一些时间。 你没有给我们整个程序,也没有确定硬件,所以我真的不能说需要多长时间。

3)然后等待50ms。 在此期间,GPS字符继续到达。 它们将存储在输入缓冲区中,直到调用read()或者直到存储 64 个字符。 然后它们将被丢弃。

在 9600(我猜)时,可能会有 50 个字符。 现在输入缓冲区几乎已满。

4) 再次执行while循环测试,读取并解析第二个字符(步骤 1),更新显示(没有新信息可用,步骤 2),再等待 50 毫秒。

15 毫秒后,输入缓冲区已满,Arduino 开始忽略字符。 当 50ms 延迟完成时,丢失了 35 个字符(在 9600 处)。

这会阻止成功解析收到的(部分)NMEA 句子,并且速度不会更新。 草图将继续使用旧信息更新显示,然后再等待一段时间,这会导致更多的字符丢失。

循环结构需要重新设计,以便仅在有新信息可用时才更新显示,并且切勿使用延迟:

#include <LiquidCrystal.h> ???
LiquidCrystal display;     ???
#include <NMEAGPS.h>
NMEAGPS gps;
gps_fix fix;
//  Here are three different ways to connect the GPS:
#define gpsPort Serial1
//#include <AltSoftSerial.h>
//AltSoftSerial gpsPort; // two specific pins required (8 & 9 on an UNO)
//#include <NeoSWSerial.h>
//NeoSWSerial gpsPort( 3, 4 );

void setup()
{
Serial.begin( 9600 );
gpsPort.begin( 9600 );
}
void loop()
{
// Read and parse any available characters from the GPS device
if (gps.available( gpsPort )) {
// Once per second, a complete fix structure is ready.
fix = gps.read();
Serial.print( F("Speed: ") );
float speed = 0.0;
if (fix.valid.speed) {
speed = fix.speed_kph() * 1000.0 / 3600.0;
Serial.print( speed );
}
Serial.println();
Serial.print( F("Sats: ") );
if (fix.valid.satellites)
Serial.println( fix.satellites );
Serial.println();
//  Update the display ONCE PER SECOND
display.clearDisplay();  // clears last number
display.display();  // writes clear to screen
display.setCursor(10, 5);  //Set drawing posision
if (fix.valid.speed)
display.print( speed );  // what to draw
display.setCursor(35, 5);
display.print(" MPS");
display.setCursor(10, 18);
if (fix.valid.satellites)
display.print( fix.satellites );
display.setCursor(35, 18);
display.print(" Sat");
display.display(); // writes to the screen
}
}

这使用我的NeoGPS库。 它比所有其他GPS库更小,更快,更可靠,更准确。 即使您不使用它,也应该阅读有关选择串行端口和故障排除的相关页面。

NeoGPS,AltSoftSerial和NeoSWSerial都可以从Arduino IDE库管理器中获得,在菜单Sketch ->Include Library -> Manage Libraries下。