How to make an LCD light up only when TCRT5000 infrared sensor breaks

So, i have a project for my Uni class where i am using an infrared sensor (TCRT5000) and an LCD. I would like for a message to pop up on the LCD only after the TCRT5000 registers a break. However, instead of the actual text popping up, the LCD just shows me "scrambled" letters...

Here's my code so far:

#include <LiquidCrystal.h>


const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int OUT = 7;
int LED = 13;
unsigned long tidSjekk = 0;

void setup() {
 
  lcd.begin(16, 2);
  pinMode(OUT, INPUT);
  pinMode(LED, LOW);

  Serial.begin(9600);
}


void loop() {

  int sensorValue = digitalRead(OUT);
  if (sensorValue == 0) {
    Serial.println("black color");
    tidSjekk = millis() + 5000;
    while (tidSjekk > millis()) {
      digitalWrite(LED, HIGH);
      lcd.print("Tusen takk :)");
    }
    digitalWrite(LED,LOW);
    lcd.clear();
  }
  if (sensorValue == 1) {
    Serial.println("other colors");
  }
  delay(500);
}

Hi! Welcome to the forum.

This looks weird... I guess you meant OUTPUT instead of LOW.

You're not setting the position of the beggining of the text, neither cleaning the screen after some writing, so each time you write on the LCD it will start where it stops and overwrite what was there before. Take a special look here:

2 Likes