我希望每秒从 GPS 接收器接收信息,但从传感器接收信息 - 每半秒......
我获取了tinyGPS的代码并添加了传感器代码:
#include <TinyGPS.h>
const int RightPin = A0;
const int FrontPin = A1;
const int LeftPin = A2;
int RightVal = 0;
int FrontVal = 0;
int LeftVal = 0;
TinyGPS gps;
void setup() {
Serial.begin(115200); //GPS DATA
Serial1.begin(9600); //GPS
Serial2.begin(9600); //BLUETOOTH
}
void loop() {
RightVal = analogRead(RightPin);
FrontVal = analogRead(FrontPin);
LeftVal = analogRead(LeftPin);
Serial1.print(RightVal);
Serial1.print(", ");
Serial1.print(FrontVal);
Serial1.print(", ");
Serial1.println(LeftVal);
bool newdata = false;
unsigned long start = millis();
// Every second we print an update
while (millis() - start < 1000)
{
if (feedgps())
newdata = true;
}
gpsdump(gps);
}
非常感谢
最佳答案
我不确定这是否是您正在寻找的,但您可以通过使用中断来实现这一点。您可以使用计时器每 0.5 秒生成一次中断,然后每次读取传感器(每两次读取 GPS)。
我没有在 arduino 中完成此操作,而是在 C 中使用 AVR 微 Controller 完成此操作。网上肯定有很多文档。
来自this link您可以阅读:
attachInterrupt(function, period)
Calls a function at the specified interval in microseconds. Be careful about trying to execute too complicated of an interrupt at too high of a frequency, or the CPU may never enter the main loop and your program will 'lock up'. Note that you can optionally set the period with this function if you include a value in microseconds as the last parameter when you call it.
关于c - 如何在arduino板上创建一种线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27764587/