2013-01-11 5 views
0

다음 코드를 실험하고 있습니다. 내 LED가 2 초/2000ms 이상 천천히 사라 지길 바래요. 이 코드는 작성한대로 루프 digitalWrite(2,HIGH)digitalWrite(2,LOW)과 구별 할 수 없습니다. 나는 프로세서가 페이드 지속 시간이 너무 느리다는 것을 알기 위해 255 단계를 빠르게 지나치는 것으로 생각한다.Arduino FOR 루프를 늦추십시오.

void setup() {      # set pin 2 as output 
    pinMode = (2,OUTPUT); 
} 

void loop() { 
    (for int fi=0;fi<255;fi++) { # fade IN pin2 from 0-254 
    analogWrite(2,fi) 
    } 
    (for int fo=0;fo<255;fo--) { # fade OUT pin2 from 0-254 
    analogWrite(2,fo) 
    } 
} 

페이딩을 늦추고 페이드 아웃하는 방법에 대한 제안이나 두 가지 제안을 원합니다. 나의 초기 생각은 각각의 단계에 delay(8)을 추가하는 것이었지만 부드럽고 선형적인 페이드보다 단계적인 전력 증가가 더 많이 나타 났습니까? 그것은 반드시 최선의 관행입니까 아니면보다 수용 가능한 방식입니까?

다른 아이디어?

답변

2

예, 당신이 마이크로 컨트롤러와 진정한 선형 페이드를 만들 수 없습니다 것을 올바른지 : 그것으로 당신은 다음과 같은 몇 가지 논리를 할 수있는 온라인왔다. 출력에는 항상 최소 단계 크기 또는 해상도가 있습니다. 당신에게 질문은 "PWM이 단계를 인식하지 못하는 충분한 해상도를 가지고 있습니까?"입니다.

PWN은 255 단계로 0-100 %를 출력 할 수 있습니다. 이것은 모니터의 각 픽셀의 8 비트 해상도와 동일합니다. 어떤 모니터 보정 페이지를 보면 아마도 8 비트 해상도가 본질적으로 부드러운 스케일로 나타날 것이라고 확신 할 수 있습니다. Example monitor gradient

여기 4 초마다 LED가 켜지고 꺼지는 샘플이 있습니다. 여기서는 백그라운드에서 작업을 수행하는 방법을 보여줍니다.

4 초로 켜기로 변경하면 깜박임 지연없이 Arduino 문서에 설명 된 방법이 사용됩니다. 각 루프는 마지막 변경 이후 경과 된 시간이 지정된 간격을 초과하는지 확인합니다. 그렇다면 상태를 변경하고 현재 시간을 표시합니다.

페이드는 약간 다르게 수행됩니다. 다음 변경 사항이 발생할 시간이 남습니다. 루프를 돌 때마다 다음 동작을 할 시간인지 확인합니다. 예인 경우 변경하고 다음 변경시기를 저장합니다.

/* unfortunately standard LED on pin 13 does not have PWM 
You will need to connect an LED and resistor */ 
const int pin = 3; 

/* this is your step time between changes in light 
output in milliseconds */ 
const unsigned int tstep = 8; 

/* this is the time then next change can be made. 
The change will happen at or after this value. */ 
unsigned int tnext = 0; 

/* this is the current and target light level 
The output will slowly ramp until the current value 
meets the target. So to request that the change start, 
the code just sets a new target. */ 
unsigned int target = 0; 
unsigned int current = 0; 

/* These are from Blink without Delay 
Shows another way to execute delays. */ 
unsigned long previousMillis = 0; 
unsigned long interval = 4000; 

void setup() { 
    pinMode(pin,OUTPUT); 
    tnext = millis() + tstep; 
    analogWrite(pin, current); 
} 

/* something in the code calls this 
to request a change in the LED state 
Pass what you want to be the new value 
and the LED will slowly change to that value. */ 
void newTarget(int value) { 
    tnext = millis() + tstep; 
    target = value; 
} 

/* call this frequently to update the LED 
If the time of the next action has been reached, 
execute the change and setup when the following 
change will occur. */ 
void driveLed() { 
    unsigned int tnow = millis(); 

    if(target != current) { 
     if(tnow >= tnext) { 
     if(target > current) { 
      current += 1; 
     } 
     else { 
      current -= 1; 
     } 
     analogWrite(pin, current); 
     tnext += tstep; 
     } 
    } 
} 


/* Note that the main loop has no delays. 
Execution will spin rapidly through this, most times 
checking and finding nothing to to. 
You would add your other functionality here, and this 
LED would continue to happen. As long as you don't pack 
anything that takes on the order of 8 ms, you won't notice 
a change in the LED fade behavior. 
void loop() { 
    unsigned int tnow = millis(); 

    // some other logic here would decide when to change the LED state 
    // For this sample, just toggle every 4 seconds 
    if((tnow - previousMillis) >= interval) {   
     if(0 == target) { 
     newTarget(255); 
     } 
     else { 
     newTarget(0); 
     } 
     previousMillis = tnow; 
    } 

    driveLed(); 
} 
2

나는 마이크로 당신이 쓰기 발행시기를 세밀하게 제어 얻을 기능을 사용합니다 :

http://arduino.cc/en/Reference/Micros

그것은 지연이 아니라 바로 보드 경과 한 마이크로 초 수를 반환합니다

setup() 
{ 
    t0 = micros() 
} 


loop() 
{ 
    t1 = micros(); 
    dt = t1 - t0; 
    if (dt > SOME_SMALL_TIME_DELTA) 
    { 
     increment_or_decrement_led_value_by_one(); 
     t0 = t1 
    } 
}