Arduino not working after a while -
my arduino stops working after while. here's code:
//pin.h header file #ifndef pin_h #define pin_h class pin{ public: pin(byte pin); void on();//turn led on void off();//turn led off void input();//input pin void output();//output pin private: byte _pin; }; #endif //pin.cpp 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 equal high } void pin::off(){ digitalwrite(this->_pin, 0);//0 equal low } //this 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 = 0; < max; i++){ led[i].output(); }//end loop initialize led output }//end setup int = 0; void loop() { for(i = 0; < 3; i++){ led[i].on(); delay(1000); } for(i = 3; >= 0; i--){ led[i].off(); delay(1000); } }//end loop //see class definition @ pin.h //see class members @ pin.cpp
my arduino stops working when use 2 loops inside void loop function, if use code below in main, works fine. why arduino stop after while when using loops?
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
this because start second loop = 3...
void loop() { for(i = 0; < 3; i++){ led[i].on(); delay(1000); } for(i = 3; >= 0; i--){ led[i].off(); // causes crash on first run led[3] out of range... delay(1000); } }//end loop
also, want replace these '3' 'max', when change size, don't have rewrite everywhere...
void loop() { for(i = 0; < max; i++){ led[i].on(); delay(1000); } for(i = max - 1; >= 0; i--){ led[i].off(); delay(1000); } }//end loop
Comments
Post a Comment