c++ - Arduino:Setup() 不会启动

标签 c++ c class arduino

我正在为学校 build 一个机器人,它需要能够使用 3 个 QRE1113 线传感器检测线。 (http://www.sparkfun.com/products/9454) 我创建了 4 个库,其中两个用于驱动(Motor() 和 Driver()),它们工作正常。现在我创建了库 Linesensor 和 Eye,它们造成了一些麻烦。当我想使用这些库时,setup() 函数将不起作用。甚至不打开 LED。似乎是什么问题?

主文件:

#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() 方法中为我的对象而不是它们的构造函数初始化事物来解决我的问题。然后我在设置 Serial 对象后调用 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()
}

在方法而不是构造函数中构造对象总是有点奇怪。但由于它是微 Controller 编程而不是常见的计算机程序,我想采用更实用的方法有时是最简单的。

如果您没有更好的答案,您仍然可以尝试。也许仅对您的 Eye 库执行此操作就足够了,因为您曾说过您对 Motor 类没有任何问题。

关于c++ - Arduino:Setup() 不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9683869/

相关文章:

c++ - 谷歌测试框架参数化装置

根据程序状态更改退出行为

python - 类装饰器自动更新磁盘上的属性字典?

java - 容器类的习惯用法

java - 在OOP中,写子类的时候是不是要改掉原来的main方法代码?什么代替?

c++ - 语法意外失败 boost::spirit 语法定义

c++ - 在函数内部声明全局对象 (C++)

c++ - 堆损坏和 F12 问题

c - 将动态创建的结构体作为非指针对象返回

arrays - 数组中的困惑