2016-07-28 9 views
-2

Mein Arduino funktioniert nach einer Weile nicht mehr. Hier ist mein Code:Arduino funktioniert nicht nach einer Weile

//Pin.h is my header file 

#ifndef Pin_h 
#define Pin_h 

class Pin{ 
    public: 
    Pin(byte pin); 
    void on();//turn the LED on 
    void off();//turn the LED off 
    void input();//input PIN 
    void output();//output PIN 

    private: 
    byte _pin;  
}; 

#endif 


//Pin.cpp is my members definitions 
#include "Arduino.h" 
#include "Pin.h" 

Pin::Pin(byte pin){//default constructor 
    this->_pin = pin; 
} 

void Pin::input(){ 
    pinMode(this->_pin, INPUT); 
} 

void Pin::output(){ 
    pinMode(this->_pin, OUTPUT); 
} 

void Pin::on(){ 
    digitalWrite(this->_pin, 1);//1 is equal to HIGH 
} 

void Pin::off(){ 
    digitalWrite(this->_pin, 0);//0 is equal to LOW 
} 


//this is my main code .ino 
#include "Pin.h" 

Pin LED[3] = {//array of objects 
Pin(11), 
Pin(12), 
Pin(13) 
}; 

const byte MAX = sizeof(LED); 

//MAIN PROGRAM---------------------------------------------------------------------------------- 
void setup() { 
    for(int i = 0; i < MAX; i++){ 
     LED[i].output(); 
    }//end for loop initialize LED as output 
}//end setup 

int i = 0; 

void loop() { 
    for(i = 0; i < 3; i++){ 
    LED[i].on(); 
    delay(1000); 
    } 

    for(i = 3; i >= 0; i--){ 
    LED[i].off(); 
    delay(1000); 
    } 

}//end loop 
//see class definition at Pin.h 
//see class members at Pin.cpp 

My Arduino nicht mehr funktioniert, wenn ich zwei for-Schleifen in der Leere Loop-Funktion verwenden, aber wenn ich diesen Code unten in meinem Haupt verwenden, funktioniert es gut. Warum stoppt mein Arduino nach einer Weile, wenn ich for-Schleifen verwende?

void loop() { 
    LED[0].on(); 
    delay(1000); 

    LED[1].on(); 
    delay(1000); 

    LED[2].on(); 
    delay(1000); 

    LED[2].off(); 
    delay(1000); 

    LED[1].off(); 
    delay(1000); 

    LED[0].off(); 
    delay(1000); 
}//end loop 
+0

Überprüfen Sie immer die Abschriftenvorschau. – LogicStuff

Antwort

0

Dies liegt daran, Sie mit i Ihre zweite Schleife start = 3 ...

void loop() { 
    for(i = 0; i < 3; i++){ 
    LED[i].on(); 
    delay(1000); 
    } 

    for(i = 3; i >= 0; i--){ 
    LED[i].off(); // This causes a crash on the first run LED[3] is out of range... 
    delay(1000); 
    } 

}//end loop 

Auch Sie wollen diese '3' ersetzen durch 'MAX', also wenn Sie die Größe ändern Sie müssen es nicht überall neu schreiben ...

void loop() { 
    for(i = 0; i < MAX; i++){ 
    LED[i].on(); 
    delay(1000); 
    } 

    for(i = MAX - 1; i >= 0; i--){ 
    LED[i].off(); 
    delay(1000); 
    } 

}//end loop 
+0

oh mein Gott. Ich bin so ein Idiot. danke .. .. danke ein Haufen .. ich schätze es wirklich .. –

+0

Markieren Sie diesen Beitrag als die Antwort dann, damit andere mit dem gleichen Problem schneller lösen können –

Verwandte Themen