unsigned long previousMillis = 0; // Store the last time the code was executed
const long interval = 1000;
int currentAction = 0;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <SoftwareSerial.h>
#define ECHO_PIN1 2
#define ECHO_PIN2 9
#define TRIGGER_PIN1 3
#define TRIGGER_PIN2 8
#define pum 7
#define sva 4
#define svb 5
#include <Wire.h>
#include <SimpleTimer.h>
int sensorPin = A0;
float tvolt;
float ntu;
SimpleTimer timer;
float Turbidityvoltage;
float MAX_HEIGHT_CM = 27.0;
float calibration_value = 21.34 + 4.3;
int phval = 0;
unsigned long int avgval;
int buffer_arr[10], temp;
float ph_act;
namespace pin {
const byte tds_sensor = A1;
}
namespace device {
float aref = 5.0;
}
namespace sensor {
float ec = 0;
unsigned int tds = 0;
float waterTemp = 33;
float ecCalibration = 1;
}
int turbidit;
float duration1;
int waterlevel1;
float duration2;
int waterlevel2;
int distance1;
int distance2;
float percentage2;
float percentage1;
long initialDisplayTime = 2000;
unsigned long nextDisplayTime;
int displayContent = 0, displayDelay = 2000;
const int displaySlides = 4;
void setup() {
Serial.begin(9600);
Wire.begin();
lcd.init();
lcd.backlight();
lcd.home();
lcd.setCursor(0, 0);
lcd.print("Water Quality");
lcd.setCursor(0, 1);
lcd.print("& Level Monitor");
delay(1500);
lcd.clear();
pinMode(TRIGGER_PIN1, OUTPUT);
pinMode(ECHO_PIN1, INPUT);
pinMode(TRIGGER_PIN2, OUTPUT);
pinMode(ECHO_PIN2, INPUT);
pinMode(pum, OUTPUT);
pinMode(sva, OUTPUT);
pinMode(svb, OUTPUT);
digitalWrite(TRIGGER_PIN1, LOW);
digitalWrite(TRIGGER_PIN2, LOW);
digitalWrite(pum, LOW);
digitalWrite(sva, HIGH);
digitalWrite(svb, HIGH);
delay(100);
}
void loop() {
readTdsQuick();
turbidity();
phsense();
long duration1, distance1;
digitalWrite(TRIGGER_PIN1, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN1, LOW);
duration1 = pulseIn(ECHO_PIN1, HIGH);
distance1 = duration1 * 0.034 / 2;
// Measure tank 2 water level
long duration2, distance2;
digitalWrite(TRIGGER_PIN2, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN2, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN2, LOW);
duration2 = pulseIn(ECHO_PIN2, HIGH);
distance2 = duration2 * 0.034 / 2;
// Convert distance to percentage
float percentage1 = (1.0 - (distance1 / MAX_HEIGHT_CM)) * 100.0;
float percentage2 = (1.0 - (distance2 / MAX_HEIGHT_CM)) * 100.0;
if (percentage1 < 0) {
percentage1 = 0;
}
if (percentage2 < 0) {
percentage2 = 0;
}
if (percentage1 < 10 && percentage2 < 10) {
digitalWrite(sva, LOW);
digitalWrite(svb, HIGH);
digitalWrite(pum, HIGH);
} else if (percentage1 > 87 && percentage2 < 10) {
digitalWrite(sva, HIGH);
digitalWrite(svb, LOW);
digitalWrite(pum, HIGH);
} else if (percentage1 < 10 && percentage2 > 87) {
digitalWrite(sva, LOW);
digitalWrite(svb, HIGH);
digitalWrite(pum, HIGH);
} else if (percentage1 > 87 && percentage2 > 87) {
digitalWrite(sva, HIGH);
digitalWrite(svb, HIGH);
digitalWrite(pum, LOW);
}
LCD();
}
void turbidity() {
int sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
int turbidit = map(sensorValue, 0, 750, 100, 0);
int ntu = map(sensorValue, 0, 750, 3000, 0);
}
void readTdsQuick() {
float rawEc = analogRead(pin::tds_sensor) * device::aref / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
float temperatureCoefficient = 1.0 + 0.02 * (sensor::waterTemp - 25.0); // temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
sensor::ec = (rawEc / temperatureCoefficient) * sensor::ecCalibration; // temperature and calibration compensation
sensor::tds = (133.42 * sensor::ec * sensor::ec * sensor::ec - 255.86 * sensor::ec * sensor::ec + 857.39 * sensor::ec) * 0.5; //convert voltage value to tds value
}
void phsense() {
timer.run(); // Initiates SimpleTimer
for (int i = 0; i < 10; i++) {
buffer_arr[i] = analogRead(A2);
delay(30);
}
for (int i = 0; i < 9; i++) {
for (int j = i + 1; j < 10; j++) {
if (buffer_arr[i] > buffer_arr[j]) {
temp = buffer_arr[i];
buffer_arr[i] = buffer_arr[j];
buffer_arr[j] = temp;
}
}
}
avgval = 0;
for (int i = 2; i < 8; i++)
avgval += buffer_arr[i];
float volt = (float)avgval * 5.0 / 1024 / 6;
ph_act = -5.70 * volt + calibration_value;
}
void LCD() {
switch (displayContent) {
case 0:
lcd.clear();
lcd.home();
lcd.print("TankLevel1: ");
lcd.print(percentage1);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("TankLevel2: ");
lcd.print(percentage2);
lcd.print("%");
break;
case 1:
lcd.clear();
lcd.home();
lcd.print("pH Val: ");
lcd.print(ph_act);
break;
case 2:
readTdsQuick();
lcd.clear();
lcd.home();
lcd.print("TDS: ");
lcd.print(sensor::tds);
lcd.setCursor(0, 1);
lcd.print("EC: ");
lcd.print(sensor::ec);
break;
case 3:
lcd.clear();
lcd.home();
if (turbidit < 0) {
turbidit = 0;
}
lcd.setCursor(0, 0);
lcd.print("turbidity: ");
lcd.print(" ");
lcd.setCursor(10, 0);
lcd.print(turbidit);
lcd.print("%");
if (turbidit < 20) {
lcd.setCursor(0, 1);
lcd.print(" its CLEAR ");
}
if ((turbidit > 20) && (turbidit < 50)) {
lcd.setCursor(0, 1);
lcd.print(" its CLOUDY ");
}
if (turbidit > 50) {
lcd.setCursor(0, 1);
lcd.print(" its DIRTY ");
}
break;
}
displayContent++;
delay(displayDelay);
if (displayContent > displaySlides) {
displayContent = 0;
}
nextDisplayTime = millis() + displayDelay;
}
so i have an issue with an LCD display. the values for Tank Percentage and Turbidity does not display outside their respective functions. Ive tried displaying all values in their functions and they display there but too many delays why i ran them as a switch in its own function