使用类时未初始化Arduino上的LED引脚

时间:2019-03-23 15:24:25

标签: arduino arduino-c++

我正在尝试在交通信号灯程序中实现类。我面临的问题是指示灯不闪烁,并且继承的类似乎不起作用。我该如何解决?

class Sensors {
    public:
        Sensors(int echopin, int trigpin);
    Sensors();
    void init();
    double light();
    double sensor();
    // protected:
    int ECHOPIN;
    int TRIGPIN;
};

class Mode: public Sensors
//Add the VARIABLES into the CLASSES from the FUNCTIONS
{
    public: Mode(int gled, int yled, int rled, int Delay1);
    void init();
    void mode1();
    void mode2();
    void mode3();
    double sensor() {
        Sensors::sensor();
    }
    private: int Gled;
    int Yled;
    int Rled;
    int Delay;
};
Sensors sense(3, 2);
Mode mode(13, 12, 11, 2000);
void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    sense.init();
    mode.init();
}

void loop() {
    double measure;
    double lightValue;
    double MAXDISTANCE = 10;
    double MAXLIGHT = 140;

    measure = sense.sensor();
    lightValue = sense.light();
    if (measure < MAXDISTANCE) {
        mode.mode2();
    } else if (lightValue < MAXLIGHT) {
        while (lightValue < MAXLIGHT) {
            mode.mode3();
            lightValue = sense.light();
        }
    } else {
        mode.mode1();
    }
}

Sensors::Sensors(int echopin, int trigpin) {
    ECHOPIN = echopin;
    TRIGPIN = trigpin;
}
Sensors::Sensors() {
    ECHOPIN;
    TRIGPIN;
}
double Sensors::light() {
    //LIGHT SENSOR
    int sensorPin = A0;
    unsigned int value = 0;
    value = analogRead(sensorPin);
    Serial.print("Light value is: ");
    Serial.println(value);
    return value;
}

double Sensors::sensor() {
    //Measure the distance for the RANGE FINDER
    double distance;
    double Time;

    digitalWrite(TRIGPIN, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIGPIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIGPIN, LOW);
    Time = pulseIn(ECHOPIN, HIGH);
    distance = (Time * 340) / 20000;
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.print(" cm \n");
    return distance;
}

void Sensors::init() {
    pinMode(ECHOPIN, INPUT);
    pinMode(TRIGPIN, OUTPUT);
}
Mode::Mode(int gled, int yled, int rled, int Delay1) {
    int Gled = gled;
    int Yled = yled;
    int Rled = rled;
    int Delay = Delay1;
}

void Mode::init() {
    pinMode(Gled, OUTPUT);
    pinMode(Yled, OUTPUT);
    pinMode(Rled, OUTPUT);
}

void Mode::mode1() {
    //First Mode
    digitalWrite(Gled, LOW);
    digitalWrite(Rled, HIGH); //RED ON
    delay(Delay);
    digitalWrite(Rled, LOW);
    digitalWrite(Yled, HIGH); //YELLOW ON
    Delay -= 1000;
    delay(Delay);

    digitalWrite(Yled, LOW);
    digitalWrite(Gled, HIGH); //GREEN ON
    Delay += 1000;
    delay(Delay);
    digitalWrite(Yled, HIGH); //YELLOW ON
    digitalWrite(Gled, LOW);
    Delay -= 1000;
    delay(Delay);
}

void Mode::mode2() {
    int DELAY = 100;
    int Y_LOOP = 10;
    int buzz = 4;

    pinMode(buzz, OUTPUT);
    for (int i = 0; i < Y_LOOP; i++) {

        tone(buzz, 20);
        digitalWrite(Yled, HIGH);
        delay(DELAY);
        digitalWrite(Yled, LOW);
        delay(DELAY);
    }
    noTone(buzz);
}

void Mode::mode3() {
    double measure;

    delay(1000);
    measure = sensor();
    if (measure < 10) {
        digitalWrite(Rled, LOW);
        digitalWrite(Gled, HIGH);
    } else {
        digitalWrite(Rled, HIGH);
        digitalWrite(Gled, LOW);
    }
    delay(1000);
}

我希望传感器方法返回一个距离,但这只是第一次。当它被称为继承方法时,它将返回0。

1 个答案:

答案 0 :(得分:0)

此代码可以简化为

struct foo
{
    int myX;

    foo(int x)
    {
        int myX = x;
    }
};

int main()
{
    foo f(42);
    //  f.myX is still garbage because in constuctor you initialize
    //  a local variable myX instead of object field
    return 0;
}

因此,您应该改为在成员初始化程序列表中初始化类字段:

foo(int x): myX{x} 
{
     // empty
}