使用Arduino的类,使用SoftwareSerial作为变量

时间:2014-06-03 16:30:18

标签: class arduino

尝试在Arduino 1.0中使用类并将SoftwareSerial设置为变量但没有成功。

class SensorGPS: public Thread
{
  private: 
        SoftwareSerial* serialGPS; // RX, TX
  public:
        SensorGPS()
        {
          serialGPS = new SoftwareSerial(10,11);
          serialGPS.begin(4800);
        }
}

serialGPS.begin返回错误

arduino_sketch.ino: In constructor 'SensorGPS::SensorGPS()':
arduino_sketch:31: error: request for member 'begin' in '((SensorGPS*)this)->SensorGPS::serialGPS', which is of non-class type 'SoftwareSerial*'
arduino_sketch.ino: In member function 'virtual void SensorGPS::run()':
arduino_sketch:37: error: request for member 'read' in '((SensorGPS*)this)->SensorGPS::serialGPS', which is of non-class type 'SoftwareSerial*'
arduino_sketch:44: error: request for member 'write' in '((SensorGPS*)this)->SensorGPS::serialGPS', which is of non-class type 'SoftwareSerial*'

如果在设置变量

时删除*
SoftwareSerial serialGPS(10,11); // RX, TX

变量

的结果错误
arduino_sketch:21: error: expected identifier before numeric constant
arduino_sketch:21: error: expected ',' or '...' before numeric constant

此问题适用于需要值作为初始化的所有类。 Dht11模块的另一个例子(1);生成相同的错误。

1 个答案:

答案 0 :(得分:0)

请注意示例Dht11库的相同问题。所以我更改了构造函数(库https://github.com/adalton/arduino/tree/master/projects/Dht11_Library)。

示例Dht11打开Dht11.h 来自

Dht11(uint8_t newPin) : humidity(-1), temperature(-1), pin(newPin) { }

To(添加了功能setPin)

Dht11() : humidity(-1), temperature(-1), pin(-1) { }

void setPin(uint8_t newPin) {
   pin = newPin;
}

所以现在在Arduino草图中我可以在使用前设置引脚。这个解决方案可以用于大多数库。

class SensorTemperatureHumidity: public Thread
{
  private: 
        static const int Pin = 3;
        Dht11 module;
  public:
        SensorTemperatureHumidity() {
          module.setPin(Pin); // Set my pin usage
        }
}