C++ Arduino:Setup()赢了';开始
我正在为学校建造一个机器人,它需要能够使用3个QRE1113线路传感器检测线路。(http://www.sparkfun.com/products/9454)我创建了4个库,其中两个用于驱动(Motor()&Driver()),它们工作正常。现在我创建了Linesensor和Eye库,它们会引起一些麻烦。当我想使用这些库时,setup()函数不起作用。甚至连LED灯都不亮。有什么问题吗 主文件:C++ Arduino:Setup()赢了';开始,c++,c,class,arduino,C++,C,Class,Arduino,我正在为学校建造一个机器人,它需要能够使用3个QRE1113线路传感器检测线路。(http://www.sparkfun.com/products/9454)我创建了4个库,其中两个用于驱动(Motor()&Driver()),它们工作正常。现在我创建了Linesensor和Eye库,它们会引起一些麻烦。当我想使用这些库时,setup()函数不起作用。甚至连LED灯都不亮。有什么问题吗 主文件: #include "Motor.h" #include "Driver.h" #include "L
#include "Motor.h"
#include "Driver.h"
#include "Lichtsensor.h"
#include "Eye.h"
Motor motor1(5, 4, true);
Motor motor2(6, 7, false);
Driver driver(motor1, motor2);
Eye eye1;
void setup(){
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(9600);
Serial.println("#################################################");
Serial.println("# This sketch communicates with the arduino and #");
Serial.println("# makes the robot drive, and react to a sensor. #");
Serial.println("#################################################\n");
}
void loop(){
if (eye1.getDikkeLijn() == true) {
Serial.println("Lijn");
}
else {
Serial.println("Niks");
}
delay(1000);
}
眼睛图书馆:
/*
Controls Lichtsensors
*/
#ifndef Eye_h
#define Eye_h
#include "Arduino.h"
#include "Lichtsensor.h"
class Eye
public:
Eye();
Eye(Lichtsensor l1, Lichtsensor l2, Lichtsensor l3);
boolean getDikkeLijn();
boolean getDunneLijn();
private:
Lichtsensor _l1;
Lichtsensor _l2;
Lichtsensor _l3;
};
#endif
和线路传感器:
/*
Library to get values from a light sensor
*/
#ifndef Lichtsensor_h
#define Lichtsensor_h
#include "Arduino.h"
class Lichtsensor {
public:
Lichtsensor();
Lichtsensor(int analogPin);
int getCalibreerWaarde();
int getLichtWaarde();
boolean isDonker();
private:
int _lichtCalibreerWaarde;
int _analogPin;
};
#endif
我记得在初始化setup()之外声明的对象的构造函数时遇到问题。我不知道为什么,我承认我没有调查过这个问题。但是我同意在程序启动之前初始化太多东西的想法 我不能保证这就是解决方案(也不能真正解释原因),但我通过在init()方法中为我的对象而不是它们的构造函数初始化东西来解决问题。在设置好串行对象之后,我调用了init()方法int my setup()。诸如此类:
#include "Motor.h"
#include "Driver.h"
#include "Lichtsensor.h"
#include "Eye.h"
Motor motor1; // I do not use any more my constructor
Motor motor2; // I do not use any more my constructor
Driver driver; // I do not use any more my constructor
Eye eye1; // I do not use any more my constructor
void setup(){
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.begin(9600);
Serial.println("#################################################");
Serial.println("# This sketch communicates with the arduino and #");
Serial.println("# makes the robot drive, and react to a sensor. #");
Serial.println("#################################################\n");
motor1.init(5, 4, true); // My object is initialized here
motor2.init(6, 7, false); // My object is initialized here
driver.init(motor1, motor2); // My object is initialized here
eye1.init()
}
在方法中而不是构造函数中构造对象总是有点奇怪。但是,由于它是微控制器编程,而不是普通的计算机程序,我想采取更实用的方法有时是最简单的
如果你没有更好的答案,你仍然可以尝试。也许只为你的眼睛库这样做就足够了,因为你告诉过你,你的运动类没有问题。看起来我们的类太多了,arduino无法再处理它。我认为很多类没有错,或者初始化了太多的东西。在我的情况下,我有很多由我定义的类(超过5个类)。我的问题在于我定义类的方式。例如,一个错误是我在头文件中定义函数时,在cpp文件中没有用类名作为前缀。在代码语言中:
void begin(); // this is a function defined in the header file
void begin(){//this is in the cpp file and this is wrong definition
}
void NameOfTheClass::begin(){ // this is the correct way in the cpp file
//code goes here
}
试着这样做,我得到的信息是:错误:“类眼”没有名为“init”的成员。你必须自己实现init()方法:)例如,创建一个init()方法,将构造函数中的内容放在其中,然后删除类的构造函数。好的,但是你的digitalWrite(13,高)是否至少打开了LED?如果是,您的Eye库中可能存在实现问题。如果不是,我就没有主意了。@Vincenthiriblen假设您将代码从构造函数移动到名为init的方法。是你干的吗?您能发布所有当前代码吗?这个问题是否与Arduino板上有限的内存有关?