Lcd.clear() wont work in an if statement

Hello there. I'm creating a surveillance system, consisting of an Arduino Uno R3, an 16x2 LCD and an MFRC522 RFID Tagreader.

The goal of the following program is displaying two different cases on my LCD.
The Lines written in my setup function, get printed correctly. But neither of the two cases programmed in the function "Entscheidung", are getting displayed. I have checked that the if works as intended (Serial prints the right line). But my display acts weirdly. The first time I scan a card, nothing happens. After repeating this process, the display gets shifted to the left or right depending on the case getting triggered and some of the letters turn into symbols.

/*
 * 
 * All the resources for this project: https://randomnerdtutorials.com/
 * Modified by Rui Santos
 * 
 * Created by FILIPEFLOP
 * 
 */

#include <SPI.h>
#include <MFRC522.h>
#include <LiquidCrystal.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.

LiquidCrystal lcd(8, 9, 5, 4, 3, 2);

byte AUmlaut[8] = {
  0b01010,
  0b00000,
  0b01110,
  0b00001,
  0b01111,
  0b10001,
  0b01111,
  0b00000
};

byte UUmlaut[8] = {
  0b01010,
  0b00000,
  0b10001,
  0b10001,
  0b10001,
  0b10011,
  0b01101,
  0b00000
};

void setup() {
  lcd.begin(16, 2);
  lcd.createChar(1, AUmlaut);
  lcd.createChar(2, UUmlaut);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" Schl");
  lcd.write(2);
  lcd.print("sselkarte");
  lcd.setCursor(0, 1);
  lcd.print("! Pr");
  lcd.write(1);
  lcd.print("sentieren !");

  SPI.begin();         // Initiate  SPI bus
  mfrc522.PCD_Init();  // Initiate MFRC522

  Serial.begin(9600);  // Initiate a serial communication
  Serial.println("Approximate your card to the reader...");
  Serial.println();
}
void loop() {

  if (!mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  // Select one of the cards
  if (!mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content = "";
  byte letter;

  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();

  if (content.substring(1) == "12 7C 72 34")  //change here the UID of the card/cards that you want to give access
  {
    Entscheidung(true);
    Serial.println("Authorized access");
    Serial.println();
    delay(3000);

  }

  else {
    Entscheidung(false);
    Serial.println(" Access denied");
    Serial.println();
    delay(3000);
  }
}
void Entscheidung(bool stimmt) {
  if (stimmt) {
    Serial.println("1. Fall");
    lcd.setCursor(0, 0);
    lcd.clear();
    lcd.print("geschafft!");
    delay(3000);
  } else {
    Serial.println("2. Fall");
    lcd.setCursor(0, 0);
    lcd.clear();
    lcd.print("nicht geschafft!");
    lcd.setCursor(0, 1);
    lcd.print("Geschafft oder");
  }
}

I tried using a different Sketch with the same Setting of Wires and Components, wich showed me, that everything is wired correctly. Can anyone see what's the Problem here?
Thanks a lot beforehand,
Bryan

Welcome to the forum

Only one of the cases in that function displays text on the LCD. In addition, you have redundant code when stimt is false because lcd.clear also set the cursor to 0,0 although you don't need to do that anyway because you immediately set the cursor to 0, 1

What happens if you call Entscheidung(false); as first statement in loop()
What happens if you call Entscheidung(true); as first statement in loop()

There are two statements in begin of loop() that return immediately.
So if neither one is met there will be no call to Entscheidung()

@UKHeliBob
Thanks, the setCursor command will get fixed by tomorrow, I won‘t have access to my system until then.

I uploaded it first whitout the print command and edited into my Code afterwards. Maybe thats why, I only had one if Statement, printing onto my LCD.

@robtillaart
I havent tested that yet. I‘m gonna give you an update, as soon, as I implent it, so by tomorrow morning.
But the Serial command allready showed that both cases get triggered

You have a conflict with pin 9 ...

1 Like

@MicroBahner
Thanks a lot. That will probably solve it. I give y‘all an update by tomorrow Morning

Hello There. Thanks for the fast help of you. The Problem is solved, so this topic can be closed.

A topic cannot be “closed”. You can mark the post that helped you best as the 'solution' - this way everyone can see that no further help is needed.

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