TTwar
TTwar
采纳率100%
2019-11-21 14:26

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条回答

  • bobhuang bobhuang 2年前

    在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();
      }
    }
    
    点赞 评论 复制链接分享