c - 如何在arduino板上创建一种线程?

标签 c multithreading gps arduino

我希望每秒从 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/

相关文章:

python - ThreadPoolExecutor 中的 worker 并不是真正的守护进程

android - onActivityResult 已弃用如何发送 startResolutionForResult

c - epoll:它会悄悄地删除 fds 吗?

c++ - 你能在c中的同一个语句中的同一个变量上有一个递增器和一个递减器吗

c++ - Boost 线程串行运行,而不是并行运行

multithreading - "benaphores"是否值得在现代操作系统上实现?

c - 链接列表添加功能未按预期工作

c - 如何确定父/其他目录中的文件和目录

android - 在不使用 GPS 的情况下查找手机的当前位置

iphone - 如何在 iPhone 上实现里程表