极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10576|回复: 3

为什么这段代码不能正确得到红外信号?

[复制链接]
发表于 2015-1-31 15:56:34 | 显示全部楼层 |阅读模式
本帖最后由 zhangtory 于 2015-2-1 16:39 编辑

为什么中途PIN2接地后不能接收到红外信号呢?
另外,在最开始就将PIN2接地就能收到红外信号,不接地后再接地也不能收到红外信号了,为什么啊?
以解决!
在输入口有电平变化的时候从新irrecv.enableIRIn(); 一下。
但是我不知道为什么。。。

  1. #include <IRremote.h>

  2. int RECV_PIN = 11; //recive pin
  3. int LED_PIN = 13;

  4. IRrecv irrecv(RECV_PIN);
  5. IRsend irsend;

  6. decode_results results;
  7. unsigned int rawCode[500];
  8. int bits;

  9. void setup()
  10. {
  11.   Serial.begin(9600);
  12.   pinMode(LED_PIN, OUTPUT);
  13.   digitalWrite(LED_PIN, HIGH);
  14.   delay(2000);
  15.   digitalWrite(LED_PIN, LOW);
  16.   irrecv.enableIRIn(); // Start the receiver
  17.   pinMode(2, INPUT_PULLUP);
  18. }

  19. //send commend
  20. void sendCode(){
  21.   irsend.sendRaw(rawCode, bits, 38);
  22.   Serial.print("sendRaw: ");
  23.   Serial.print(rawCode, HEX);
  24.   Serial.print(" | ");
  25.   Serial.println(bits);
  26.   delay(200);
  27. }

  28. //recive and record
  29. void rec_ir(){
  30.   if (irrecv.decode(&results)) {
  31.     Serial.println(results.value, HEX);
  32.     irrecv.resume(); // Receive the next value
  33.   }
  34. }

  35. void loop() {
  36.   //delay(3000);
  37.   if(digitalRead(2) == LOW){
  38.     rec_ir();
  39.   }
  40.   if(digitalRead(2) == HIGH){
  41.     sendCode();
  42.     delay(1000);
  43.   }
  44. }


复制代码


在rec_ir();函数中加了print,发现是执行了 的,那应该就是红外库的原因吧!
有可能是什么问题?

谢谢啦~
回复

使用道具 举报

发表于 2015-1-31 17:19:46 | 显示全部楼层
作为输入端应该有上拉电阻或下拉电阻。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-1-31 18:02:50 | 显示全部楼层
林定祥 发表于 2015-1-31 17:19
作为输入端应该有上拉电阻或下拉电阻。

在setup()中已经加了pinMode(2, INPUT_PULLUP);
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-2-1 12:43:28 | 显示全部楼层
代码更新了下,求助啊!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 16:02 , Processed in 0.052525 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表