0
votes

I am new to Arduino and I'm trying to make a program that receives IR codes from a TV remote, uses them as a 4 number pass code lighting up a LED as you press each button. And then comparing the code to a hard-coded one. In this case 1234. I made a function to verify that the value entered is equal to the pass. If so, light up a green LED and else, light up a red one. However, even if I input the correct code, only the red led lights up. Here is my whole code as I'm not sure which part of it is the one causing problems:

const int pass[4] = {1, 2, 3, 4};
int value[4] = {};
int digitNum = 0;
int input;


void loop() 
{
  value[digitNum] = input; //where input is a number between 0 and 9
  digitNum++;
  if(digitNum == 1){
    lightFirstLed(); 
  }
  else if(digitNum == 2){
    lightSecondLed();
  }
  else if(digitNum == 3){
    lightThirdLed();
  }
  else if(digitNum == 4){
    lightFourthLed();
    verify();
  }
}

void verify()
{
  bool falseCharacter = false;
  for(int i = 0; i <= 4; i++){
    if(value[i] != pass[i]){
      falseCharacter = true;
    }
  }
  if(!falseCharacter){
    lightGreenLed();
  }
  else{
    lightRedLed();
  }
}

Functions in the form of light*Led actually do what they're supposed to do. I tried changing the verify function around, that ended up making the green LED the one that always shone. I've been doing this for hours and I'm starting to feel disparate.

I would really appreciate any help. And please tell me if anything I'm doing does not comply with best practices even if it's out of the scope of this question.

For full code and design, here's a link to autodesk's simulator: https://www.tinkercad.com/things/0keqmlhVqNp-mighty-leelo/editel?tenant=circuits?sharecode=vVUD2_4774Lj4PYXh6doFcOqWUMY2URIfW8VXGxutRE= EDIT: Now reset doesn't work

1
Welcome to Stack Overflow! There is far too much code in your question, and it is unlikely that all of it is contributing to your issue. Please create a minimal reproducible example that shows your problem in as few lines as possible.Joe C
Ok I will try. Though I have no idea what's causing the problem and how to fix it.Oussema
@JoeC is this better?Oussema
Does the Arduino have an IR remote library?Fiddling Bits
410 - The requested page has expiredFiddling Bits

1 Answers

0
votes

Your for loop in verify is accessing outside the array:

const int pass[4] = {1, 2, 3, 4};
int value[4] = {};

for(int i = 0; i <= 4; i++){
    if(value[i] != pass[i]){
        falseCharacter = true;
    }
}

Change i <= 4 to i < 4. Also, when falseCharacter is set to true, break from the loop:

for(int i = 0; i < 4; i++)
{
    if(value[i] != pass[i])
    {
        falseCharacter = true;
        break;
    }
}

Update

You need an else statement in loop:

void loop(void)
{
    if(irrecv.decode(&results))
    {
        if (results.value == powBtn)
        {
            reset();
        }
        else if (results.value == zeroBtn)
        {
            input = 0;
        }
        else if (results.value == oneBtn)
        {
            input = 1;
        }
        else if (results.value == twoBtn)
        {
            input = 2;
        }
        else if (results.value == threeBtn)
        {
            input = 3;
        }
        else if (results.value == fourBtn)
        {
            input = 4;
        }
        else if (results.value == fiveBtn)
        {
            input = 5;
        }
        else if (results.value == sixBtn)
        {
            input = 6;
        }
        else if (results.value == sevenBtn)
        {
            input = 7;
        }
        else if (results.value == eightBtn)
        {
            input = 8;
        }
        else if (results.value == nineBtn)
        {
            input = 9;
        }
        else
        {
            return; /*** !!! Unrecognized Value !!! ***/
        }

        value[digitNum] = input;
        digitNum++;
        if(digitNum == 1)
        {
            digitalWrite(LED1, HIGH);
        }
        else if(digitNum == 2)
        {
            digitalWrite(LED2, HIGH);
        }
        else if(digitNum == 3)
        {
            digitalWrite(LED3, HIGH);
        }
        else if(digitNum == 4)
        {
            digitalWrite(LED4, HIGH);
            verify();
        }
        else
        {
            if (results.value == powBtn)
            {
                reset();
            }
        }

        // Receive the next value
        irrecv.resume();
    }
}