使用 Arduino-timer.h 通过 reference/pointer 为 Arduino 传递结构

Passing a struct by reference/pointer using arduino-timer.h for Arduino

我正在构建一个带有各种必须闪烁的 LED 的 arduino 项目。我决定使用 [arduino-timer.h] (https://www.arduino.cc/reference/en/libraries/arduino-timer/)。我不知道 C++。我知道 javascript.

arduino-timer.h 函数(.in、.at、.every)允许将第三个参数传递给正在调用的定时器函数。我希望这第三个参数成为一个结构,以获得更大的灵活性。

请参阅下面的代码,其中显示了我正在尝试的基本示例。然而,变量 colourCount 并没有改变。根据我收集到的信息,为了能够做到这一点,我应该通过引用或指针传递结构变量。

在函数调用中我试过类似

bool genericOn(TimerStruct &obj) { 

但这是抛出错误:

In function 'void setup()':
timer_struct_test:27:58: error: no matching function for 
call to 'Timer<3u, millis, TimerStruct>::every(const int&, bool (&)(TimerStruct&), TimerStruct*)'
   repeatTask = u_timer.every(Interval, genericOn, &timerC);

所以....有人可以帮忙吗?

除了 colorCount 保持不变之外,下面是有效的代码。

#include <arduino-timer.h>

Timer<>::Task repeatTask;
Timer<>::Task blinkTask;

const int DIO_GREEN = 17; 
const int Interval = 1000;
struct TimerStruct
{
    int total;
    int color;
    int colorCount;
    int inBetween;
};

Timer<3, millis, struct TimerStruct> u_timer;
Timer<10, millis, struct TimerStruct> b_timer;
void setup (){
  Serial.begin(9600);   // Initialize serial communications with the PC
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  pinMode (DIO_GREEN, OUTPUT);
  struct TimerStruct timerC;
  timerC.total = 10;
  timerC.color = DIO_GREEN;
  timerC.colorCount = 0;
  timerC.inBetween = 500;
  repeatTask = u_timer.every(Interval, genericOn, timerC);
}

void loop() {
  u_timer.tick(); 
  b_timer.tick(); 
}

bool genericOn(TimerStruct obj) {
  digitalWrite(obj.color, LOW);
   Serial.print(obj.colorCount);
   Serial.print(" - color ");
   Serial.print(obj.color);
   Serial.print(" - total ");
  Serial.print(obj.total);
  Serial.print("- inbetween");
   Serial.println(obj.inBetween);
  blinkTask = b_timer.in(obj.inBetween, genericOff, obj);
  if (obj.colorCount< obj.total){
   obj.colorCount++;
   return true;
  }
  return false;
}

bool genericOff(TimerStruct obj) {
  digitalWrite(obj.color, HIGH);
  return true;
}

我亲爱的朋友 Ant#1 通过将 & 和 * 放在正确的位置解决了这个问题:

    #include <arduino-timer.h>

 Timer<>::Task repeatTask;
 Timer<>::Task blinkTask;

const int DIO_GREEN = 17;
const unsigned long Interval = 1000;
struct TimerStruct
{
  int total;
  int color;
  int colorCount;
  int inBetween;
};

TimerStruct timerC;


Timer<3, millis, TimerStruct*> u_timer;
Timer<10, millis, TimerStruct*> b_timer;

void setup () {
  Serial.begin(9600);
  while (!Serial);
  pinMode (DIO_GREEN, OUTPUT);
  timerC.total = 10;
  timerC.color = DIO_GREEN;
  timerC.colorCount = 0;
  timerC.inBetween = 500;
  u_timer.every(Interval, genericOn, &timerC);
}

void loop() {
  u_timer.tick();
  b_timer.tick();
}

bool genericOn(TimerStruct *obj) {
  digitalWrite(obj->color, LOW);
  Serial.print(obj->colorCount);
  Serial.print(" - color ");
  Serial.print((*obj).color);
  Serial.print(" - total ");
  Serial.print(obj->total);
  Serial.print("- inbetween");
  Serial.println(obj->inBetween);
  b_timer.in(obj->inBetween, genericOff, obj);
  if (obj->colorCount < obj->total) {
    Serial.println("inside if");
    obj->colorCount++;
    return true;
  }
  return false;
}

bool genericOff(TimerStruct *obj) {
  digitalWrite(obj->color, HIGH);
  return true;
}