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);
}
///////////////////////////////////////////