红外遥控实验

红外遥控实验

实验现象

当按下遥控器上某个按键,串口输出该按键的名称

理论学习

实验使用红外接收头VS1838+红外遥控器,VS1838使用NEC码编码格式
NEC码格式:

1.使用38kHz载波频率
2.引导码间隔是9ms+4.5ms
3.使用16位客户代码
4.使用8位数据代码和8位取反的数据代码
在这里插入图片描述
二进制0和1的表示方法:
在这里插入图片描述

原理图

在这里插入图片描述
在这里插入图片描述

代码编写

#include <IRremote.h>
int RECV_PIN = 11;//端口声明
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LED6 = 7;
long on1 = 0x00FF6897;//编码示例,与发送匹配用
long off1 = 0x00ff30CF;
long on2 = 0x00FF9867;
long off2 = 0x00FF18E7;
long on3 = 0x00FFB04F;
long off3 = 0x00FF7A85;
long on4 = 0x00FF10EF;
long off4 = 0x00FF42BD;
long on5 = 0x00FF38C7;
long off5 = 0x00FF4AB5;
long on6 = 0x00FF5AA5;
long off6 = 0x00FF52AD;
IRrecv irrecv(RECV_PIN);
decode_results results;//结构声明
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
void dump(decode_results* results)
{
    int count = results->rawlen;
    if (results->decode_type == UNKNOWN)
    {
        Serial.println("Could not decode message");
    }
    else
    {
        if (results->decode_type == NEC)
        {
            Serial.print("Decoded NEC: ");
        }
        else if (results->decode_type == SONY)
        {
            Serial.print("Decoded SONY: ");
        }
        else if (results->decode_type == RC5)
        {
            Serial.print("Decoded RC5: ");
        }
        else if (results->decode_type == RC6)
        {
            Serial.print("Decoded RC6: ");
        }
        Serial.print(results->value, HEX);
        Serial.print(" (");
        Serial.print(results->bits, DEC);
        Serial.println(" bits)");
    }
    Serial.print("Raw (");
    Serial.print(count, DEC);
    Serial.print("): ");

    for (int i = 0; i < count; i++)
    {
        if ((i % 2) == 1)
        {
            Serial.print(results->rawbuf[i] * USECPERTICK, DEC);
        }
        else
        {
            Serial.print(-(int)results->rawbuf[i] * USECPERTICK, DEC);
        }
        Serial.print(" ");
    }
    Serial.println("");
}

void setup()
{
    pinMode(RECV_PIN, INPUT);   //端口模式,输入
    pinMode(LED1, OUTPUT);//端口模式,输出
    pinMode(LED2, OUTPUT);//端口模式,输出
    pinMode(LED3, OUTPUT);//端口模式,输出
    pinMode(LED4, OUTPUT);//端口模式,输出
    pinMode(LED5, OUTPUT);//端口模式,输出
    pinMode(LED6, OUTPUT);//端口模式,输出
    pinMode(13, OUTPUT); 端口模式,输出
        Serial.begin(9600);    //波特率9600
    irrecv.enableIRIn(); // Start the receiver
}

int on = 0;
unsigned long last = millis();

void loop()
{
    if (irrecv.decode(&results)) //调用库函数:解码
    {
        // If it's been at least 1/4 second since the last
        // IR received, toggle the relay
        if (millis() - last > 250)
        {
            on = !on;
            digitalWrite(13, on ? HIGH : LOW);
            dump(&results);
        }
        if (results.value == on1)
            digitalWrite(LED1, HIGH);
        if (results.value == off1)
            digitalWrite(LED1, LOW);
        if (results.value == on2)
            digitalWrite(LED2, HIGH);
        if (results.value == off2)
            digitalWrite(LED2, LOW);
        if (results.value == on3)
            digitalWrite(LED3, HIGH);
        if (results.value == off3)
            digitalWrite(LED3, LOW);
        if (results.value == on4)
            digitalWrite(LED4, HIGH);
        if (results.value == off4)
            digitalWrite(LED4, LOW);
        if (results.value == on5)
            digitalWrite(LED5, HIGH);
        if (results.value == off5)
            digitalWrite(LED5, LOW);
        if (results.value == on6)
            digitalWrite(LED6, HIGH);
        if (results.value == off6)
            digitalWrite(LED6, LOW);
        last = millis();
        irrecv.resume(); // Receive the next value
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_45671732

你们鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值