Difficulty getting LED to light up correctly

Hello. I am doing my first ever Arduino project, which basically consists of a password verification.

The correct password has 24 characters and I intend the Arduino to light a green LED if the password is correct and a red LED if incorrect. However, by entering just the first four numbers, the red LED already lights up. I have tried looking for many solutions, but it always keeps happening.

I am facing great struggle. Any help is appreciated. Code is below:

int ledVermelho = 13;
int ledVerde = 12;
char input[25];
byte contador = 0;
int verificacao; 

const byte ROWS = 4; 
const byte COLS = 4; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[COLS] = {5, 4, 3, 2}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  pinMode(ledVermelho, OUTPUT);
  pinMode(ledVerde, OUTPUT);
}
  
void loop(){
  char customKey = customKeypad.getKey();
  if (customKey){
    input[contador] = customKey;
    contador++;
  }

  if (contador == 24){
    verificacao = verify(input);
    if (verificacao == 0){
      digitalWrite(ledVerde, HIGH);
      digitalWrite(ledVermelho, LOW);}
      clearData();
    if (verificacao == 1){
    digitalWrite(ledVerde, LOW);
    digitalWrite(ledVermelho, HIGH);}
    clearData();
  }

  if (contador > 24){
    digitalWrite(ledVerde, LOW);
    digitalWrite(ledVermelho, HIGH);}
    clearData();
  }


void clearData() {
  while (contador != 0) {
    input[contador--] = 0;
  }
  return;
}

Verify function:

int verify(char chave[24]){
  Serial.println("entrei");
  int contador2;
  int i2;
  int points = 0;
  if ((chave[0] <= chave[8]) && (chave[0] <= chave[16]) && (chave[8] >= chave[16])){
    return 1;
  }

  else {
      char val1[2] = "0";
      char val2[2] = "0";
      char val3[2] = "0";
      float eigvet1[3] = {0.0, 0.0, 0.0};
      float eigvet2[3] = {0.0, 0.0, 0.0};
      contador2 = 0;

      while (contador2 < 17){
        char temp[3] = "00";
        temp[0] = chave[contador2];
        temp[1] = chave[contador2 + 1];
        int singtemp = atoi(temp);
        float singval = (float)singtemp;
        float eigval = ((singval/10.0) * (singval/10.0));
        if (contador2 == 0){
              val1[0] = chave[5];
              val2[0] = chave[13];
              val3[0] = chave[21];
              eigvet2[0] = atoi(val1);
              eigvet2[1] = atoi(val2);
              eigvet2[2] = atoi(val3);
              val1[0] = chave[contador2 + 1];
              val2[0] = chave[contador2 + 2];
              val3[0] = chave[contador2 + 3];
              eigvet1[0] = atoi(val1);
              eigvet1[1] = atoi(val2);
              eigvet1[2] = atoi(val3);
        }
        if (contador2 == 8){
              val1[0] = chave[6];
              val2[0] = chave[14];
              val3[0] = chave[22];
              eigvet2[0] = atoi(val1);
              eigvet2[1] = atoi(val2);
              eigvet2[2] = atoi(val3);
              val1[0] = chave[contador2 + 1]*(-1.0);
              val2[0] = chave[contador2 + 2];
              val3[0] = (chave[contador2 + 3]);
              eigvet1[0] = atoi(val1);
              eigvet1[1] = atoi(val2);
              eigvet1[2] = atoi(val3);
        }
        if (contador2 == 16){
              val1[0] = chave[7];
              val2[0] = chave[15];
              val3[0] = chave[28];
              eigvet2[0] = atoi(val1);
              eigvet2[1] = atoi(val2);
              eigvet2[2] = atoi(val3);
              val1[0] = chave[contador2 + 1]*(-1.0);
              val2[0] = chave[contador2 + 2]*(-1.0);
              val3[0] = (chave[contador2 + 3]);
              eigvet1[0] = atoi(val1);
              eigvet1[1] = atoi(val2);
              eigvet1[2] = atoi(val3);
        }

        eigvet1[0] = (eigvet1[0]/10);
        eigvet2[0] = (eigvet2[0]/10);
        eigvet1[1] = (eigvet1[1]/10);
        eigvet2[1] = (eigvet2[1]/10);
        eigvet1[2] = (eigvet1[2]/10);
        eigvet2[2] = (eigvet2[2]/10);

        float result11[3] = {eigval * eigvet1[0], eigval * eigvet1[1], eigval * eigvet1[2]};
        float result12[3] = {eigval * eigvet2[0], eigval * eigvet2[1], eigval * eigvet2[2]};
        float result21[3] = {29.1, 18.9, 7.6};
        float result22[3] = {7.1, 5.2, 30.4};
        for (i2 = 0; i2 < 3; i2++)
        {
          if (((int)result11[i2] - (int)result21[i2]) < 1){
            points++;
          }
          if (((int)result12[i2] - (int)result22[i2]) < 1){
            points++;
          }

        }
        
        contador2 = contador2 + 8;

        }

      if (points == 18){
        return 0;
      }
      }

  }

Welcome to the forum

Is that the whole code or did you forget to post some of it ?

Assuming you did have a complete sketch functioning, I think the problem is with

  char customKey = customKeypad.getKey();

You don't say what keypad library is included. getKey() says what key is being pressed upon, and the loop is so fast you get 24 characters all from one humanly brief press.

Put

  Serial.begin(115200);

in your setup() function, and put some serial printing in your loop code, you might see those characters rapidly piling up and showing as a not correct password.

There are ways to see when a key gets pressed, rather than reacting to the fact that it is (still) pressed.

HTH

a7

Thank you!

Is that the whole code or did you forget to post some of it ?

The whole code that is related to the arduino. The rest of the code is simply the verify function which checks if the password is correct.

The library is Keypad.h. Something that has been very strange is I can't get the serial monitor to work. For some reason, it just doesn't print anything. I tried configurating as the website says, but it just stays blank.

Hey guys. I figured out the error. It turns out the usb was not properly connected to my computer. So I think the upload was not being done. Anyway, now, I tried placing the correct password and no LEDs light up. Perhaps it is due to the verify function?
I tested it isolatedly on C tutor and it worked fine.

Post that code here.

Post your doesn't work yet Arduino version as a complete sketch.

Did printing as I suggested reveal anything?

a7

No it isn't

What about #incluing a library, for instance ?
Please post the whole sketch

Hello alto777. Sorry for the late response. I hope it is not too late tgo get back to you.
I did try printing and that seemed to reveal what is going on. My Keypad isn't functioning properly. Whenever I press a key, two or more characters are printed on the serial monitor. And the password only has decimal numbers, but this is what is passed to the verify() function:
6789CCC5D0987C5D#7DD55DB
That is clearly not right. Is my Keypad broken? Or do I just need to debounce it?
Thank you once more.

Hello. I just included the verify() function. But I believe the problem is due to the Keypad not functioning properly. The password is supposed to have only decimal numbers, and when I tried typing it in, this is what was passed on to the verify function:
6789CCC5D0987C5D#7DD55DB
That is not correct, and the verify() function can't process hexadecimal numbers. Is my Keypad broken? Or do I just need to debounce it?
Thank you.

You seem to have gone out of your way to make things complicated

Why not put each character read from the keypad into an array of chars as they are received and add a '\0' to the end of the input array (note that the array will need to be 25 chars in length) to turn input into a C style string (note, NOT a String) then use strcmp() to compare input with the required password ?

I cannot do that, since this project is for a cyber security reverse engineering challenge. Doing that would just make it too easy and not secure at all.

The verify function is working. I just need a way to make the input system work properly.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

How does your method make it more secure ?

Hello Tom. I finally found my mistake. It was just a silly beginner mistake. Thank you for your help.

It is not trivial to find the input that makes the verify function return 0, which is what makes the green led light up. And there is only one correct answer.

Are you assuming that someone who is trying to guess the password will have access to the source code ?

If so then they could, of course, simply modify the code to do what they want

Neither would it be simple to guess the 24 character password where each character could be one of about 70 characters without access to the source code

Yes. Like I said, it is a reverse engineering challenge. You have to try to understand what the code is doing to find the correct input. The goal is to find the correct input, not create your own.

Yes, but that would not help you find the correct answer.

I was also wondering if there is a way to get the hex code from the arduino and find the sketch file. Is it possible?

You can get the hex file from the Arduino but you cannot recreate the sketch from it

No but if it was worth the time, enough could be reversed out to determine the password algorithm.

Which shouldn't be a problem. Real security does not depend on obscurity.

The erfication function looks odd of course. I wonder if the password could be reverse engineered from an examination of that function, be it source code or combing through the machine code.

a7