极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13712|回复: 2

红外遥控器控制蜂鸣器

[复制链接]
发表于 2015-6-21 11:43:51 | 显示全部楼层 |阅读模式
题目:红外遥控一个蜂鸣器,要求蜂鸣器发出不同频率的声音
正常思维:
#include <IRremote.h>
#include <LiquidCrystal.h>
int RECV_PIN = 11; //红外线接收器OUTPUT端接在pin 11
IRrecv irrecv(RECV_PIN); // 定义IRrecv 对象来接收红外线信号
int Buzzer=9; void setup()
{
Serial.begin(9600);
pinMode(Buzzer,OUTPUT);
irrecv.enableIRIn(); //启动红外解码
delay(1000);
} void loop()
{tone(Buzzer,500);}

执行上述代码会出现类似
core.a(Tone.cpp.o): In function `__vector_7':
E:\arduino-1.0.1\hardware\arduino\cores\arduino/Tone.cpp:523: multiple definition of `__vector_7'
IRremote\IRremote.cpp.o:E:\arduino-1.0.1\libraries\IRremote/IRremote.cpp:311: first defined here
的错误。
是由于IRremote.h和tone中有相同的内部计时器,直接改IRremote.h或tone中的计时器显然不好改,那我们就想办法替换掉tone()函数,用newtone()实现tone()的功能(虽然我并不知道tone()和IRremote.h中的计时器是什么样的,我们现在只需要实现他的功能就可以)。
正确思维:
#include <IRremote.h>
#include <LiquidCrystal.h>
int RECV_PIN = 11; //红外线接收器OUTPUT端接在pin 11
IRrecv irrecv(RECV_PIN); // 定义IRrecv 对象来接收红外线信号
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); //启动红外解码
delay(1000);
}
void newtone(int tonePin,int frequency, int duration)
{
  pinMode(tonePin,OUTPUT);
  int period = 1000000 / frequency;
  int pulse = period / 2;
  for (long i = 0; i < duration * 1000; i += period) {
    digitalWrite(tonePin, HIGH);
    delayMicroseconds(pulse);
    digitalWrite(tonePin, LOW);
    delayMicroseconds(pulse);
  }
}
void loop()
{newtone(8,500,1000);}

最终成果(红外遥控的代码用ardublock生成,ardublock详情见附件):
#include <IRremote.h>
#include <ctype.h>
#include <Wire.h>

IRrecv __ab_irrecv(11);
void __ab_setupIrReceiver()
{
  __ab_irrecv.enableIRIn();
  __ab_irrecv.resume();
}
void charsToUpper(char *str)
{
  int p=0;
  while(str[p] != 0)
  {
    str[p] = toupper(str[p]);
    ++p;
  }
}
void __ab_getIrCommand(char *receivedCommand)
{
  decode_results result;
  if (__ab_irrecv.decode(&result))
  {
    ltoa(result.value, receivedCommand, 16);
    charsToUpper(receivedCommand);
    __ab_irrecv.resume();
  }
  else
  {
    receivedCommand[0] = '\0';
  }
}
char _ABVAR_1_ircode[64] = "";
void setup()
{
  __ab_setupIrReceiver();
}
void newtone(int tonePin,int frequency, int duration)
{
  pinMode(tonePin,OUTPUT);
  int period = 1000000 / frequency;
  int pulse = period / 2;
  for (long i = 0; i < duration * 1000; i += period) {
    digitalWrite(tonePin, HIGH);
    delayMicroseconds(pulse);
    digitalWrite(tonePin, LOW);
    delayMicroseconds(pulse);
  }
}
void loop()
{
  __ab_getIrCommand(_ABVAR_1_ircode);
  if (!( strlen(_ABVAR_1_ircode) == 0 ))
  {
    if (strcmp(_ABVAR_1_ircode, "FF30CF") == 0)     //FF30CF 为遥控器按键的编码
    {
      newtone(8,440, 1000);
    }
    if (strcmp(_ABVAR_1_ircode, "FF18E7") == 0)
    {
      newtone(8,1000, 1000);
    }
  }
  delay( 1000 );
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2015-6-23 18:36:18 | 显示全部楼层
            scartch,很好
回复 支持 反对

使用道具 举报

发表于 2016-4-6 15:09:22 | 显示全部楼层
感谢 彩色大冰棍
按照你的思路,用了新函数,解决了问题
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 04:49 , Processed in 0.037183 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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