硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个,LED灯三个 软件平台:WIN7操作系统,android4.0或android4.4系统 实验目标:通过PWM0,PWM1,PWM2同时实现三盏灯的渐变效果。 打开arduino的IDE开发工具,依次点击文件->示例-> 03.Analog->AnalogWriteMega,AnalogWriteMega的示例程序将会被打开,其源码如下: - const int lowestPin = PWM0;
- const int highestPin = PWM2;
-
- void setup() {
- // set pins PWM0 through PWM2 as outputs:
- for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
- pinMode(thisPin, OUTPUT);
- }
- }
-
- void loop() {
- // iterate over the pins:
- for (int thisPin = lowestPin; thisPin <= highestPin; thisPin++) {
- // fade the LED on thisPin from off to brightest:
- for (int brightness = 0; brightness < 255; brightness++) {
- analogWrite(thisPin, brightness);
- delay(2);
- }
- // fade the LED on thisPin from brithstest to off:
- for (int brightness = 255; brightness >= 0; brightness--) {
- analogWrite(thisPin, brightness);
- delay(2);
- }
- // pause between LEDs:
- delay(100);
- }
- }
复制代码 运行程序,通过调节程序中相关参数,实现PWM脉宽变化,最终控制三盏灯的LED渐变效果。
|