LCD displaying nonsense

I wired an LCD to my Arduino like it's shown here https://www.arduino.cc/en/Tutorial/LiquidCrystalDisplay
With the code provided the display works, but when I put in my code it shows random characters. The LCD starts fine, but then the random characters slowly fill the screen. The program then freezes and doesn't clear nor display the next part (lcd.print("Preparazione SD..."); ). What did I do wrong?

This is only the start of the program since it's the only part (so far) that's giving me trouble.

    lcd.display();
    lcd.begin(16, 2);
    lcd.clear();
    lcd.print("LCD inizializzato");
    delay(2000);
  
    pinMode(faucet, OUTPUT);
    pinMode(pin, INPUT);
    pinMode(relay, OUTPUT);
    digitalWrite(relay, LOW);
    
    pinMode(currentF, OUTPUT);
    pinMode(currentP, OUTPUT);
    digitalWrite(currentF, HIGH);
    digitalWrite(currentP, HIGH);

    lcd.clear();
    lcd.print("Preparazione SD...");
    delay(1000);

Show the entire sketch.
It is impossible for us to evaluate what is happening when we can't see all the code.

--- bill

Here it is

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

File valuesFile;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//var & const
const int ns=6; //ns (number of sensors) can be edited but do not forget to add or delete extra sensors in sensors!
unsigned int sensorsReading;
unsigned int minimum = 1024;
unsigned int maximum = 0;
unsigned int sensors = analogRead(A0) + analogRead(A1) + analogRead(A2) + analogRead(A3) + analogRead(A4) + analogRead(A5);

//pins
int faucet = 0;
int pin = 2;
int currentF = 1, currentP = 3;
int relay = 4; //THIS RELAY IS ONLY FOR THE SENSORS NOT FOR THE FAUCET



void setup() {
  //first time setup
    lcd.display();
    lcd.begin(16, 2);
    lcd.clear();
    lcd.print("LCD inizializzato");
    delay(2000);
  
    pinMode(faucet, OUTPUT);
    pinMode(pin, INPUT);
    pinMode(relay, OUTPUT);
    digitalWrite(relay, LOW);
    
    pinMode(currentF, OUTPUT);  //for toggle buttons faucet and pin
    pinMode(currentP, OUTPUT);
    digitalWrite(currentF, HIGH);
    digitalWrite(currentP, HIGH);

    lcd.clear();
    lcd.print("Preparazione SD...");
    delay(1000);
    //if file exists already restore values from backup
    
    //open file
    valuesFile = SD.open("values.txt", FILE_READ);
    //grab if file is online
    if (valuesFile) {
      minimum = valuesFile.parseFloat();
      maximum = valuesFile.parseFloat();
      valuesFile.close();
      lcd.clear();
      lcd.print("Dati recuperati dall'SD con successo");
      delay(2000);
    }
    else { //first time no file setup
      unsigned int i;
      lcd.clear();
      lcd.print("Non è stato possibile leggere i dati dall'SD.");
      delay(2000);
      lcd.clear();
      lcd.print("E' necessario iniziare un nuovo ciclo di registrazione dati");
      delay(2000);
      for(i=0;i<8;i++) {
        lcd.clear();
        lcd.print("Inizio registrazione dati ciclo ");
        lcd.print(i+1);
        lcd.print("/8?");
        delay(2000);
        //wait till button is pressed
        while(digitalRead(pin) != LOW) {}
        
        //sensors on then get value for max and min then sensors off
        digitalWrite(relay, HIGH);
        delay(1000);
        minimum += sensors;
        delay(1000);
        
        lcd.clear();
        lcd.print("Fine registrazione dati ciclo ");
        lcd.print(i+1);
        lcd.print("/8?");
        while(digitalRead(pin) != LOW) {}
        maximum += sensors;
        delay(1000);
        digitalWrite(relay, LOW);
        }
      
      lcd.clear();
      lcd.print("Registrazione dati completa!");
      delay(2000);
      
      //mean value of sensors
      minimum = minimum/ns;
      maximum = maximum/ns;

      //write them to the sd
      valuesFile = SD.open("values.txt", FILE_WRITE);
  
      if (valuesFile) {
        valuesFile.println(minimum);
        valuesFile.println(maximum);
        valuesFile.close();
        lcd.clear();
        lcd.print("E' possibile disconnettere l'SD");
        delay(2000);
      }
      else {
        lcd.clear();
        lcd.print("Non è stato possibile scrivere i dati sull'SD.");
        lcd.print(" ");
        lcd.print(maximum);
        lcd.print(" ");
        lcd.print(minimum);
        delay(10000);
      }
    }
}

void loop() {
  //sensors on
  lcd.clear();
  lcd.print("Registrazione dati in corso...");
  delay(2000);
  digitalWrite(relay, HIGH);
  delay(1000);
  
  //get values
  sensorsReading = (sensors)/ns;
  
  if(sensorsReading <= minimum) {
    digitalWrite(faucet, HIGH);
    lcd.clear();
    lcd.print("Rubinetto aperto");
    delay(500);

  }
  if(sensorsReading >= maximum) {
    digitalWrite(faucet, LOW);
    lcd.clear();
    lcd.print("Rubinetto chiuso");
    delay(500);
  }
  
  //sensors off
  digitalWrite(relay, LOW);
  lcd.clear();
  lcd.print("Standby");
  delay(2000);
  delay(120000); //2min delay
}
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pin = 2;
   pinMode(pin, INPUT);

:wink:
--- bill

There are more pins redefined.

Also, pin 1 is RX.

.

bperrybap:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pin = 2;
   pinMode(pin, INPUT);

:wink:
--- bill

LarryD:
There are more pins redefined.

Also, pin 1 is RX.

.

The code works now. Thank you very much!