Arduino过了一会儿就不工作了
我的arduino在一段时间后停止工作。 这是我的密码:Arduino过了一会儿就不工作了,arduino,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 p
//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
这是因为你以i=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
void循环(){
对于(i=0;i<3;i++){
LED[i].on();
延迟(1000);
}
对于(i=3;i>=0;i--){
LED[i].off();//这会在第一次运行时导致崩溃LED[3]超出范围。。。
延迟(1000);
}
}//端环
此外,您希望将这些“3”替换为“MAX”,因此当您更改大小时,不必到处重写它
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
void循环(){
对于(i=0;i=0;i--){
LED[i].off();
延迟(1000);
}
}//端环
始终查看降价预览。哦,天哪。我真是个白痴。非常感谢。非常感谢。。我真的很感激。把这篇文章作为答案,这样其他有同样问题的人就能更快地解决它
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