您好,我有这段代码必须处理红外遥控器,当我包含与红外遥控器相关的代码时,我的代码停止运行

问题描述 投票:0回答:1

因此,我必须创建一个安全系统,可以使用红外遥控器或面包板上的按钮来解锁。面包板按钮延迟的部分功能正常,但是,我的问题出在红外遥控器上。当我输入与遥控器相关的代码时,我的代码将停止工作。

这是我的代码:

// C++ code
//
// In this code, when motion is detected, the alarm will go off, a buzzer will start to beep, and the LED will blink. Now, the user has two options. 1. they can unlock the system using push buttons on the breadboard. Pressing the correct passcode will unlock the system, and the LED and the buzzer will stop. The user can achieve the same functionality using an IR remote. The system will unlock by pressing the correct passcode combinations on the IR. 
#include <IRremote.h>
#include <LiquidCrystal.h>

bool resetalarm=false; //Setting the reset alarm to false. 
int button0=0; // Setting the button counts for button 0 to 0.
int button1=0; // Setting the button counts for button 1 to 0.
int digit1=0; //Setting the variables used to store user input to zero for the passcode. 
int digit2=0;
int digit3=0;
int digit4=0; 
LiquidCrystal lcd(5, 4, 3, 2, 1, 0);

IRrecv IR(6);
decode_results results;
// variable related to remote function 
int number1=0;
int number0=0;
int code1=0;
int code2=0;
int code3=0;
int code4=0;

void setup()
{
  pinMode(13, INPUT);
  pinMode(12, INPUT);
  pinMode(11, INPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  lcd.begin(16, 2);
  IR.enableIRIn(); 
  
}

void loop()
{ 
  if (!resetalarm && digitalRead(13)==HIGH)
  {
    while (!resetalarm)
    {
      //Activating the buzzer and setting the LED to blink and displaying system locked on the LCD. 
      tone(9, 1000);
      digitalWrite(10, HIGH);
      delay(300); 
      digitalWrite(10, LOW);
      delay(300); 
      lcd.clear();
      lcd.print("System Locked");
      delay(200);
      
      // calling function remote
      if (IR.decode()) 
      {
        remote();
        delay(1500);
        IR.resume(); // Receive the next value
      }
      
      // Check if button 1 is pressed 
      if (digitalRead(11) == HIGH)
      {
        button1++; //Adds to the button count each time it is pressed. 
        // Males a small beep sound and displaying "*" for each digit/ 
        tone(9, 200, 100); 
        delay(50);
        lcd.setCursor(button1+button0 - 1, 1);
        lcd.print("*");
      }
      // Check if button 0 is pressed
      if (digitalRead(12) == HIGH)
      {
        button0++; //Adds to the button count each time it is pressed. 
        // Makes a small beep sound and displaying "*" for each digit/ 
        tone(9, 200, 100);
        delay(50);
        lcd.setCursor(button0+button1 - 1, 1);
        lcd.print("*");
      }
      // Check if the passcode is incorrect and reset the valuses tp 0. 
      if (button1+button0==4 && digit1!=1 && digit2!=1 && digit3!=1 && digit4!=1)
      {
        button0=0;
        button1=0;
        digit1=0; 
        digit2=0;
        digit3=0;
        digit4=0;
        lcd.setCursor(0, 1);
        lcd.clear();
        lcd.print("Invalid passcode");
        delay(2000);
        
      } 
      //checking for the passcode
      // Check if button 1 is pressed for digit 1.
      if (button1==1 && button0==0 && digit1==0)
      {
        digit1=1; // Mark digit 1 as entered.
        delay(50);
      }
      // Check if button 0 is pressed after digit 1.
      else if (button1==2 && button0==0 && digit1==1 && digit2==0)
      {
        digit2=1; // Mark digit 2 as entered.
        delay(50);
      }
      // Check if button 1 is pressed after digit 2. 
      else if (button1==2 && button0==1 && digit1==1 && digit2==1 && digit3==0)
      {
        digit3=1;// Mark digit 3 as entered.
        delay(50);
      }
      // Check if button 1 is pressed after digit 3. 
      else if (button1==3 && button0==1 && digit1==1 && digit2==1 && digit3==1 && digit4==0)
      {
        digit4=1;// Mark digit 4 as entered.
        delay(50);
      }
      // Check if the passcode is entered correctly.
      if (digit1==1 && digit2==1 && digit3==1 && digit4==1)
      {
        // Reset the alarm.
        resetalarm = true;
        lcd.clear();
        lcd.print("System unlocked");
        noTone(9);
        digitalWrite(10, LOW);
        button0 = 0;
        button1 = 0;
        digit1=0;
        digit2=0;
        digit3=0;
        digit4=0;
        delay(500);
      }
    }
  }
  else
  {
    resetalarm=false;
  }
}

//function for the remote. 
void remote()
{
  // check if button 1 on the remote is pressed and add one to the 
  if (results.value == 0xFFA857)
  {
    number1++; //Adds to the button count each time it is pressed. 
    // making a small beep sound and displaying "*" for each digit/ 
    tone(9, 200, 100);
    delay(50);
    lcd.setCursor(button1+button0 - 1, 1);
    lcd.print("*");
  }
  if (results.value == 0xFF629D)
  {
    number0++; //Adds to the button count each time it is pressed. 
    // making a small beep sound and displaying "*" for each digit/ 
    tone(9, 200, 100);
    delay(50);
    lcd.setCursor(button0+button1 - 1, 1);
    lcd.print("*");
  }
  // Checks if an inccorect passcode is entered and if so it will rest all the values and displays "incorrect passcoe". 
  if (number1+number0==4 && code1!=1 && code2!=1 && code3!=1 && code4!=1)
  {
    number0=0;
    number1=0;
    code1=0; 
    code2=0;
    code3=0;
    code4=0;
    lcd.setCursor(0, 1);
    lcd.clear();
    lcd.print("Invalid passcode");
    delay(2000);
  } 
  // Check if button 1 is pressed for the first time and add one to code 1. 
  if (number1==1 && number0==0 && code1==0)
  {
    code1=1; // Mark code 1 as entered.
    delay(50);
  }
  // Check if button 1 is pressed after code 1.
  else if (number1==2 && number0==0 && code1==1 && code2==0)
  {
    code2=1; // Mark code 2 as entered.
    delay(50);
  }
  // Check if button 0 is pressed after code 2.
  else if (number1==2 && number0==1 && code1==1 && code2==1 && code3==0)
  {
    code3=1;// Mark code 3 as entered.
    delay(50);
  }
   // Check if button 1 is pressed after code 3.
  else if (number1==3 && number0==1 && code1==1 && code2==1 && code3==1 && code4==0)
  {
    code4=1;// Mark code 4 as entered.
    delay(50);
  }
  // Check if the passcode is entered correctly.
  if (code1==1 && code2==1 && code3==1 && code4==1)
  {
    // Reset the alarm.
    resetalarm = true;
    lcd.clear();
    lcd.print("System unlocked");
    noTone(9);
    digitalWrite(10, LOW);
    number0 = 0;
    number1 = 0;
    code1=0;
    code2=0;
    code3=0;
    code4=0;
    delay(500);

  }
}`

当我输入红外遥控器的代码时,不仅红外遥控器不起作用,而且 LED、蜂鸣器或 LCD 也不起作用。

c++ arduino
1个回答
0
投票

我认为你应该将结果指针传递到这里

IR.decode()

首先检查他们的文档IRremote

© www.soinside.com 2019 - 2024. All rights reserved.