arduino平台项目开发

  1. This example shows how to fade an LED on pin 9
  2. using the analogWrite() function.
  3.  
  4. The analogWrite() function uses PWM, so if
  5. you want to change the pin you're using, be
  6. sure to use another PWM capable pin. On most
  7. Arduino, the PWM pins are identified with 
  8. a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
  9.  
  10. This example code is in the public domain.
  11. */
  12.  
  13. int led = 9;           // the PWM pin the LED is attached to
  14. int brightness = 0;    // how bright the LED is
  15. int fadeAmount = 5;    // how many points to fade the LED by
  16.  
  17. // the setup routine runs once when you press reset:
  18. void setup() {
  19.   // declare pin 9 to be an output:
  20.   pinMode(led, OUTPUT);
  21. }
  22.  
  23. // the loop routine runs over and over again forever:
  24. void loop() {
  25.   // set the brightness of pin 9:
  26.   analogWrite(led, brightness);
  27.  
  28.   // change the brightness for next time through the loop:
  29.   brightness = brightness + fadeAmount;
  30.  
  31.   // reverse the direction of the fading at the ends of the fade:
  32.   if (brightness == 0 || brightness == 255) {
  33.     fadeAmount = -fadeAmount ;
  34.   }
  35.   // wait for 30 milliseconds to see the dimming effect
  36.   delay(30);
  37. }

为了使LED的熄灭和点亮出现渐变的效果,需要逐渐将PWM值从0(一直熄灭)增加255(一直点亮),然后再回到0完成循环。在下面的模板例程中,PWM值使用brightness变量来设置。循环每执行一次,变量fadeAmount的值增加。

如果brightness值处于是在任一极端(0或255),则fadeAmount改变为它的符号。换句话说,如果fadeAmount是5,那么它会被设为-5。如果它是-5,那么它会被设为5。下个循环时,也会更改fadeAmount的符号。

  1. 实验效果图:

 

v

 

 

 

 

 

你可能感兴趣的:(系统架构)