我对 C++ 非常缺乏经验,但是我需要将它用于我正在从事的这个项目。我正在使用 Arduino Mega 来读取和记录 LED 阵列。我让代码连续工作,但现在我试图通过定义我自己的库和类来整理它。当我尝试使用访问器传递简单的数据(如引脚号和超时)时,它们总是返回 0。或者当我尝试在串行端口上打印它们进行测试时似乎就是这种情况。这是我的代码
沉积物记录器.h
#ifndef SEDIMENTLOGGER_H
#define SEDIMENTLOGGER_H
#include "/home/marrabld/Programming/Arduino/arduino-1.0/libraries/QTRSensors/QTRSensors.h"
#include <Arduino.h>
#define NUM_SENSORS 8
//==================================================
// This header should hold 3 small classes used for :
//--------------------------------------------------
// 1. sedimentSensor
// Dealing with sensorHardware
// 2. sedimentWriter
// Reading and writing data
// 3. sedimentTimer
// Reading and writing to the RTC
//--------------------------------------------------
//
//==================================================
class sedimentSensor
{
public:
QTRSensorsRC sensor1;
QTRSensorsRC sensor2;
sedimentSensor(); //default constructor
~sedimentSensor();
void calibrateSensors();
unsigned int *SensorValues1(){return sensorValues1;};
unsigned int *SensorValues2(){return sensorValues2;};
int SampleDelay(){return sampleDelay;};
int EmitterPin1();
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[];
};
沉积物记录器.cpp
#include "sedimentLogger.h"
#include <Arduino.h>
#include <QTRSensors.h>
//#include "/home/marrabld/Programming/Arduino/arduino-1.0/libraries/QTRSensors/QTRSensors.h"
sedimentSensor::sedimentSensor()
{
//Constructor
int led1 = 24;
int led2 = 26;
int led3 = 28;
int led4 = 30;
int led5 = 32;
int led6 = 34;
int led7 = 36;
int led8 = 38;
int emitterPin1 = 22;
//second Array
int led9 = 25;
int led10 = 27;
int led11 = 29;
int led12 = 31;
int led13 = 33;
int led14 = 35;
int led15 = 37;
int led16 = 39;
int emitterPin2 = 23;
int timeout = 2500; // waits for 2.5 seconds for sensor outputs to go low
int sampleDelay = 1000; // 1 second
QTRSensorsRC sensor1 ((unsigned char[]) {led1,led2,led3,led4,led5,led6,led7,led8},numSensors, timeout,emitterPin1);
QTRSensorsRC sensor2 ((unsigned char[]) {led9,led10,led11,led12,led13,led14,led15,led16},numSensors,timeout,emitterPin2);
unsigned int sensorValues1[NUM_SENSORS];
unsigned int sensorValues2[NUM_SENSORS];
};
sedimentSensor::~sedimentSensor()
{
};
int sedimentSensor::EmitterPin1()
{return emitterPin1;};
void sedimentSensor::calibrateSensors()
{
Serial.begin(9600);
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 "/home/marrabld/Programming/Arduino/arduino-1.0/libraries/QTRSensors/QTRSensors.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()
{
unsigned int position1 = sedLog.sensor1.readLine(sedLog.SensorValues1()); // guessing its passing SensorValues as 0s
unsigned int 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(position1);
for (i=0;i < NUM_SENSORS; i++)
{
Serial.print(sedLog.SensorValues2()[i]*10/1001);
Serial.print(' ');
}
Serial.print(" ");
Serial.println(position2);
delay(sedLog.SampleDelay()); // No delay
Serial.println(sedLog.SampleDelay()); // prints zero and shouldn't
Serial.println(sedLog.EmitterPin1()); // prints zero and shouldn't
}
校准传感器会在 LED 按应有的短暂亮起时被调用。但即使是打印出串口的校准值也是垃圾。如果我在主循环中编写所有这些代码,它会按预期工作。
任何帮助理解出了什么问题将不胜感激。谢谢