呼吸灯闪烁,课后作业代码:
- //analogWrite()函数支持以下引脚: 3, 5, 6, 9, 10, 11。
- int r = 9;
- int g = 11;
- int b = 10;
-
- void setup() {
- pinMode(r,OUTPUT);
- pinMode(g,OUTPUT);
- pinMode(b,OUTPUT);
- }
-
- void loop() {
- Serial.begin(9600);
- colorRGB(random(0,255),random(0,255),random(0,255));
- delay(1000);
- }
- void colorRGB(int red,int green,int blue){
- fadeIn(1000,5,red,r);
- fadeIn(1000,5,green,g);
- fadeIn(1000,5,blue,b);
- fadeOut(1000,5,red,r);
- fadeOut(1000,5,green,g);
- fadeOut(1000,5,blue,b);
- }
-
- void fadeIn(unsigned int time,int incnum,int color,int port){
- for(byte i = 0; i< color; i+= incnum){
- analogWrite(port,constrain(i,0,255));
- delay(time/(255/5));
- }
- }
-
- void fadeOut(unsigned int time,int incnum,int color,int port){
- for(int i = color; i > 0; i-= incnum){
- analogWrite(port,constrain(i,0,255));
- delay(time/(255/5));
- }
- }
-
复制代码
|