Issues with code

I'm trying to create a lock
And while making the code I've encountered 2 annoying issues:

  1. A Serial.println() function from a different function runs without being called
  2. every time the same Serial.println is being called, it removes the first letter of the string for some reason

These are the 2 problematic functions, the first one is the getCodeInput() function - which triggers the Serial.println() function from a different function INSTEAD OF THE ONE IT ACTUALLY NEEDS TO PRINT

void getCodeInput(int passLength, int amountOfNums, int* userInput)
{
  int i = 0;
  int aButtonPressed = HIGH;
  for(i = 0; i < AMOUNT_OF_NUMBERS; i++)
  {
    aButtonPressed = HIGH;
    aButtonPressed = digitalRead(numKeys[i]); //Checking on each number if it has been pressed, if yes - run the rest of the code
    if (aButtonPressed == LOW)
    {
      if (count < PASSWORD_LENGTH)
      {
        userInput[count] = i;
        count++;
        Serial.println(count + "^");
      }
      break;
    }
  }
}

The second one is getNewPassword, which has the problematic Serial.println() command (the one that says "CHANGE PASS"), each time the first function calls it - it runs the Serial.println() command and each time it removes the first letter of the string

In the first time the first function runs after a user's input reset - it does not print anything upon getting an input (and it needs to print the input thst the user entered so far) - not even the "CHANGE PASS" string

void getNewPassword(int passLength, int* userInput, int numCount, int* oldPassword)
{
  resetInput(userInput, PASSWORD_LENGTH);
  Serial.print("CHANGE PASS\n");
  while (count != passLength)
  {
    getCodeInput(passLength, numCount, userInput);
    delay(1000);
  }
  changePass(oldPassword, userInput, passLength);
  printArray(oldPassword, PASSWORD_LENGTH);
}

Here is an output example so you'll be able to see the issue (-1 is a placeholder for not having anything inserted there)

CHANGE PASS

HANGE PASS

ANGE PASS

1101
FAILURE

CHANGE PASS

HANGE PASS

ANGE PASS

1233
FAILURE

Extra info:

  • The only time that the getNewPassword is being called is when the user enters a specific sequence of number AFTER getting the normal password correctly, and here the Serial.println in the function is being printed because of the getCodeInput function.
    -The arduino board is Arduino Uno.
    -The getCodeInput function is being called inside the getNewPassword function, but I do not call it upon a code fail.
    -The code writes "FAILURE" every time the password entered was incorrect - not the sequence to change the password

Please help me, thanks!

Welcome to the forum

        Serial.println(count + "^");

What should this line of code do ?
What does this line of code actually do ?
What type of variable is count and where is it declared and defined ?

1 Like

Serial.print(count); Serial.println("^"); would probably have a better output ...

that line of code should print count and then an arrow up
it prints a completely different Serial.println() line that is located in the second function
count is an integer

You are probably thinking of the too-frequent abomination

Serial.println(String(count) + String("^"));

which is preferably done with the Streaming library, as

Serial << count << "^" << endl;

The Arduino print function can only print one value, please consult the documentation on this site.

That's what you hoped it would print but count + "^" is equivalent to "^"[count], treating the string literal as a character array.

count == 0 gets you "^".
count == 1 gets you "". (Empty string)
count == 2 gets you the string literal after "^" in memory (apparently "CHANGE PASS")
count == 3 gets you the string literal after "^" in memory, starting at the second character ("HANGE PASS").
count == 4 gets you the string literal after "^" in memory, starting at the third character ("ANGE PASS").

alright thank you, that's good to know lol

so you're telling me that what happens is that the incensement of count makes the code to overflow to the area of the memory in which CHANGE PASSWORD is in?

Yes. Adding an integer to a pointer moves the pointer that number of bytes. The pointer "^" points to a two-byte string containing a '^' and a null/zero byte to signal the end of the string. When you move the pointer past the end of the string you are pointing to whatever is next in memory. In this case, it is the string "CHANGE PASSWORD".

I see, thank you!
I'll try to change that and let you know if it works

that worked! thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.