九鼎创展论坛

标题: arduino ibox项目实战22-AnalogWriteMega实验 [打印本页]

作者: armeasy    时间: 2014-10-28 13:53
标题: arduino ibox项目实战22-AnalogWriteMega实验
硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个,LED灯三个
软件平台:WIN7操作系统,android4.0android4.4系统
实验目标:通过PWM0PWM1PWM2同时实现三盏灯的渐变效果。
打开arduino的IDE开发工具,依次点击文件->示例-> 03.Analog->AnalogWriteMega,AnalogWriteMega的示例程序将会被打开,其源码如下:
  1. const int lowestPin = PWM0;
  2. const int highestPin = PWM2;

  3. void setup() {
  4.   // set pins PWM0 through PWM2 as outputs:
  5.   for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
  6.     pinMode(thisPin, OUTPUT);
  7.   }
  8. }

  9. void loop() {
  10.   // iterate over the pins:
  11.   for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
  12.     // fade the LED on thisPin from off to brightest:
  13.     for (int brightness = 0; brightness < 255; brightness++) {
  14.       analogWrite(thisPin, brightness);
  15.       delay(2);
  16.     }
  17.     // fade the LED on thisPin from brithstest to off:
  18.     for (int brightness = 255; brightness >= 0; brightness--) {
  19.       analogWrite(thisPin, brightness);
  20.       delay(2);
  21.     }
  22.     // pause between LEDs:
  23.     delay(100);
  24.   }
  25. }
复制代码
       运行程序,通过调节程序中相关参数,实现PWM脉宽变化,最终控制三盏灯的LED渐变效果。






欢迎光临 九鼎创展论坛 (http://bbs.9tripod.com/) Powered by Discuz! X3.2