查看: 35717|回复: 46

[教程] 呼吸灯实验

[复制链接]
  • 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 打开呼吸灯示例程序,程序如下:


    [mw_shl_code=cpp,true]/*
    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);                           
      }
    }[/mw_shl_code]

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

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

    [mw_shl_code=cpp,true]/*
    另一种呼吸灯写法
    感谢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);
    }[/mw_shl_code]



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

    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呼吸频率的变化。

    实现程序代码如下:

    [mw_shl_code=cpp,true]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);  // 读取电位器输出电压,并用于延时
      }
    }[/mw_shl_code]

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

    需要注意的是,程序中的语句
    [mw_shl_code=cpp,true]delay(analogRead(pot)/5);[/mw_shl_code]
    等效于语句
    [mw_shl_code=cpp,true]int time=analogRead(pot)/5;
    delay(time);[/mw_shl_code]


  • TA的每日心情
    无聊
    2019-10-23 21:43
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2019-10-22 21:42 | 显示全部楼层
    本帖最后由 LYH3528286 于 2019-10-22 21:51 编辑

    找到原来错误的地方了。好愚蠢的错误。

    1. int led = 9;
    2. int liandu = 0;
    3. int bianhua = 5;

    4. void setup() {
    5.   pinMode(led, OUTPUT);
    6. }

    7. void loop() {
    8.   analogWrite(led, liandu);
    9.   liandu = liandu + bianhua;
    10.   if (liandu == 0 || liandu == 255) {
    11.     bianhua = -bianhua;
    12.   }
    13.   delay(50);
    14. }
    复制代码




  • 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的每日心情
    慵懒
    2020-8-11 09:24
  • 签到天数: 761 天

    [LV.10]以坛为家III

    发表于 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 | 显示全部楼层
    学习了学习了
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    热门推荐

    5分钟带你快速了解新一代开发板:M5STACK
    5分钟带你快速了解新一代
    一、什么是M5Stack M5Stack是一种模块化、可堆叠扩展的开发板,每个模块
    创客火首发无人机编队套装,开启不一样的人工智能教育
    创客火首发无人机编队套装
    2017年国务院发布《新一代人工智能发展规划》,提出要广泛开展人工智能科普活动,在中
    未见过的TFT屏,是什么驱动芯片?
    未见过的TFT屏,是什么驱
    这两块屏是从POS机里拆的,准备做几个锂电池组电压监测,在网上没找到资料,在这里问
    win10系统显示不出端口信息,编译后显示serial port not selected。
    win10系统显示不出端口信
    求各位哥哥帮忙,小兄弟实在不懂是win10系统的原因,还是需要安装什么驱动程式?具体
    更智能的电脑远程开关机-基于继电器
    更智能的电脑远程开关机-
    我只是刚刚入门的萌新,所以有不足请指出 先展示下 功能:开关机,开关机检测,
    Copyright   ©2015-2016  Arduino中文社区  Powered by©Discuz!   
    快速回复 返回顶部 返回列表