Hello.
I am having trouble with my project.
The code is meant to do as follows.
-** A solution with a tds reading of 600 is used during test**
-Read tds value and display on lcd.
-If tds value is < 1000 then the relay connected to D3 turns on for 10 seconds then turns off.
-Then it waits for 20 seconds and checks the tds value again.
--If tds value is < 1000 then the relay connected to D3 turns on for 10 seconds then turns off.
-Then it waits for 20 seconds and checks the tds value again.
-This keeps repeating continuously.
-While the tds check part of the code is running the lcd is meant to keep showing the refreshed tds reading every 1 second.
Now I have a few thing wrong with my code.
When it executes the relay (D3) never comes on.
The tds reading on the lcd refreshes every second.
Now I tinkered with the code a bit and worked out if I remove the part below from the void loop() section the relay now comes on for 10 seconds, then turns off BUT the lcd reading stops showing refreshed data during the 10 seconds and then refreshes so it only updates the lcd reading every 10 seconds.
This is the part I removed from the void loop() section-
unsigned long currentMillis = millis();
while (millis() - currentMillis < 20000) {
return;
}
Now with more tinkering I can get the the code to take the tds reading and if it is < 1000 the relay comes on for 10 seconds then turns off.
Then it waits another 10 seconds (meant to be 20 seconds) and then repeats.
I think there is a problem with the way I have written this part because I lose 10 seconds from the relay on time count and the pause time is 10 seconds not 20.
Also the lcd only shows refreshed data every 20 seconds now.
To do this I changed the void relayOn() section at the top of the code to this-
void relayOn()
{
unsigned long currentMillis = millis();
while (millis() - currentMillis < 10000) {
digitalWrite(RELAY, HIGH);
}
{
digitalWrite(RELAY, LOW);
}
while (millis() - currentMillis < 20000) {
digitalWrite(RELAY, LOW);
}
Thanks for taking the time to read this.
I hope it all makes sense (1st project ) and somebody can steer me in the right direction.
Here is the full code before tinkering-
#include <EEPROM.h>
#include "GravityTDS.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define ONE_WIRE_BUS 2
#define TdsSensorPin A1
OneWire oneWire(ONE_WIRE_BUS);
GravityTDS gravityTds;
DallasTemperature sensors(&oneWire);
float tdsValue = 0;
int RELAY = 3;
void checkTDS()
{
if (tdsValue < 1000) {
relayOn();
}
}
void relayOn()
{
unsigned long currentMillis = millis();
while (millis() - currentMillis < 10000) {
digitalWrite(RELAY, HIGH);
}
{
digitalWrite(RELAY, LOW);
}
}
void setup()
{
Serial.begin(115200);
lcd.begin(16, 2);
sensors.begin();
gravityTds.setPin(TdsSensorPin);
gravityTds.setAref(5.0); //reference voltage on ADC, default 5.0V on Arduino UNO
gravityTds.setAdcRange(1024); //1024 for 10bit ADC;4096 for 12bit ADC
gravityTds.begin(); //initialization
pinMode(RELAY, OUTPUT);
}
void loop()
{
sensors.requestTemperatures();
gravityTds.setTemperature(sensors.getTempCByIndex(0)); // set the temperature and execute temperature compensation
gravityTds.update(); //sample and calculate
tdsValue = gravityTds.getTdsValue(); // then get the value
Serial.print(tdsValue, 0);
Serial.println("ppm");
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0));
lcd.setCursor(0, 0);
lcd.print("TDS: ");
lcd.print(tdsValue, 0);
lcd.print(" PPM");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(sensors.getTempCByIndex(0));
lcd.print(" C");
delay(1500);
unsigned long currentMillis = millis();
while (millis() - currentMillis < 20000) {
return;
}
checkTDS();
}