arduino 红外遥控器设置功能按键

给红外遥控器设置两个功能按键,文字输入和数字输入,按下后再去按1-9按键可在LCD1602上显示数字或设置好的一串字母,红外遥控模块和LCD显示模块都没有问题,只是判断按键后执行语句中无法再次进行判断,功能键的功能无法执行,不知道有什么方法可以解决,代码如下,球球各位大神给指点一下

#include <IRremote.h>
#include <IRremoteInt.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(11,10,9,8,7,6);
#define RECV_PIN 13
IRrecv irrecv(RECV_PIN);
decode_results results;
void words();
void number();

void setup() 
{
  Serial.begin(9600);
  lcd.begin(16,2);
  Serial.println("Enabling IRin");
  irrecv.enableIRIn();
  Serial.println("Enabled IRin");
  lcd.clear();
  lcd.setCursor(0,0);
 }


void words()
{
   if (irrecv.decode(&results))
    {
      Serial.println(results.value, HEX); 
      if(results.value==0xFF30CF)
      {lcd.setCursor(2,0);lcd.print("Hello World!");delay(2000);lcd.clear();}
      else if(results.value==0xFF18E7)
      {lcd.setCursor(3,0);lcd.print("Tian Ji");delay(2000);lcd.clear();}
      else if(results.value==0xFF7A85)
      {lcd.setCursor(3,0);lcd.print("Da Xue");delay(2000);lcd.clear();}
      else if(results.value==0xFF10EF)
      {lcd.setCursor(3,0);lcd.print("Ren Ai");delay(2000);lcd.clear();}
      else if(results.value==0xFF38C7)
      {lcd.setCursor(3,0);lcd.print("Xue Yuan");delay(2000);lcd.clear();}
      else if(results.value==0xFF5AA5)
      {lcd.setCursor(3,0);lcd.print("Welcome!");delay(2000);lcd.clear();}
      else if(results.value==0xFF42BD)
      {lcd.setCursor(3,0);lcd.print("Ni Hao!");delay(2000);lcd.clear();}
      else if(results.value==0xFF4AB5)
      {lcd.write("Nice to meet you");delay(2000);lcd.clear();}
      else if(results.value==0xFF52AD)
      {lcd.setCursor(4,0);lcd.print("Bye-bye");delay(2000);lcd.clear();}
    }
}
void number()
{
    lcd.leftToRight();
    if(irrecv.decode(&results))
  {
    Serial.println(results.value, HEX); 
    if(results.value==0xFF30CF)
      {lcd.print("1");delay(100);}
    else if(results.value==0xFF18E7)
      {lcd.print("2");delay(100);}
    else if(results.value==0xFF7A85)
      {lcd.print("3");delay(100);}
    else if(results.value==0xFF10EF)
      {lcd.print("4");delay(100);}
    else if(results.value==0xFF38C7)
      {lcd.print("5");delay(100);}
    else if(results.value==0xFF5AA5)
      {lcd.print("6");delay(100);}
    else if(results.value==0xFF42BD)
      {lcd.print("7");delay(100);}
    else if(results.value==0xFF4AB5)
      {lcd.print("8");delay(100);}
    else if(results.value==0xFF52AD)
      {lcd.print("9");delay(100);}
    else if ( results.value==0xFF6897)
      {lcd.print("0");delay(100);}
    }
}

void loop() 
{
   if (irrecv.decode(&results)) 
   {
     Serial.println(results.value, HEX); 
    if  (results.value==0xFFE21D)
       {delay(1000);lcd.clear();lcd.print("screen opening...");delay(1000);lcd.clear();}
  else if (results.value==0xFFA25D)
       {lcd.setCursor(3,0);lcd.print("screen off");delay(1000);lcd.clear();}
  else if (results.value==0xFF629D)
       {lcd.clear();}
  else if (results.value==0xFF22DD)
       {lcd.setCursor(3,0);lcd.print("Input words:");delay(1000);lcd.clear();words();}
  else if (results.value==0xFF02FD)
       {lcd.setCursor(3,0);lcd.print("Input number:");delay(1000);lcd.clear();number();}
        irrecv.resume();
   } 
    delay(1000);
}

1个回答

在lcd.print("Input words:");之后,delay之前,调用一下irrecv.resume(); ,使得irrecv开始接收下一个按键信息。

补充一下,大致上是这个意思。我没有调试环境,所以只能把代码逻辑写出来。

#include <IRremote.h>
#include <IRremoteInt.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(11, 10, 9, 8, 7, 6);
#define RECV_PIN 13

enum menu_states { 
  TOP_MENU, 
  INPUT_WORDS, 
  INPUT_NUMBER 
};

int stat;

IRrecv irrecv(RECV_PIN);
decode_results results;
void words();
void number();
void main_menu();

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  Serial.println("Enabling IRin");
  irrecv.enableIRIn();
  Serial.println("Enabled IRin");
  lcd.clear();
  lcd.setCursor(0, 0);
  stat = TOP_MENU;
}

void words() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    // 每个decode配一个resume
    irrecv.resume();

    if (results.value == 0xFF30CF) {
      lcd.setCursor(2, 0);
      lcd.print("Hello World!");
      delay(2000);
      lcd.clear();
    } else if (results.value == 0xFF18E7) {
      lcd.setCursor(3, 0);
      lcd.print("Tian Ji");
      delay(2000);
      lcd.clear();
    } else if (results.value == 0xFF7A85) {
      lcd.setCursor(3, 0);
      lcd.print("Da Xue");
      delay(2000);
      lcd.clear();
    } else if (results.value == 0xFF10EF) {
      lcd.setCursor(3, 0);
      lcd.print("Ren Ai");
      delay(2000);
      lcd.clear();
    } else if (results.value == 0xFF38C7) {
      lcd.setCursor(3, 0);
      lcd.print("Xue Yuan");
      delay(2000);
      lcd.clear();
    } else if (results.value == 0xFF5AA5) {
      lcd.setCursor(3, 0);
      lcd.print("Welcome!");
      delay(2000);
      lcd.clear();
    } else if (results.value == 0xFF42BD) {
      lcd.setCursor(3, 0);
      lcd.print("Ni Hao!");
      delay(2000);
      lcd.clear();
    } else if (results.value == 0xFF4AB5) {
      lcd.write("Nice to meet you");
      delay(2000);
      lcd.clear();
    } else if (results.value == 0xFF52AD) {
      lcd.setCursor(4, 0);
      lcd.print("Bye-bye");
      delay(2000);
      lcd.clear();
    }
  }

  // 每个decode配一个delay
  delay(100);

  stat = TOP_MENU;
}

void number() {
  lcd.leftToRight();
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    // 每个decode配一个resume
    irrecv.resume();

    if (results.value == 0xFF30CF) {
      lcd.print("1");
      delay(100);
    } else if (results.value == 0xFF18E7) {
      lcd.print("2");
      delay(100);
    } else if (results.value == 0xFF7A85) {
      lcd.print("3");
      delay(100);
    } else if (results.value == 0xFF10EF) {
      lcd.print("4");
      delay(100);
    } else if (results.value == 0xFF38C7) {
      lcd.print("5");
      delay(100);
    } else if (results.value == 0xFF5AA5) {
      lcd.print("6");
      delay(100);
    } else if (results.value == 0xFF42BD) {
      lcd.print("7");
      delay(100);
    } else if (results.value == 0xFF4AB5) {
      lcd.print("8");
      delay(100);
    } else if (results.value == 0xFF52AD) {
      lcd.print("9");
      delay(100);
    } else if (results.value == 0xFF6897) {
      lcd.print("0");
      delay(100);
    }
  }

  // 每个decode配一个delay
  delay(100);

  stat = TOP_MENU;
}

void main_menu() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume();

    if (results.value == 0xFFE21D) {
      delay(1000);
      lcd.clear();
      lcd.print("screen opening...");
      delay(1000);
      lcd.clear();
    } else if (results.value == 0xFFA25D) {
      lcd.setCursor(3, 0);
      lcd.print("screen off");
      delay(1000);
      lcd.clear();
    } else if (results.value == 0xFF629D) {
      lcd.clear();
    } else if (results.value == 0xFF22DD) {
      lcd.setCursor(3, 0);
      lcd.print("Input words:");
      delay(1000);
      lcd.clear();
      stat = INPUT_WORDS;
    } else if (results.value == 0xFF02FD) {
      lcd.setCursor(3, 0);
      lcd.print("Input number:");
      delay(1000);
      lcd.clear();
      stat = INPUT_NUMBER;
    }
  }
  delay(1000);
}

void loop() {
  if (TOP_MENU == stat) {
    main_menu();
  } else if (INPUT_NUMBER == stat) {
    number();
  } else if (INPUT_WORDS == stat) {
    words();
  }
}
TTwar
TTwar 回复bobhuang: 十分感谢,已经做出来了,谢谢谢谢
4 个月之前 回复
bobhuang
bobhuang 回复TTwar: 状态控制的代码逻辑,我编辑进第一个回复了。
4 个月之前 回复
TTwar
TTwar 回复bobhuang: 我按照你说的做了做,但是每次loop只读一个输入无法实现,我要设置的不止这个功能键,还要控制LCD背光,开关屏幕,没法只读某一个状态,我就不知道该咋弄了
4 个月之前 回复
bobhuang
bobhuang 回复TTwar: 如果irrecv.resume无效的话,至少每次loop的按键接收是正常的。这种情况可以用状态机的机制来实现控制。例如,定义一个变量stat记录当前状态,可以有三个状态:top_menu, input_words, input_number。在top_menu状态下,执行现在loop里的if/else逻辑,在input_number/input_words状态下执行对应的输入逻辑。也就是说每次loop只读取一个输入并做相应处理和转换状态。
5 个月之前 回复
TTwar
TTwar 还是不太行,反复改了改还是没有反应
5 个月之前 回复
TTwar
TTwar 谢谢~我去试试
5 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问