嗨,我正在尽我所能了解 C++ 和 OOP。我写了一些代码,当我把它全部放在我的主循环中时。我现在正在尝试整理它并制作我自己的简单库和类。
我的项目有一个连接到 Arduino 板的 8 LED 传感器阵列。我正在尝试读取光传感器的状态。我正在使用 QTRSensors 库,它有一个校准方法,当我实例化我的类时,我想从我的类中调用它。
目前我有以下代码。
标题
#ifndef SEDIMENTLOGGER_H
#define SEDIMENTLOGGER_H
#include <QTRSensors.h>
#include <Arduino.h>
#define NUM_SENSORS 8
class sedimentSensor
{
public:
QTRSensorsRC sensor1;//(unsigned char [8], int&, int&, int&);
QTRSensorsRC sensor2;//(unsigned char [8], int&, int&, int&);
sedimentSensor(); //default constructor
~sedimentSensor();
void calibrateSensors();
unsigned int *SensorValues1(){return sensorValues1;};
unsigned int *SensorValues2(){return sensorValues2;};
int SampleDelay(){return sampleDelay;};
int EmitterPin1();
unsigned int Position1(){return position1;};
void Position1(unsigned int value){position1 = value;};
unsigned int Position2(){return position2;};
void Position2(unsigned int value){position2 = value;};
private:
// Map pins to LED numbers
// first LED array
int led1;
int led2;
int led3;
int led4;
int led5;
int led6;
int led7;
int led8;
int emitterPin1;
//second Array
int led9;
int led10;
int led11;
int led12;
int led13;
int led14;
int led15;
int led16;
int emitterPin2;
int timeout; // waits for 2.5 seconds for sensor outputs to go low
long sampleDelay; // 1 second
int numSensors;
unsigned int sensorValues1[];
unsigned int sensorValues2[];
unsigned int position1;
unsigned int position2;
};
#endif
我对课堂实施的尝试
#include "sedimentLogger.h"
#include <Arduino.h>
#include <QTRSensors.h>
sedimentSensor::sedimentSensor()
{
//Constructor
led1 = 24;
led2 = 26;
led3 = 28;
led4 = 30;
led5 = 32;
led6 = 34;
led7 = 36;
led8 = 38;
emitterPin1 = 22;
//second Array
led9 = 25;
led10 = 27;
led11 = 29;
led12 = 31;
led13 = 33;
led14 = 35;
led15 = 37;
led16 = 39;
emitterPin2 = 23;
timeout = 2500; // waits for 2.5 seconds for sensor outputs to go low
sampleDelay = 1000; // 1 second
sensorValues1[NUM_SENSORS];
sensorValues2[NUM_SENSORS];
};
sedimentSensor::~sedimentSensor()
{
};
int sedimentSensor::EmitterPin1()
{return emitterPin1;};
void sedimentSensor::calibrateSensors()
{
sensor1((unsigned char[]){led1,led2,led3,led4,led5,led6,led7,led8},numSensors,timeout,emitterPin1);
sensor2((unsigned char[]) {led9,led10,led11,led12,led13,led14,led15,led16},numSensors,timeout,emitterPin2);
unsigned int _delay = 500;
delay(_delay);
pinMode(13,OUTPUT);
for(int i = 0; i<400; i++)
{
sensor1.calibrate();
sensor2.calibrate();
}
digitalWrite(13,LOW);
//TODO WRITE MAX AND MIN VALS TO FILE
Serial.print("Array 1 Min :: ");
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print (sensor1.calibratedMinimumOn[i]);
Serial.print(' ');
}
Serial.println();
Serial.print("Array 2 Min :: ");
for (int i = 0;i< NUM_SENSORS; i++)
{
Serial.print (sensor2.calibratedMinimumOn[i]);
Serial.print(' ');
}
Serial.println();
Serial.print("array 1 Max :: ");
// print the calibration maximum values measured when emitters were on
for (int i = 0; i < NUM_SENSORS; i++)
{
Serial.print(sensor1.calibratedMaximumOn[i]);
Serial.print(' ');
}
Serial.println();
Serial.print("array 2 Max :: ");
// print the calibration maximum values measured when emitters were on
for (int
i = 0; i < NUM_SENSORS; i++)
{
Serial.print(sensor2.calibratedMaximumOn[i]);
Serial.print(' ');
}
Serial.println();
Serial.println();
delay(_delay);
};
还有我的主要功能和循环
#include <QTRSensors.h>
#include <Arduino.h>
#include "sedimentLogger.h"
#include <Wire.h>
#include "/home/marrabld/Programming/Arduino/arduino-1.0/libraries/RTClib/RTClib.h"
sedimentSensor sedLog;//= sedimentSensor();
void setup()
{
Serial.begin(9600);
sedLog.calibrateSensors();
}
void loop()
{
sedLog.Position1(sedLog.sensor1.readLine(sedLog.SensorValues1()));
sedLog.Position2(sedLog.sensor2.readLine(sedLog.SensorValues2()));
unsigned char i;
for (i = 0; i < NUM_SENSORS; i++)
{
Serial.print(sedLog.SensorValues1()[i] * 10/10001);
Serial.print(' ');
}
Serial.println(sedLog.Position1());
for (i=0;i < NUM_SENSORS; i++)
{
Serial.print(sedLog.SensorValues2()[i]*10/1001);
Serial.print(' ');
}
Serial.print(" ");
Serial.println(sedLog.Position2());
delay(sedLog.SampleDelay());
Serial.println(sedLog.SampleDelay());
Serial.println(sedLog.EmitterPin1());
}
我收到以下错误
沉积物记录器.cpp:在成员函数“无效沉积物传感器::校准传感器()”中:沉积物记录器.cpp:47:102:错误:不匹配调用“(QTRSensorsRC)(无符号字符[8],int&,int&,int&)”沉积物Logger.cpp:48:108: 错误: 不匹配调用'(QTRSensorsRC) (unsigned char [8], int&, int&, int&)'</p>
我尝试了一些不同的变体,比如将传感器实例化放在我的沉积物传感器构造函数中,以及其他一堆没有运气的东西。
我将不胜感激任何帮助和建议让这个工作。我显然在这里也遗漏了一个概念,因此任何教练建议同样会有所帮助。