猜测原因:受限于人眼感受和LED特性影响,PWM是有一个最佳的调节范围的,超出范围外将不能出现预料中的变化;另外怀疑是硬件bug
以本人手上的LED测试为例20000ms周期为例:
最佳亮灯周期为1%~18%个周期,亮灯周期低于1%(0.01),或高于18%(0.18),都会表现为中亮。
测试代码如下:
#define R (3)
#define G (4)
#define B (2)
#define CYCLE 20000
void setup(){
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Serial OK");
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
}
float percent=0.0;
boolean state_up=false;
float minper=0.01;//when min is too small(smaller than stepp),it work incorrectly;
float maxper=0.18;//when it's over 0.18,it work incorrectly;
float stepp=0.001;//control stepp , control speed
//G:0.01~0.18 stepp=0.001
int LED=B;
void loop(){
if(state_up){
if(percent>maxper){
state_up=false;