您好我正在尽我所能去了解C ++和OOP。我写了一些代码,当我把它全部放在我的主循环中时。我现在正在尝试整理它并创建我自己的简单库和类。
我的项目有一个8个LED传感器阵列连接到Arduino板。我正在尝试阅读光传感器的状态。我正在使用QTRSensors库,它有一个校准方法,当我实例化我的班级时,我想从我的班级调用。
目前我有以下代码。
HEADER
#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());
}
我得到以下错误
sedimentLogger.cpp:在成员函数'void sedimentSensor :: calibrateSensors()'中: sedimentLogger.cpp:47:102:错误:无法匹配调用'(QTRSensorsRC)(unsigned char [8],int&amp;,int&amp;,int&amp;)' sedimentLogger.cpp:48:108:错误:不匹配调用'(QTRSensorsRC)(unsigned char [8],int&amp;,int&amp;,int&amp;)'
我尝试了一些不同的变体,例如将传感器实例化放入我的沉积物传感器构造函数和其他一些没有运气的东西。
我很感激任何帮助和建议让这个工作。我在这里显然也错过了一个概念,所以任何教练建议都会有所帮助。
答案 0 :(得分:3)
您似乎正在尝试调用sensor1
和sensor2
的构造函数,但是在代码中他们已经构造了它们。请尝试使用他们的init
功能:
sensor1.init(
(unsigned char[]){led1,led2,led3,led4,led5,led6,led7,led8},
numSensors,
timeout,
emitterPin1);
sensor2.init(
(unsigned char[]){led9,led10,led11,led12,led13,led14,led15,led16},
numSensors,
timeout,
emitterPin2);
答案 1 :(得分:1)
我假设第47和48行是以下两行:
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);
问题是您尝试将对象称为功能。需要在构造函数中初始化对象,如下所示:
sedimentSensor::sedimentSensor()
: led1(24), led2(26), /* other variables needing initialization */
sensor1(/*arguments*/), sensor2(/*arguments*/)
{
/* .... */
}
要使上述工作正常,您需要更改类成员变量声明的顺序,因为初始化的顺序成员变量与它们声明的顺序相同。换句话说,您应该在用于初始化它们的变量之后将sensor1
和sensor2
的声明设为。