0

我正在尝试对非阻塞 LED 闪烁进行编程。

因此我编写了一个小课:

03: class timer {
04: private:  
05:   int startMillis;
06:   int delayMillis;  
07: public:  
08:  timer ( int pDelay ) {
09:    reset ( pDelay );
10:  }  
11:  void start () {
12:    startMillis = millis();
13:   }  
14:   void reset ( int pDelay ) {
15:     delayMillis = pDelay;    
16:   }  
17:   boolean checkTimer () {
18:     if ( (millis() - startMillis) >= delayMillis ) {
19:       return true;
20:     } else {
21:       return false;
22:     }
23:   }
24: };

然后我想在循环()中做这样的事情:

42: void switchLed (int *pPin, timer *pTimer) {
43:   if ( (*pTimer->checkTimer()) == true ) {
44:     if ( bitRead(PORTD, *pPin) == HIGH ) {
45:       digitalWrite(*pPin, LOW);
46:     } else {      
47:       digitalWrite(*pPin, HIGH);
48:     }
49:     *pTimer->start();
50:   }
51: }

我在 loop() 中使用参数“(&led[0], &ledTimer01)”调用 switchLed() 函数。我认为它应该可以工作,但我的编译器说

nonblockingblink:5: error: 'timer' has not been declared
nonblockingblink.ino: In function 'void switchLed(int*, timer*)':
nonblockingblink:43: error: invalid type argument of 'unary *'
nonblockingblink:49: error: void value not ignored as it ought to be

问题出在哪里?感谢帮助 :)。

4

2 回答 2

1

pTimer->checkTimer()有类型boolean

所以这:

*pTimer->checkTimer()

无效,因为boolean它不是指针类型。

其他功能也一样,为什么要使用*运算符?这是不正确的:

*pTimer->start();

这是对的:

pTimer->start();

或者

(*pTimer).start();  // equivalent to above, prefer the above form
于 2013-02-21T20:57:17.883 回答
1

您使用两种类型的指针取消引用。首先你使用->访问pTimer结构成员,然后你*再次使用非指针类型(boolean返回的checkTimer)。删除星号,它应该可以工作。

于 2013-02-21T20:58:07.453 回答