마음에 와서 세 가지 옵션 :이 옵션은 한 번 코드를 실행 while(true)
void loop(){
//the code you want to run once here,
//e.g., If (blah == blah)...etc.
while(1) //last line of main loop
}
동등하게 좋은 while(1)
... 나에
1) 끝 void loop()
... 그리고 그런 다음 Ard를 으로 끝없는 "보이지 않는"루프로 시작합니다. 아마도 에가는 가장 좋은 방법은 아니지만, 외관상으로는 끝내게됩니다.
Ard는 에서 끝없는 원을 그리며 전류를 계속 끌어 올 것입니다 ... 아마도 타이머의 종류를 설정할 수 있습니다. 많은 시간이 지나면 Ard가 잠자기 상태가됩니다. 분 등등 루핑 ... 그냥 생각 ... 저기에 확실히 다양한 수면 라이브러리가 있습니다 ... 자세한 내용은 예 : Monk, Programming Arduino : 다음 단계, 페이지 85-100 을 참조하십시오.
두 번째) 조건부 컨트롤 구조를 사용하여 두 번째 패스에서 초기 테스트가 실패하는 "메인 루프 중지"함수를 만듭니다.
이것은 종종 전역 변수를 선언하고 "주 루프 중지"기능을 사용하면 종료시 변수 의 값을 토글해야합니다. 예 :
boolean stop_it = false; //global variable
void setup(){
Serial.begin(9600);
//blah...
}
boolean stop_main_loop(){ //fancy stop main loop function
if(stop_it == false){ //which it will be the first time through
Serial.println("This should print once.");
//then do some more blah....you can locate all the
// code you want to run once here....eventually end by
//toggling the "stop_it" variable ...
}
stop_it = true; //...like this
return stop_it; //then send this newly updated "stop_it" value
// outside the function
}
void loop{
stop_it = stop_main_loop(); //and finally catch that updated
//value and store it in the global stop_it
//variable, effectively
//halting the loop ...
}
허용되는 경우 매우 좋지 않을 수도 있지만 작동합니다.
Ard를 다른 보이지 않는 "보이지 않는"루프로 시작하지만이 번 if(stop_it == false)
상태를 반복적으로 확인하는 경우가 발생합니다. stop_main_loop()
처음에는 처음부터 매번 통과하지 못합니다.
3rd) 다시 한 번 전역 변수를 사용할 수 있지만 멋진 "stop main loop"기능 대신 간단한 if (test == blah){}
구조를 사용할 수 있습니다.
boolean start = true; //global variable
void setup(){
Serial.begin(9600);
}
void loop(){
if(start == true){ //which it will be the first time through
Serial.println("This should print once.");
//the code you want to run once here,
//e.g., more If (blah == blah)...etc.
}
start = false; //toggle value of global "start" variable
//Next time around, the if test is sure to fail.
}
확실히 무한 메인 루프 하지만이 세 가지뿐만 아니라 이미 시작 가야 언급 한 것과 같은를 성가신 "중지"할 수있는 다른 방법이 있습니다.
프로세서를 어떻게 중단합니까? – Brent
@Brent : 인터럽트를 비활성화하고 프로세서를 절전 상태로 전환합니다. 무한 루프로 감싸서 실행을 계속할 수 있습니다. –
"return"은 그렇게 할 것이라고 생각합니다. –