硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个,串联有电阻的LED灯外设一个 软件平台:WIN7操作系统,android4.0或android4.4系统 打开arduino的IDE开发工具,依次点击文件->示例->01.Basics->DigitalReadSerial,DigitalReadSerial的示例程序将会被打开,其源码如下: - int led = PWM0; // the pin that the LED is attached to PWM0 = 5.
- int brightness = 0; // how bright the LED is
- int fadeAmount = 80; // how many points to fade the LED by
-
- // the setup routine runs once when you press reset:
- void setup() {
- // declare pin 5(PWM0) to be an output:
- pinMode(led, OUTPUT);
- }
-
- // the loop routine runs over and over again forever:
- void loop() {
- // set the brightness of pin 5(PWM0):
- analogWrite(led, brightness);
-
- // change the brightness for next time through the loop:
- brightness = brightness + fadeAmount;
-
- // reverse the direction of the fading at the ends of the fade:
- if (brightness <= 0 || brightness >= 4095) {
- fadeAmount = -fadeAmount ;
- }
- // wait for 30 milliseconds to see the dimming effect
- delay(30);
- }
复制代码 从源码可以看出,程序通过pwm0口控制LED灯,通过analogWrite函数的传入参数brightness的渐变,实现PWM脉宽的调制,最终实现LED灯的渐亮渐灭的效果。 将LED灯外设的负极接到PWM0口,正级接到3.3V电源口,运行arduino,可以发现,LED灯确实实现了渐亮渐灭的效果。
|