2016-07-28 4 views
-2

잠시 후 내 arduino 작동이 멈 춥니 다. 다음은 내 코드입니다 :잠시 후 Arduino가 작동하지 않습니다.

//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 

나는 빈 공간 루프 기능 내부 루프를 두를 사용할 때 내 아두 이노가 작동을 멈 춥니 다,하지만 난 내 주요 아래이 코드를 사용하는 경우, 그것은 잘 작동합니다. for 루프를 사용할 때 arduino가 잠시 멈춘 이유는 무엇입니까? 당신은 내가 = 3 번째 루프를 시작하기 때문에

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

항상 미리보기 미리보기를 확인하십시오. – LogicStuff

답변

0

이것은 당신이 크기를 변경할 그렇게 할 때, 당신이 '3'으로 'MAX'를 대체 할, 또한 ...

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 

입니다 어디서나 다시 쓸 필요가 없습니다 ...

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

오 마이 갓. 그래서 바보. 고마워 .. .. 한꺼번에 고마워. 정말 고마워 .. –

+0

이 게시물을 답으로 표시하면 같은 문제가있는 다른 사람들이 더 빨리이 문제를 해결할 수 있습니다. –

관련 문제