|
大家好,我是個Arduino新手,最近看到PWM的文章,想寫一個可以控制週期的PWM我是看到http://yehnan.blogspot.tw/2012/02/arduino_16.html,葉難大大的呼吸燈文章
想進而修改,下面是我的程式碼:
void manualPwm(double, double);
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
void loop() {
double brightness = 0, fade = 0.01;
digitalWrite(4, HIGH);
manualPwm(10000, brightness);
digitalWrite(4, LOW);
brightness = brightness + fade;
if(brightness<=0 || brightness>=1)fade = -fade;
}
void manualPwm(int cycle, double dutyCycle){//unit: us
if(dutyCycle == 0){
digitalWrite(3, LOW);
delayMicroseconds(cycle);
}
else if(dutyCycle == 1){
digitalWrite(3, HIGH);
delayMicroseconds(cycle);
}
else{
digitalWrite(3, HIGH);
delayMicroseconds((int)(dutyCycle*cycle));
digitalWrite(3, LOW);
delayMicroseconds((int)((1-dutyCycle)*cycle));
}
}
但我輸出都維持再0,我另外接一個LED在pin4,發現他一直再manualPwm這個副程式沒有回傳回來,加了return也一樣
一直百思不解,求大大回答 |
|