LCD keeps scrambling after some time; HW or Code?

My LCD readout randomly scrambles until RESET is pressed, upon which it goes back to proper function until it scrambles again. The code I'm using is many different functions that I "spliced together" to achieve my desired functions. It is still in progress, so my notes are visible. Someone told me that it could be a faulty LCD, but since it works after a RESET, I have to think my code is causing it...? The goal is for a PH sensor to read the PH of a solution every 6 seconds (eventually up to 10 minutes after it works correctly) then adjust PH from a pump (relay powered, pin 6-7,).


#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);
int sensorPin = A1; // select the analog input pin for the pH sensor
int sensorValue = 0; // variable to store the value coming from the sensor
float ad7 = 399.0; // change this value to the one on serial monitor when in pH7 buffer
float ad4 = 333.0; // change this value to the one on serial monitor when in pH4 buffer
float phDesired = 6.0;
float phThresh = 0.2;
float adjustVol = 1.; // mL
float modifierSec1 = 1.; // must calibrate this
int waitBetweenAdjustments = 600000; // ms
int primeTime = 1000; // pump priming
int loopDelay = 6000; // main loop polling

void setup()
{
lcd.begin(16,2);
Serial.begin(9600);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT); // delete if only one pump

}
//dose volume
float calibrate_volume(int volM1) {
return 1.;
}
void addVolume(int pump, float volM1) {
  
float timeMs = modifierSec1 * volM1;
  
  digitalWrite(pump,HIGH);
  delay(timeMs);
  digitalWrite(pump,LOW);
}

void addPhModifier(int pump) {
  addVolume(pump, adjustVol);
}

void primePumps() {
  digitalWrite(1,HIGH); 
  digitalWrite(2,HIGH); // delete if only one pump
  delay(primeTime);
  digitalWrite(1,LOW);
  digitalWrite(2,LOW); // delete if only one pump
}

void loop()
{
Get_pH();
}
///////////////////////////////////////////
void Get_pH()
{
int currentValue = 0;
for (int i = 0; i < 10; i++)
{
currentValue += analogRead(sensorPin);
delay(100);
}
sensorValue =(currentValue / 10);
lcd.setCursor(0,1);
lcd.print(sensorValue);
float m =(-3.0/(ad4-ad7));
//Serial.println(m);
float c = 7-(m*ad7);
//Serial.println(c);
float a =((m*sensorValue)+c);
lcd.print("PH = ");
lcd.print(a);
delay(10000);
}

void phModify() {

  if ((sensorValue-phDesired)>phThresh) {
    addPhModifier(1); // add ph down
    delay(waitBetweenAdjustments);
  } else if ((sensorValue-phDesired)< -phThresh) {
    addPhModifier(2);
    delay(waitBetweenAdjustments);
  }
  delay(loopDelay);
}
///////////////////////////////////////////

I suspect that the main reason is that you use pin number 2 for both a pump and the display.

Do not use (so-called) magic numbers for pins. Place something like below before setup()

// pump pin numbers
const byte pumpOne = 1;
const byte pumpTwo = 2;

And use that everywhere. that way you can have a list of pin numbers near the top of the code and there is less risk that you use a pin for two things in your code.

Thank you

I changed the pins as stated. But the problem still remains. Any other ideas?

Hi,
You are also using Pin1, this is a programming pin, you need to use a different pin.
Avoid pins 0 and 1.

Tom... :grinning: :+1: :coffee: :australia:

Hi,
Another tip.

// pump pin numbers
const byte pumpOne = 1;
const byte pumpTwo = 2;

Make the names have a purpose.

// pump pin numbers
const byte pumpOnePin = 1;
const byte pumpTwoPin = 2;

This identifies them as pin numbers and not variables or other constants.
Of course in the above example don't use 1 or 2.

Tom... :grinning: :+1: :coffee: :australia:

So the LCD kept scrambling, even after changing the pins, but only one cell would scramble at this point. Someone said to try a different type of LCD (i2C) and see what happens. I swapped the LCDs and corresponding code and the system has gone 24hrs with no issue. Thanks for the replies!

1 Like

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