#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define SET_POINT 25 // Punto de ajuste de temperatura en grados Celsius
int buf[10];
int temp = 0;
unsigned long int inValue;
const uint16_t tdsThreshold = 2000; // 12 bits 4096
// pins
// sda, scl -> 21 , 22
const uint8_t tdsPin = 32;
const uint8_t greenLedPin = 16;
const uint8_t yellowLedPin = 17;
const uint8_t redLedPin = 18;
const uint8_t relay1Pin = 19;
const uint8_t relay2Pin = 5;
const uint8_t relay3Pin = 4;
const uint8_t relay4Pin = 23;
// lcd
const uint8_t lcdAddr = 0x27;
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(lcdAddr, 16, 2);
// parameters global vars
float temperature;
float ph;
float tds;
void setup(){
Serial.begin(115200);
// pin config
pinMode(tdsPin, INPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
// lcd init
lcd.init();
lcdWelcomeMessage();
delay(500);
}
void loop(){
readAndCheckTDSLevel();
showParametersOnLcd();
delay(1000);
}
// =========== Helper functions =============
void lcdWelcomeMessage(){
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" Bienvenido a ");
lcd.setCursor(0, 1);
lcd.print(" Medidor pH ");
}
void readAndCheckTDSLevel(){
// checks the current tds level
tds = analogRead(tdsPin);
Serial.println(tds);
if (tds > tdsThreshold)
{
digitalWrite(redLedPin, HIGH);
}
else
{
digitalWrite(redLedPin, LOW);
}
}
void showParametersOnLcd(){
lcd.clear();
// PH
lcd.setCursor(0, 0);
lcd.print("Ph:");
lcd.print(ph);
lcd.print(" ");
// temp
lcd.setCursor(9, 0);
lcd.print(" T:");
lcd.print(temperature);
lcd.print("°c");
// TDS
lcd.setCursor(8, 1);
lcd.print("tds:");
lcd.print(tds);
lcd.print(" ");
}