查看: 12440|回复: 28

[教程] 呼吸灯实验

[复制链接]
  • TA的每日心情
    郁闷
    2018-12-6 22:21
  • 签到天数: 48 天

    [LV.5]常住居民I

    发表于 2018-3-5 21:10 | 显示全部楼层 |阅读模式
    本文节选自《Arduino程序设计基础》

    之前的章节已经介绍了多种方法控制LED,但单是开关LED未免显得太过单调了,还可以尝试用analogWrite() 函数输出PWM波,来制作一个带呼吸效果的LED灯。

    实验所需材料
    Arduino Uno、面包板、LED一个、220Ω电阻一个

    连接示意图
    1111.jpg


    原理图
    2222.png


    如图所示,实验中我们将LED连接到了带PWM功能的D9引脚。
    可以在 Arduino IDE菜单>文件>示例>03.Analog>Fading 打开呼吸灯示例程序,程序如下:


    kittenblock中小学创客名师推荐的图形化编程软件

    /*
    Fading
    通过analogWrite() 函数实现呼吸灯效果
    */
    
    int ledPin = 9;    // LED连接在9号引脚上
    
    void setup()  { 
      // Setup部分不进行任何处理
    } 
    
    void loop()  { 
      // 从暗到亮,以每次加5的形式逐渐亮起来
      for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
        // 输出PWM
        analogWrite(ledPin, fadeValue);         
        // 等待30ms,以便观察到渐变效果
        delay(30);                            
      } 
    
      // 从亮到暗,以每次减5的形式逐渐暗下来
      for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
        // 输出PWM
        analogWrite(ledPin, fadeValue);         
        // 等待30ms,以便观察到渐变效果
        delay(30);                            
      } 
    }


    上传程序到Arduino Uno后,可以观察到LED亮灭交换渐变,好似呼吸一般的效果。

    以上程序中,通过for循环,逐渐改变LED的亮度,达到呼吸的效果。在两个for循环中都有delay(30) 的延时语句,这是为了让我们肉眼能观察到亮度调节的效果。如果没有这个语句,整个变化效果将一闪而过。
            在编程开发中,可以用多种不同的程序写法实现近似的效果。这里再提供一种呼吸灯程序的写法,供大家研究学习。

    kittenblock中小学创客名师推荐的图形化编程软件

    /*
    另一种呼吸灯写法
    感谢PPeach推荐
     */
    
    int led = 9;           // LED灯连接在9号引脚
    int brightness = 0;     // LED灯亮度
    int fadeAmount = 5;   // 亮度渐变值
    
    void setup() {
      pinMode(led, OUTPUT);
    }
    
    void loop() {
      analogWrite(led, brightness);
      brightness = brightness + fadeAmount;
      if (brightness == 0 || brightness == 255) {
        fadeAmount = -fadeAmount ;
      }
      delay(30);
    }




    现在我们要对呼吸灯实验做一个升级,使用电位器控制呼吸灯的呼吸频率。

    3333.png          

    电位器是一个可调电阻,其原理如图2-38所示。通过旋转旋钮改变2号脚位置,从而改变2号脚到两端的阻值。
    实验中,我们需要将电位器1、3脚分别接到GND和3.3V,再通过模拟输入引脚读取电位器2号脚输出的电压,根据我们旋转电位器的情况,2号脚的电压会在0V至3.3V间变化。

    实验所需材料
    Arduino Uno、面包板、LED一个、220Ω电阻一个、10K电位器一个

    连接示意图
    4444.png
    图 2 37 调节呼吸灯频率实验连接示意图


    原理图

    55555.jpg


    如图所示,Arduino通过模拟输入口A0读入经过电位器分压的电压,程序通过判断电压的大小,来调节LED的闪烁频率。
    呼吸频率修改,即是修改每次亮度改变后的延时长短。因此我们将原来的延时函数中固定的参数替换为变量time,通过time的变化,来调节LED呼吸频率的变化。

    实现程序代码如下:

    kittenblock中小学创客名师推荐的图形化编程软件

    int ledPin = 9;  // 9号引脚控制LED
    int pot=A0;    // A0引脚读取电位器输出电压
    void setup(){} 
    
    void loop(){ 
      // LED逐渐变亮
      for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) 
      { 
        analogWrite(ledPin, fadeValue);
    // 读取电位器输出电压,除以5时为了缩短延时时间
    int time=analogRead(pot)/5;
        delay(time);  // 将time用于延时
      } 
      // LED逐渐变暗
      for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) 
      { 
    analogWrite(ledPin, fadeValue);
        delay(analogRead(pot)/5);  // 读取电位器输出电压,并用于延时
      } 
    }


    上传该程序后,你便可以通过电位器来调节呼吸灯的呼吸频率了。

    需要注意的是,程序中的语句

    等效于语句



    如果以上内容对你有帮助,你可以通过打赏支持作者
  • TA的每日心情
    开心
    2018-4-10 16:38
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2018-3-21 14:34 | 显示全部楼层

    Arduino:1.8.5 (Windows 7), 开发板:"Arduino/Genuino Uno"

    构建选项已变更,全部重新构建
    sketch_mar21a:2: error: stray '\302' in program

    聽*/

    ^

    sketch_mar21a:2: error: stray '\240' in program

    sketch_mar21a:3: error: stray '\302' in program



    ^

    sketch_mar21a:3: error: stray '\240' in program

    sketch_mar21a:4: error: stray '\302' in program

    int led = 9;聽聽聽聽聽聽聽聽聽聽 // LED鐏繛鎺ュ湪9鍙峰紩鑴?

    ^

    sketch_mar21a:4: error: stray '\240' in program

    sketch_mar21a:4: error: stray '\302' in program

    sketch_mar21a:4: error: stray '\240' in program

    sketch_mar21a:4: error: stray '\302' in program

    sketch_mar21a:4: error: stray '\240' in program

    sketch_mar21a:4: error: stray '\302' in program

    sketch_mar21a:4: error: stray '\240' in program

    sketch_mar21a:4: error: stray '\302' in program

    sketch_mar21a:4: error: stray '\240' in program

    sketch_mar21a:4: error: stray '\302' in program

    sketch_mar21a:4: error: stray '\240' in program

    sketch_mar21a:4: error: stray '\302' in program

    sketch_mar21a:4: error: stray '\240' in program

    sketch_mar21a:4: error: stray '\302' in program

    sketch_mar21a:4: error: stray '\240' in program

    sketch_mar21a:4: error: stray '\302' in program

    sketch_mar21a:4: error: stray '\240' in program

    sketch_mar21a:4: error: stray '\302' in program

    sketch_mar21a:4: error: stray '\240' in program

    sketch_mar21a:5: error: stray '\302' in program

    int brightness = 0;聽聽聽聽 // LED鐏寒搴?

    ^

    sketch_mar21a:5: error: stray '\240' in program

    sketch_mar21a:5: error: stray '\302' in program

    sketch_mar21a:5: error: stray '\240' in program

    sketch_mar21a:5: error: stray '\302' in program

    sketch_mar21a:5: error: stray '\240' in program

    sketch_mar21a:5: error: stray '\302' in program

    sketch_mar21a:5: error: stray '\240' in program

    sketch_mar21a:6: error: stray '\302' in program

    int fadeAmount = 5;聽聽 // 浜害娓愬彉鍊?

    ^

    sketch_mar21a:6: error: stray '\240' in program

    sketch_mar21a:6: error: stray '\302' in program

    sketch_mar21a:6: error: stray '\240' in program

    sketch_mar21a:7: error: stray '\302' in program



    ^

    sketch_mar21a:7: error: stray '\240' in program

    sketch_mar21a:9: error: stray '\302' in program

    聽聽pinMode(led, OUTPUT);

    ^

    sketch_mar21a:9: error: stray '\240' in program

    sketch_mar21a:9: error: stray '\302' in program

    sketch_mar21a:9: error: stray '\240' in program

    sketch_mar21a:11: error: stray '\302' in program



    ^

    sketch_mar21a:11: error: stray '\240' in program

    sketch_mar21a:13: error: stray '\302' in program

    聽聽analogWrite(led, brightness);

    ^

    sketch_mar21a:13: error: stray '\240' in program

    sketch_mar21a:13: error: stray '\302' in program

    sketch_mar21a:13: error: stray '\240' in program

    sketch_mar21a:14: error: stray '\302' in program

    聽聽brightness = brightness + fadeAmount;

    ^

    sketch_mar21a:14: error: stray '\240' in program

    sketch_mar21a:14: error: stray '\302' in program

    sketch_mar21a:14: error: stray '\240' in program

    sketch_mar21a:15: error: stray '\302' in program

    聽聽if (brightness == 0 || brightness == 255) {

    ^

    sketch_mar21a:15: error: stray '\240' in program

    sketch_mar21a:15: error: stray '\302' in program

    sketch_mar21a:15: error: stray '\240' in program

    sketch_mar21a:16: error: stray '\302' in program

    聽聽聽聽fadeAmount = -fadeAmount ;

    ^

    sketch_mar21a:16: error: stray '\240' in program

    sketch_mar21a:16: error: stray '\302' in program

    sketch_mar21a:16: error: stray '\240' in program

    sketch_mar21a:16: error: stray '\302' in program

    sketch_mar21a:16: error: stray '\240' in program

    sketch_mar21a:16: error: stray '\302' in program

    sketch_mar21a:16: error: stray '\240' in program

    sketch_mar21a:17: error: stray '\302' in program

    聽聽}

    ^

    sketch_mar21a:17: error: stray '\240' in program

    sketch_mar21a:17: error: stray '\302' in program

    sketch_mar21a:17: error: stray '\240' in program

    sketch_mar21a:18: error: stray '\302' in program

    聽聽delay(30)锛?

    ^

    sketch_mar21a:18: error: stray '\240' in program

    sketch_mar21a:18: error: stray '\302' in program

    sketch_mar21a:18: error: stray '\240' in program

    sketch_mar21a:18: error: stray '\357' in program

    sketch_mar21a:18: error: stray '\274' in program

    sketch_mar21a:18: error: stray '\233' in program

    sketch_mar21a:2: error: expected unqualified-id before '/' token

    聽*/

        ^

    sketch_mar21a:2: error: expected constructor, destructor, or type conversion before '/' token

    D:\Documents\Arduino\sketch_mar21a\sketch_mar21a.ino: In function 'void setup()':

    sketch_mar21a:9: error: 'led' was not declared in this scope

    聽聽pinMode(led, OUTPUT);

                 ^

    D:\Documents\Arduino\sketch_mar21a\sketch_mar21a.ino: In function 'void loop()':

    sketch_mar21a:13: error: 'led' was not declared in this scope

    聽聽analogWrite(led, brightness);

                     ^

    sketch_mar21a:19: error: expected ';' before '}' token

    }

    ^

    D:\Documents\Arduino\sketch_mar21a\sketch_mar21a.ino: At global scope:

    sketch_mar21a:20: error: expected declaration before '}' token

    }

    ^

    exit status 1
    stray '\302' in program


    验证显示错误是什么原因,我是新手

    点评

    代码左上角,点“纯文本查看”再复制  详情 回复 发表于 2018-3-22 09:41
  • TA的每日心情
    开心
    2018-4-3 11:56
  • 签到天数: 7 天

    [LV.3]偶尔看看II

    发表于 2018-3-21 10:41 | 显示全部楼层
    还得买电位器
    打赏作者鼓励一下!
  • TA的每日心情
    郁闷
    2018-12-6 22:21
  • 签到天数: 48 天

    [LV.5]常住居民I

     楼主| 发表于 2018-3-22 09:41 | 显示全部楼层
    炽红莲凤凰 发表于 2018-3-21 14:34
    Arduino:1.8.5 (Windows 7), 开发板:"Arduino/Genuino Uno"

    构建选项已变更,全部重新构建

    代码左上角,点“纯文本查看”再复制
    如果以上内容对你有帮助,你可以通过打赏支持作者

    该用户从未签到

    发表于 2018-3-26 15:32 | 显示全部楼层
    纯文本查看还是错误啊

    该用户从未签到

    发表于 2018-3-26 15:39 | 显示全部楼层
    复制纯文本了 还是这样

    Arduino:1.8.3 (Windows 10), 开发板:"Arduino/Genuino Uno"

    sensorread:1: error: stray '\302' in program

    sensorread:1: error: stray '\240' in program

    sensorread:2: error: stray '\302' in program

    sensorread:2: error: stray '\240' in program

    sensorread:2: error: stray '\302' in program

    sensorread:2: error: stray '\240' in program

    sensorread:2: error: stray '\302' in program

    sensorread:2: error: stray '\240' in program

    sensorread:4: error: stray '\302' in program

    sensorread:4: error: stray '\240' in program

    sensorread:6: error: stray '\302' in program

    sensorread:6: error: stray '\240' in program

    sensorread:6: error: stray '\302' in program

    sensorread:6: error: stray '\240' in program

    sensorread:7: error: stray '\302' in program

    sensorread:7: error: stray '\240' in program

    sensorread:7: error: stray '\302' in program

    sensorread:7: error: stray '\240' in program

    sensorread:8: error: stray '\302' in program

    sensorread:8: error: stray '\240' in program

    sensorread:8: error: stray '\302' in program

    sensorread:8: error: stray '\240' in program

    sensorread:9: error: stray '\302' in program

    sensorread:9: error: stray '\240' in program

    sensorread:9: error: stray '\302' in program

    sensorread:9: error: stray '\240' in program

    sensorread:9: error: stray '\302' in program

    sensorread:9: error: stray '\240' in program

    sensorread:9: error: stray '\302' in program

    sensorread:9: error: stray '\240' in program

    sensorread:12: error: stray '\302' in program

    sensorread:12: error: stray '\240' in program

    sensorread:12: error: stray '\302' in program

    sensorread:12: error: stray '\240' in program

    sensorread:12: error: stray '\302' in program

    sensorread:12: error: stray '\240' in program

    sensorread:12: error: stray '\302' in program

    sensorread:12: error: stray '\240' in program

    sensorread:12: error: stray '\302' in program

    sensorread:12: error: stray '\240' in program

    sensorread:13: error: stray '\302' in program

    sensorread:13: error: stray '\240' in program

    sensorread:13: error: stray '\302' in program

    sensorread:13: error: stray '\240' in program

    sensorread:14: error: stray '\302' in program

    sensorread:14: error: stray '\240' in program

    sensorread:14: error: stray '\302' in program

    sensorread:14: error: stray '\240' in program

    sensorread:15: error: stray '\302' in program

    sensorread:15: error: stray '\240' in program

    sensorread:15: error: stray '\302' in program

    sensorread:15: error: stray '\240' in program

    sensorread:16: error: stray '\302' in program

    sensorread:16: error: stray '\240' in program

    sensorread:16: error: stray '\302' in program

    sensorread:16: error: stray '\240' in program

    sensorread:18: error: stray '\302' in program

    sensorread:18: error: stray '\240' in program

    sensorread:18: error: stray '\302' in program

    sensorread:18: error: stray '\240' in program

    sensorread:18: error: stray '\302' in program

    sensorread:18: error: stray '\240' in program

    sensorread:18: error: stray '\302' in program

    sensorread:18: error: stray '\240' in program

    sensorread:18: error: stray '\302' in program

    sensorread:18: error: stray '\240' in program

    sensorread:19: error: stray '\302' in program

    sensorread:19: error: stray '\240' in program

    sensorread:19: error: stray '\302' in program

    sensorread:19: error: stray '\240' in program

    exit status 1
    stray '\302' in program

    在文件 -> 首选项开启
    “编译过程中显示详细输出”选项
    这份报告会包含更多信息。

    点评

    网页上复制的代码,有不可见字符造成的,其他软件编辑下,或者手工重输入下  详情 回复 发表于 2018-6-21 14:12
  • TA的每日心情
    无聊
    2018-5-8 13:06
  • 签到天数: 7 天

    [LV.3]偶尔看看II

    发表于 2018-3-28 19:12 | 显示全部楼层
    要8个led做呼吸灯,怎么做?
  • TA的每日心情
    慵懒
    2019-1-17 09:16
  • 签到天数: 418 天

    [LV.9]以坛为家II

    发表于 2018-4-13 11:09 | 显示全部楼层
    18782023396 发表于 2018-3-28 19:12
    要8个led做呼吸灯,怎么做?

    这个看你需求啊   想8个灯怎么变化   是一起变化还是每个都分别变化
    如果以上内容对你有帮助,你可以通过打赏支持作者
  • TA的每日心情

    2018-5-15 11:29
  • 签到天数: 5 天

    [LV.2]偶尔看看I

    发表于 2018-4-21 12:50 | 显示全部楼层
    学习了学习了
    您需要登录后才可以回帖 登录 | 立即注册  

    本版积分规则

    热门推荐

    KittenBot杯第六届开源硬件开发大赛启动啦
    KittenBot杯第六届开源硬
    大赛简介: 第六届开源硬件开发大赛由Arduino中文社区发起 由KittenBot冠名赞助
    GRBL sender 自己制作的控制软件
    GRBL sender 自己制作的控
    网上找了许多GRBL控制软件都不理想,于是决定自己动手做一个,这是第一版,功能有限,
    有关程序编译出错问题
    有关程序编译出错问题
    ActionGroup_[AG_NUM_][CHAR_NUM]二维数组传入void handleActionGroup(const char
    基于arduino的ZS-40蓝牙模块传输超声波距离,app显示
    基于arduino的ZS-40蓝牙模
    基于arduino的ZS-40蓝牙模块传输超声波距离,app显示
    上传出错
    上传出错
    自从我做红外遥控小车之后就上传出错了,换了一根线也不行,单片机没有插任何东西
    Copyright   ©2015-2016  Arduino中文社区  Powered by©Discuz!   ( 蜀ICP备14017632号-3 )
    快速回复 返回顶部 返回列表