108个传感器之3:红外线接收点灯

序言

最近在用 arduino uno 开发智能小车,红外线无线操作的时候碰到了一些问题,参照网上的教程基本都是老掉牙的很丑代码或者方案... 

好好好,既然没有现成的,那就自己参考官方案例琢磨,通过本文记录一下,也希望能对更多人有所帮助。

前期准备

  1. arduino 开发板 x 1

  2. 红外线遥控器 x 1

    ee01f02180fe56e375918132459c5f36.jpeg

  3. Ky-022 红外线接收模块 x 1

  4. 杜邦线若干

环境配置

这个项目我是基于 platformIO + vscode 的形式开发,更符合时代的进步(arduino-ide是什么玩意儿...界面看吐了都)

我愿称这个搭配为最强生产力,至少对于做前端的我来说很好上手。

1. 安装 IRremote 依赖

这个库主要是来负责接收红外线信号,使用起来也是相当的方便。首先去到 platformIO 的首页,进入 library 找到这个库下载就行。

782de6c58e1e4a81c09b592acc7aa2fa.jpeg

2. 线路连接

传感器 GND 连 uno GND,VCC 连接 3.3v 或者 5v,DAT 我这里由于其它端口暂时被占用,连接的是 10 号引脚(理论上2、3、5、6、9、10、11 都可)。

9486db655ef2631ef4f77a4005fce872.jpeg

开发流程

记得插上你的开发设备,并通电。

1. 测试红外线设备

把下面的代码烧录进 arduino 设备。然后打开串口监视页面,按下遥控器,看终端是否会打印出值。如果返回,那说明正常连接成功,可以进行下一步了。

#include <Arduino.h>

#include <IRremote.hpp>

void setup() { &nbsp; &nbsp;

Serial.begin(9600); &nbsp; &nbsp;

IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

}

void loop() { &nbsp;&nbsp;

&nbsp;if (IrReceiver.decode()) { &nbsp; &nbsp; &nbsp; &nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;if (IrReceiver.decodedIRData.protocol != UNKNOWN) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Serial.println(IrReceiver.decodedIRData.command); &nbsp; &nbsp;// 打印接收到的值 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IrReceiver.resume(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IrReceiver.printIRResultShort(&Serial); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IrReceiver.printIRSendUsage(&Serial); &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp;} &nbsp;&nbsp;

&nbsp;}}

2. 写入 LED 判断语句

#include <Arduino.h>#include <IRremote.hpp>int ledPin = A5; &nbsp;// 我这里给 LED 的引脚是 A5int LED_CODE = 22; &nbsp; &nbsp; &nbsp; // led 对应的按钮,数字0 的codebool ledState = false; void setup() { &nbsp; &nbsp;Serial.begin(9600); &nbsp; &nbsp;IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); &nbsp;// 初始化红外线接收模块}void loop() { &nbsp; &nbsp;// 当接收到红外线信号时 &nbsp; &nbsp;if (IrReceiver.decode()) { &nbsp; &nbsp; &nbsp; &nbsp;if (IrReceiver.decodedIRData.protocol != UNKNOWN) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switch (IrReceiver.decodedIRData.command) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case LED_CODE: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ledState = !ledState; // 切换LED的状态 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;digitalWrite(ledPin, ledState ? HIGH : LOW); // 设置LED的状态 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 其他情况不作处理 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IrReceiver.resume(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IrReceiver.printIRResultShort(&Serial); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IrReceiver.printIRSendUsage(&Serial); &nbsp; &nbsp; &nbsp; } &nbsp; }}

把代码上传到uno,不出意外的话,led就会随着点击数字0键,开启关闭切换。

小结

完整案例我加入了控制小车移动的控制,包括蜂鸣器做喇叭的代码,虽然小车移动左右方向还有点问题,但是问题不大,今天先写到这里,晚安,玛卡巴卡。

首发:dev 的艺术空间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值