从Arduino中的自定义C ++库构造对象时出错

时间:2018-06-26 18:33:21

标签: c++ arduino

我试图使用自定义库(jtaServoController)编译此Arduino草图,并且Arduino IDE声称我在Arduino草图中的构造函数编写不正确(更准确地说,'jtaServoControllerFour'没有命名类型)。 / p>

#include <jtaServoController.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(0x40);

#define SERVOMIN  0
#define SERVOMAX  600

jtaServoControllerFour pointerFinger(1,2,3,4);   //line in question


void setup() {
  Serial.begin(9600);
  pwm1.begin();
  pwm1.setPWMFreq(60);
}

void loop() {
  pointerFinger.jtaSetPWMFour(300,400,500,600);
}

我的问题是所讨论的行实际上写错了还是我的代码的另一部分出了问题?可能在下面的库中。 (顺便说一句,我在库中的Arduino教程中找到了构造对象的信息。)

这些分别是Header和.cpp文件:

#ifndef jtaServoController_h
#define jtaServoController_h

#include "Wire.h"
#include "Adafruit_PWMServoDriver.h"
#include "Arduino.h"

class jtaServoControllerFour
{
    public:
        jtaServoControllerFour(int servo1, int servo2, int servo3, int servo4);
        void jtaSetPWMFour(unsigned int servo41, unsigned int servo42, unsigned int servo43, unsigned int servo44);
    private:
        int _servoOne;
        int _servoTwo;
        int _servoThree;
        int _servoFour;
};


   #endif

CPP文件

#include "Arduino.h"
#include "jtaServoController.h"
#include "Adafruit_PWMServoDriver.h"
#include "Wire.h"

jtaServoControllerFour::jtaServoControllerFour(int servoOne, int servoTwo, int servoThree, int servoFour)
{
    _servoOne = servoOne;
    _servoTwo = servoTwo;
    _servoThree = servoThree;
    _servoFour = servoFour;
}

void jtaServoControllerFour::jtaSetPWMFour(int servoFourOne, int servoFourTwo, int servoFourThree, int servoFourFour)
{
    pwm1.setPWM(_servo1, 0, servoFourOne);
    pwm1.setPWM(_servo2, 0, servoFourTwo);
    pwm1.setPWM(_servo3, 0, servoFourThree);
    pwm1.setPWM(_servo4, 0, servoFourFour);
    return;
}

1 个答案:

答案 0 :(得分:0)

#include <jtaServoController.h>

您可能需要改用以下内容(双引号):

#include "jtaServoController.h"

和/或检查头文件的路径。

有关两者之间的区别,请参见here