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

时间:2015-01-04 10:29:38

标签: 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);
}

非常感谢

1 个答案:

答案 0 :(得分:1)

我不确定这是否是您正在寻找的,但您可以通过使用中断来实现这一目标。您可以使用计时器每0.5秒生成一次中断,并且每次只读取您的传感器(以及每两次GPS)。

我还没有在arduino中这样做,但在AV中使用AVR微控制器。在线必须有很多文档。

来自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.