硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个,电位器一个,LED灯一个 软件平台:WIN7操作系统,android4.0或android4.4系统 实验目标:通过PWM实现LED灯的渐亮渐灭效果。 打开arduino的IDE开发工具,依次点击文件->示例-> 03.Analog->Fading,Fading的示例程序将会被打开,其源码如下: - int ledPin = PWM0; // LED connected to digital pin PWM0
-
- void setup() {
- // nothing happens in setup
- }
-
- void loop() {
- // fade in from min to max in increments of 5 points:
- for (int fadeValue = 0 ; fadeValue <= 4095; fadeValue += 45) {
- // sets the value (range from 0 to 4095):
- analogWrite(ledPin, fadeValue);
- // wait for 30 milliseconds to see the dimming effect
- delay(30);
- }
-
- // fade out from max to min in increments of 5 points:
- for (int fadeValue = 4095 ; fadeValue >= 0; fadeValue -= 45) {
- // sets the value (range from 4095 to 0):
- analogWrite(ledPin, fadeValue);
- // wait for 30 milliseconds to see the dimming effect
- delay(30);
- }
- }
复制代码 将LED的正极连5V电源,负极接PWM0,运行程序,结合程序观看LED灯的效果,同时修改程序中参数反复实验,观察LED灯的状态。
|