Simple Counting Program Glitches LCD display and shows weird characters

I’m not entirely sure why this could be happening. I believe I’m using all of the right data types, but for some reason after a random amount of time my board starts displaying gibberish on the LCD screen. I’ve never used an LCD display before so let me know if I’m doing something obviously wrong.

the idea of the program is to use a limit switch to make contact with a bag going down a conveyor belt and then increment the count on the LCD when it does so. I wired up the LCD screen and wrote its code based on this video I found: Arduino LCD Tutorial | How To Control An LCD - YouTube

Is the issue that I have the screen wired up for 4 bits? I don’t think that would explain why I’m getting this error on numbers as low as 5 in the count.

Also, the error only happens at the point where the cursor is reset to (7, 0) not over the entire screen.

#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
const int resetPin = 8;
const int bagPin = 13;
bool bag;
int count;
bool present;
long Time;
long resetTime;

void setup() {
  // put your setup code here, to run once:
pinMode (resetPin, INPUT);
pinMode (bagPin, INPUT);
lcd.begin(16,2);
lcd.print("COUNT: 0");
Time = 0;
}

void loop() {
  // put your main code here, to run repeatedly:
//allow user to reset the counter
if ((digitalRead(resetPin) == HIGH)) {
  if (resetTime == 0) {
    resetTime == millis();
  };
}else {
  resetTime == 0;
};

if ((digitalRead(resetPin) == HIGH) && ((millis() - resetTime) >= 1000)) {
  count = 0;
  lcd.clear();
  lcd.print("COUNT: 0");
};

//check if a bag has contacted the switch
if (digitalRead(bagPin) == HIGH) {
  //check if it was already there
  if ((present == false) && ((millis() - Time) > 700)){
    count = count + 1;
    Time = millis();
    //display the count on the LCD
    lcd.setCursor(7,0);
    lcd.print(count);
    
    //set present to true to stop the counter
    present = true;
  };
} else {
  //if there is no bag nearby, then set present to false
  if ((millis() - Time) > 300){ 
    present = false;
    Time = millis();
  };
};
}

All of the timing variables (anything used with millis()) should be declared unsigned long.

noiasca:
pin 1 is a very bad choice for a LCD pin because pin 1 (and 0) should be used for serial.
You will need serial for debugging your code.

Follow EXACTLY the wiring on

https://www.arduino.cc/en/Tutorial/LibraryExamples/HelloWorld

upload the Hello World sketch and see - if it is working.

If not: post pictures of your arduino + LCD in a way where we can see CLEARLY each and every wire.

groundFungus:
All of the timing variables (anything used with millis()) should be declared unsigned long.

Thank you both for the help! I made the changes you both recommended and so far so good. I didn’t know that the serial pin could cause this issue since I never started serial output, but I suppose you live and you learn. Here’s my updated code. So far it is working fine. I didn’t do the “Hello world” exercise because the error only seems to happen after a certain amount of reprinting to the screen.

#include <LiquidCrystal.h>
LiquidCrystal lcd(10, 2, 4, 5, 6, 7);
const int resetPin = 8;
const int bagPin = 13;
bool bag;
int count;
bool present;
unsigned long Time;
unsigned long resetTime;

void setup() {
  // put your setup code here, to run once:
pinMode (resetPin, INPUT);
pinMode (bagPin, INPUT);
lcd.begin(16,2);
lcd.print("COUNT: 0");
Time = 0;
}

void loop() {
  // put your main code here, to run repeatedly:
//allow user to reset the counter
if ((digitalRead(resetPin) == HIGH)) {
  if (resetTime == 0) {
    resetTime == millis();
  };
}else {
  resetTime == 0;
};

if ((digitalRead(resetPin) == HIGH) && ((millis() - resetTime) >= 1000)) {
  count = 0;
  lcd.clear();
  lcd.print("COUNT: 0");
};

//check if a bag has contacted the switch
if (digitalRead(bagPin) == HIGH) {
  //check if it was already there
  if ((present == false) && ((millis() - Time) > 300)){
    count = count + 1;
    Time = millis();
    //display the count on the LCD
    lcd.setCursor(7,0);
    lcd.print(String(count));
    
    //set present to true to stop the counter
    present = true;
  };
} else {
  //if there is no bag nearby, then set present to false
  if ((millis() - Time) > 300){ 
    present = false;
    Time = millis();
  };
};
}

So, it's not fixed, and there is a new revelation. I ignored the cursor commands assuming they wouldn't factor in, however it turns out that this error occurs when the block-style cursor appears on its own. I don't really know why a cursor would appear without being called, or why it takes a random amount of time to appear, disappear, or why it breaks the LCD translation when I try to print the the screen while it is there. Regardless, I've added lcd.noBlink() and lcd.noCursor() to hopefully stop this.

I've also noticed that when this happens, trying to print to the screen results in more gibberish.

nevermind again, again. I found an older post from someone with the same issue. Their issue was power interference on the LCD (because I guess these LCDs are very poor at handling power issues) and the solution for them was to add a 4.7 uF capacitor across pins 1 and 2 (VCC and VDD). I didn’t have a capacitor that large so I’m using a 0.2 uF capacitor. The issue still occured, but now only on the second line of the LCD screen. I Then disabled the second line of the LCD and tried out something quite silly. I removed the plastic film from around the LCD. I had left it on not thinking it would do anything, but it actually had a small amount of adhesive on the back, and many adhesives are great at carrying charge. It does seem to be working now, but only time will tell. Here’s my new code with adjustments I’ve made (and an unused variable for later).

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 4, 5, 6, 7);
const int resetPin = 8;
const int bagPin = 13;
bool bag;
int count;
bool present;
unsigned long Time;
unsigned long resetTime;
                      
void setup() {
  // put your setup code here, to run once:
pinMode (resetPin, INPUT);
pinMode (bagPin, INPUT);
lcd.begin(16,1);
lcd.noBlink();
lcd.noCursor();
lcd.print("COUNT: 0");
Time = 0;
resetTime = 0;
}

void loop() {
lcd.noBlink();
lcd.noCursor();

//check if a bag has contacted the switch
if (digitalRead(bagPin) == HIGH) {
  //check if it was already there
  if ((present == false) && ((millis() - Time) > 300)){
    count = count + 1;
    Time = millis();
    //display the count on the LCD
    lcd.setCursor(7,0);
    lcd.print(count);
    
    //set present to true to stop the counter
    present = true;
  };
} else {
  //if there is no bag nearby, then set present to false
    present = false;
    Time = millis();

};
}