AI智能
改变未来

Arduino双色LED实验

一、组件
Arduino Uno主板、USB数据线、双色LED模块、面包板、跳线

二、功能
LED红灯从亮到灭,绿灯从灭到亮;
LED红灯从灭到亮,绿灯从亮到灭;
循环。

三、原理


脉冲宽度调制与analogWrite函数。通过for循环使analogWrite函数传入值降低,使得LED由亮变灭。

int redPin = 11;    // select the pin for the red LEDint greenPin = 10;    // select the pin for the blueLEDint val = 0;void setup(){pinMode(redPin, OUTPUT); //set redPin as OUTPUTpinMode(greenPin, OUTPUT);//set greenPin as OUTPUTSerial.begin(9600);}/******************************************************/void loop(){for(val=255; val>0; val--) //{analogWrite(redPin, val); //red value decreaseanalogWrite(greenPin, 255-val);//green value increaseSerial.println(val, DEC);//print the val on in serial monitordelay(30); //delay 30ms}for(val=0; val<255; val++){analogWrite(redPin, val); //red value increaseanalogWrite(greenPin, 255-val);//green value decreaseSerial.println(val, DEC);//print the val on in serial monitordelay(30); //delay 30ms}}/********************************************************/
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Arduino双色LED实验