C++ 高度、航向和速度未显示在LCD显示屏上
我为我的自行车电脑写了这个代码;但是,尽管Alt、航向和速度在串行监视器上,但它不显示在LCD显示屏上。GPS模块也没有返回值C++ 高度、航向和速度未显示在LCD显示屏上,c++,arduino,arduino-uno,C++,Arduino,Arduino Uno,我为我的自行车电脑写了这个代码;但是,尽管Alt、航向和速度在串行监视器上,但它不显示在LCD显示屏上。GPS模块也没有返回值 /* BaldwinOS by John Seong An Open-Source Project Version 1.0 */ #include <SPI.h> #include <SD.h> #include <TinyGPS++.h> // Include the TinyGPS++ library #includ
/* BaldwinOS by John Seong
An Open-Source Project
Version 1.0
*/
#include <SPI.h>
#include <SD.h>
#include <TinyGPS++.h> // Include the TinyGPS++ library
#include <Wire.h>
TinyGPSPlus tinyGPS; // Create a TinyGPSPlus object
#define ARDUINO_USD_CS 10 // uSD card CS pin (pin 10 on SparkFun GPS Logger Shield)
/////////////////////////
// Log File Defintions //
/////////////////////////
// Keep in mind, the SD library has max file name lengths of 8.3 - 8 char prefix,
// and a 3 char suffix.
// Our log files are called "gpslogXX.csv, so "gpslog99.csv" is our max file.
#define LOG_FILE_PREFIX "gpslog" // Name of the log file.
#define MAX_LOG_FILES 100 // Number of log files that can be made
#define LOG_FILE_SUFFIX "csv" // Suffix of the log file
char logFileName[13]; // Char string to store the log file name
// Data to be logged:
#define LOG_COLUMN_COUNT 8
char * log_col_names[LOG_COLUMN_COUNT] = {
"longitude", "latitude", "altitude", "speed", "course", "date", "time", "satellites"
}; // log_col_names is printed at the top of the file.
//////////////////////
// Log Rate Control //
//////////////////////
#define LOG_RATE 5000 // Log every 5 seconds
unsigned long lastLog = 0; // Global var to keep of last time we logged
#define GPS_BAUD 9600 // GPS module baud rate. GP3906 defaults to 9600.
#define SerLCD_Address 0x72
#include <SoftwareSerial.h>
#define ARDUINO_GPS_RX 9 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX 8 // GPS RX, Arduino TX pin
SoftwareSerial ssGPS(ARDUINO_GPS_TX, ARDUINO_GPS_RX); // Create a SoftwareSerial
#define gpsPort ssGPS
#define SerialMonitor Serial
const int statusLED = 9;
const int blueLeftLED = 8;
const int redLeftLED = 7;
const int blueRightLED = 0;
const int redRightLED = 3;
const int leftButton = A3;
const int rightButton = 11;
const int modeButton = 12;
const int buzzer = 13;
void setup() {
SerialMonitor.begin(9600);
gpsPort.begin(GPS_BAUD);
SerialMonitor.println("Setting up SD card.");
// see if the card is present and can be initialized:
if (!SD.begin(ARDUINO_USD_CS))
{
SerialMonitor.println("Error initializing SD card.");
}
updateFileName(); // Each time we start, create a new file, increment the number
printHeader(); // Print a header at the top of the new file
Wire.begin();
Wire.setClock(400000);
Wire.beginTransmission(SerLCD_Address);
Wire.write('|'); // Put LCD into setting mode
Wire.write('-'); // Send clear display command
Wire.write('|'); // Put LCD into setting mode
Wire.write('+'); // Send the Set RGB command
Wire.write(0xFF); // Send the red value
Wire.write(0x00); // Send the green value
Wire.write(0x00); // Send the blue value
Wire.println("Welcome to");
Wire.print("BaldwinOS!");
Wire.endTransmission();
delay(2000);
}
void loop() {
Wire.beginTransmission(SerLCD_Address);
Wire.write('|'); // Put LCD into setting mode
Wire.write('-'); // Send clear display command
Wire.println("Pathfinder");
Wire.print("Bike Computer");
if ((lastLog + LOG_RATE) <= millis())
{ // If it's been LOG_RATE milliseconds since the last log:
if (tinyGPS.location.isUpdated()) // If the GPS data is vaild
{
if (logGPSData()) // Log the GPS data
{
SerialMonitor.println("GPS logged."); // Print a debug message
lastLog = millis(); // Update the lastLog variable
}
else // If we failed to log GPS
{ // Print an error, don't update lastLog
SerialMonitor.println("Failed to log new GPS data.");
}
}
else // If GPS data isn't valid
{
// Print a debug message. Maybe we don't have enough satellites yet.
SerialMonitor.print("No GPS data. Sats: ");
SerialMonitor.println(tinyGPS.satellites.value());
}
}
printStats1();
smartDelay(1000);
}
byte logGPSData()
{
File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file
if (logFile)
{ // Print longitude, latitude, altitude (in feet), speed (in mph), course
// in (degrees), date, time, and number of satellites.
logFile.print(tinyGPS.location.lng(), 6);
logFile.print(',');
logFile.print(tinyGPS.location.lat(), 6);
logFile.print(',');
logFile.print(tinyGPS.altitude.feet(), 1);
logFile.print(',');
logFile.print(tinyGPS.speed.mph(), 1);
logFile.print(',');
logFile.print(tinyGPS.course.deg(), 1);
logFile.print(',');
logFile.print(tinyGPS.date.value());
logFile.print(',');
logFile.print(tinyGPS.time.value());
logFile.print(',');
logFile.print(tinyGPS.satellites.value());
logFile.println();
logFile.close();
return 1; // Return success
}
return 0; // If we failed to open the file, return fail
}
void printHeader()
{
File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file
if (logFile) // If the log file opened, print our column names to the file
{
int i = 0;
for (; i < LOG_COLUMN_COUNT; i++)
{
logFile.print(log_col_names[i]);
if (i < LOG_COLUMN_COUNT - 1) // If it's anything but the last column
logFile.print(','); // print a comma
else // If it's the last column
logFile.println(); // print a new line
}
logFile.close(); // close the file
}
}
// updateFileName() - Looks through the log files already present on a card,
// and creates a new file with an incremented file index.
void updateFileName()
{
int i = 0;
for (; i < MAX_LOG_FILES; i++)
{
memset(logFileName, 0, strlen(logFileName)); // Clear logFileName string
// Set logFileName to "gpslogXX.csv":
sprintf(logFileName, "%s%d.%s", LOG_FILE_PREFIX, i, LOG_FILE_SUFFIX);
if (!SD.exists(logFileName)) // If a file doesn't exist
{
break; // Break out of this loop. We found our index
}
else // Otherwise:
{
SerialMonitor.print(logFileName);
SerialMonitor.println(" exists"); // Print a debug statement
}
}
SerialMonitor.print("File name: ");
SerialMonitor.println(logFileName); // Debug print the file name
}
void printStats1() {
Wire.beginTransmission(SerLCD_Address);
Wire.write('|'); // Put LCD into setting mode
Wire.write('-'); // Send clear display command
Wire.print("Alt: "); Wire.print(tinyGPS.altitude.feet());
Wire.print(" Course: "); Wire.println(tinyGPS.course.deg());
Wire.print("Speed: "); Wire.println(tinyGPS.speed.mph());
Serial.print("Alt: "); SerialMonitor.println(tinyGPS.altitude.feet());
Serial.print("Course: "); SerialMonitor.println(tinyGPS.course.deg());
Serial.print("Speed: "); SerialMonitor.println(tinyGPS.speed.mph());
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
// If data has come in from the GPS module
while (gpsPort.available())
tinyGPS.encode(gpsPort.read()); // Send it to the encode function
// tinyGPS.encode(char) continues to "load" the tinGPS object with new
// data coming in from the GPS module. As full NMEA strings begin to come in
// the tinyGPS library will be able to start parsing them for pertinent info
} while (millis() - start < ms);
}
/*John Seong创作的BaldwinOS
开源项目
版本1.0
*/
#包括
#包括
#include//include TinyGPS++库
#包括
Tinygpslus tinyGPS;//创建TinyGPSPlus对象
#定义ARDUINO_USD_CS 10//USD卡CS pin(SparkFun GPS记录器护罩上的pin 10)
/////////////////////////
//日志文件定义//
/////////////////////////
//请记住,SD库的最大文件名长度为8.3-8个字符前缀,
//和一个3字符的后缀。
//我们的日志文件称为“gpslogXX.csv”,因此“gpslog99.csv”是我们的最大文件。
#定义日志文件前缀“gpslog”//日志文件名。
#定义最大日志文件数100//
#定义日志文件后缀“csv”//日志文件后缀
char logFileName[13];//用于存储日志文件名的char字符串
//要记录的数据:
#定义日志列计数8
字符*日志列名称[日志列计数]={
“经度”、“纬度”、“高度”、“速度”、“航向”、“日期”、“时间”、“卫星”
};//log_col_名称打印在文件顶部。
//////////////////////
//日志速率控制//
//////////////////////
#每5秒定义一次日志记录率5000//LOG
unsigned long lastLog=0;//保留上次登录时的全局变量
#定义GPS_波特率9600//GPS模块波特率。GP3906默认为9600。
#定义SerLCD_地址0x72
#包括
#定义ARDUINO_GPS_RX 9//GPS TX,ARDUINO RX引脚
#定义ARDUINO_GPS_TX 8//GPS RX,ARDUINO TX引脚
SoftwareSerial ssGPS(ARDUINO_GPS_TX,ARDUINO_GPS_RX);//创建一个SoftwareSerial
#定义gpsPort ssGPS
#定义串行监视器串行
const int statusLED=9;
常数int blueLeftLED=8;
常数int redLeftLED=7;
常数int blueRightLED=0;
常数int redrighted=3;
常量int leftButton=A3;
常数int rightButton=11;
const int modeButton=12;
常数int蜂鸣器=13;
无效设置(){
SerialMonitor.begin(9600);
gpsPort.begin(GPS_波特);
SerialMonitor.println(“设置SD卡”);
//查看该卡是否存在并可以初始化:
如果(!SD.开始(ARDUINO_USD_CS))
{
SerialMonitor.println(“初始化SD卡时出错”);
}
updateFileName();//每次启动时,创建一个新文件,并递增该数字
printHeader();//在新文件的顶部打印头
Wire.begin();
有线设置时钟(400000);
线路起始传输(SerLCD_地址);
Wire.write(“|”);//将LCD置于设置模式
Wire.write('-');//发送清除显示命令
Wire.write(“|”);//将LCD置于设置模式
Wire.write(“+”);//发送Set RGB命令
Wire.write(0xFF);//发送红色值
Wire.write(0x00);//发送绿色值
Wire.write(0x00);//发送蓝色值
Wire.println(“欢迎使用”);
Wire.print(“BaldwinOS!”);
导线端传动();
延迟(2000年);
}
void循环(){
线路起始传输(SerLCD_地址);
Wire.write(“|”);//将LCD置于设置模式
Wire.write('-');//发送清除显示命令
Wire.println(“探路者”);
电线印刷(“自行车电脑”);
如果((lastLog+LOG_RATE)如果您使用的是无源GPS天线,则应在室外(或在天线指向天空的打开窗口中)对其进行测试获取卫星定位。TinyGPS库不会返回任何数据,如果您没有定位值电路图可能比照片更有用,如果您没有电路图,那么这可能是一个很好的起点,绘制一个图表并确保您的布线与之匹配。照片是无用的。绘制一个原理图,但这是一个代码Q&a site,不是电路设计圆桌会议。我确实在外面测试了它;但是,我得到了与上面所述相同的结果。此外,“leftBlueLED”由于某种原因一直闪烁:(嗨,约翰!让我们在这个问题上再深入一点。你能为串行监视器中打印GPS数据制作一个代码吗?现在不要担心LCD或SD卡,让我们隔离这个问题。一旦你有了这个代码,请与我们分享串行输出Alan和Tom给你的一个好建议:首先绘制一个原理图;这将有助于丢弃任何硬件发问候