Serial.reading() problems? On tinkercad works perfectly well.

Hello once again.

I’m trying to send and receive encrypted words/phrases/data from one arduino to another through bluetooth module hc05.

The problem I am facing is the following code.

The objective is to enter the right key to decryp the data I’m receiving.

When I enter A , it works perfectly and asks me for the second letter but then when I enter B it gives me the error, it enters the else: (B) Caracter errado. Reinicie o serial monitor. and also does not “reset” the code.

I’m calling this void on the void loop (no need to show it since I don’t have anything else there, unless you really want me to! Let me know).

I tried this code on tinkercad and it worked 100% with no errors. All the letters worked perfectly well and if I entered the wrong one it would reset so I could enter the A letter again.

Not sure what I’m missing here!

Thanks in advance.

void desencript() {

  Serial.println("Insira a chave para desencriptar: ");
  delay(10000);
 char data = Serial.read();

  if (data == 'A')
  {
    Serial.println("Correto. Insira o 2º caracter");
    delay(10000);
    char data1 = Serial.read();

    if (data1 == 'B')
    {
      Serial.println("Correto. Insira o 3º caracter");
      delay(20000);
      char data2 = Serial.read();

      if (data2 == 'C')
      {
        Serial.println("Chave aceite.");
        delay(20000);
        Serial.println("Desencriptação iniciada.");
        delay(500);

        do
        {
          digitalWrite(12, HIGH);

        }
        while (data2 == 'C');
      }
      else {
        digitalWrite(12, LOW);
        Serial.println("(C) Caracter errado. Reinicie o serial monitor.");
      }
    }
    else {
      digitalWrite(12, LOW);
      Serial.println("(B) Caracter errado. Reinicie o serial monitor.");
    }
  }
  else {
    digitalWrite(12, LOW);
    Serial.println("(A) Caracter errado. Reinicie o serial monitor.");
  }
  //Serial.flush();
}

OK, so you receive an 'A' and straight after that you read again; who guarantees that there is anything available at that moment?

  if (data == 'A')
  {
    Serial.println("Correto. Insira o 2º caracter");
    delay(10000);
    char data1 = Serial.read();

An other problem is

        do
        {
          digitalWrite(12, HIGH);

        }
        while (data2 == 'C');

data2 will never change so you will get stuck in that while-loop

A very rough way to solve the first problem, not recommended.

  if (data == 'A')
  {
    Serial.println("Correto. Insira o 2º caracter");
    delay(10000);
    while(Serial.available() == 0)
    {
    }
    char data1 = Serial.read();

And the better way is to use a finite state machine / sequencer. You start with a state where you inform the user. Next you go to the next state/step and process the received character. This repeats. If there is an error, the variable state is set to 0 so the process starts from the beginning. In the last step, we simply ignore what is received and go back to the beginning.

void desencript()
{
  // variable to remember the state/step
  static byte state = 0;

  // get serial data
  int serialData = Serial.read();

  // if nothing received
  if (serialData == -1)
  {
    // nothing to do
    return;
  }

  switch (state)
  {
    // tell user to start
    case 0:
      Serial.println("Insira a chave para desencriptar: ");
      // go to next state / step
      state++;
      break;
    // process first character
    case 1:
      if (serialData == 'A')
      {
        Serial.println("Correto. Insira o 2º caracter");
        // next state / step
        state++;
      }
      else
      {
        digitalWrite(12, LOW);
        Serial.println("(A) Caracter errado. Reinicie o serial monitor.");
        // back to the beginning
        state = 0;
      }
      break;
    // process second character
    case 2:
      if (serialData == 'B')
      {
        Serial.println("Correto. Insira o 3º caracter");
        state++;
      }
      else
      {
        digitalWrite(12, LOW);
        Serial.println("(B) Caracter errado. Reinicie o serial monitor.");
        state = 0;
      }
      break;
    // process third character
    case 3:
      if (serialData == 'C')
      {
        Serial.println("Chave aceite.");
        Serial.println("Desencriptação iniciada.");
        state++;

        digitalWrite(12, HIGH);
      }
      else
      {
        digitalWrite(12, LOW);
        Serial.println("(C) Caracter errado. Reinicie o serial monitor.");
        state = 0;
      }
      break;
    // process any character to stop and go to the beginning
    case 4:
      // if any new data is received in this state, we will go back to the beginning
      state = 0;
      break;
  }
}

Code compiles but is not tested.

Note 1: Robin's updated https://forum.arduino.cc/index.php?topic=396450.0 might give you some other ideas for handling serial data.

Note 2: If it comes to security, you should never tell a user which character in a 'password' is correct ;)

Thanks in advance! I will test you and tell you if it worked!

For other various example sketches for reading text from Serial take a look at my Arduino Software Solutions tutorial.
Has examples for single char reads as well as Strings both blocking and non-blocking examples.