#include <PCF8563.h>
#include <Servo.h>
PCF8563 pcf;
Servo servo;
#define TDSSENSOR_PIN_A0 A0 // ted sensor
#define VREF 3.3 // analog reference voltage of the ADC
#define SCOUNT 30 // sum of sample point
#define RELAY_PIN_2 2 // water pump number 2
#define RELAY_PIN_3 3 // water pump number 1
#define RELAY_PIN_4 4 // air pump number 2
#define RELAY_PIN_5 5 // air pump number 1
#define PUMP_PIN_6 6 // medicine pump
int servoDegree = 45;
int day = 1;
int month = 1;
int century = 0;
int year = 23;
int hour = 12;
int minute = 0;
int second = 0;
int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0;
int copyIndex = 0;
float averageVoltage = 0; // Floats to calculate average voltage & TDS value
float tdsValue = 0;
float temperature = 25; // current temperature for compensation
// median filtering algorithm
int getMedianNum(int bArray[], int iFilterLen) {
int bTab[iFilterLen];
for (byte i = 0; i < iFilterLen; i++)
bTab[i] = bArray[i];
int i, j, bTemp;
for (j = 0; j < iFilterLen - 1; j++) {
for (i = 0; i < iFilterLen - j - 1; i++) {
if (bTab[i] > bTab[i + 1]) {
bTemp = bTab[i];
bTab[i] = bTab[i + 1];
bTab[i + 1] = bTemp;
}
}
}
if ((iFilterLen & 1) > 0) {
bTemp = bTab[(iFilterLen - 1) / 2];
}
else {
bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
}
return bTemp;
}
void setup() {
Serial.begin(9400);
pinMode(TDSSENSOR_PIN_A0, INPUT);
pinMode(RELAY_PIN_2, OUTPUT);
pinMode(RELAY_PIN_3, OUTPUT);
pinMode(RELAY_PIN_4, OUTPUT);
pinMode(RELAY_PIN_5, OUTPUT);
pinMode(PUMP_PIN_6, OUTPUT);
servo.attach(7);
pcf.init();//initialize the clock
pcf.stopClock();//stop the clock
//set time to to 31/3/2018 17:33:0
pcf.setYear(year);//set year
pcf.setMonth(month);//set month
pcf.setDay(day);//set day
pcf.setHour(hour);//set hour
pcf.setMinut(minute);//set minute
pcf.setSecond(second);//set second
pcf.startClock();//start the clock
}
void loop() {
Time nowTime = pcf.getTime();//get current time
static unsigned long analogSampleTimepoint = millis();
if (millis() - analogSampleTimepoint > 40U) { //every 40 milliseconds,read the analog value from the ADC
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(TDSSENSOR_PIN_A0); //read the analog value and store into the buffer
analogBufferIndex++;
if (analogBufferIndex == SCOUNT) {
analogBufferIndex = 0;
}
}
static unsigned long printTimepoint = millis();
if (millis() - printTimepoint > 800U) {
printTimepoint = millis();
for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++) {
analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
// read the analog value more stable by the median filtering algorithm, and convert to voltage value
averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 4096.0;
//temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0);
//temperature compensation
float compensationVoltage = averageVoltage / compensationCoefficient;
//convert voltage value to tds value
tdsValue = (133.42 * compensationVoltage * compensationVoltage * compensationVoltage - 255.86 * compensationVoltage * compensationVoltage + 857.39 * compensationVoltage) * 0.5;
if (tdsValue < 300) {
Serial.print("Excellent TDS Value:");
Serial.print(tdsValue, 0);
Serial.println("ppm");
digitalWrite(RELAY_PIN_2, HIGH);
digitalWrite(RELAY_PIN_3, LOW);
digitalWrite(RELAY_PIN_4, HIGH);
digitalWrite(RELAY_PIN_5, LOW);
}
else if (tdsValue < 600) {
Serial.print("Good TDS Value:");
Serial.print(tdsValue, 0);
Serial.println("ppm");
digitalWrite(RELAY_PIN_2, HIGH);
digitalWrite(RELAY_PIN_3, LOW);
digitalWrite(RELAY_PIN_4, HIGH);
digitalWrite(RELAY_PIN_5, LOW);
}
else if (tdsValue < 900) {
Serial.print("Fair TDS Value:");
Serial.print(tdsValue, 0);
Serial.println("ppm");
digitalWrite(RELAY_PIN_2, HIGH);
digitalWrite(RELAY_PIN_3, LOW);
digitalWrite(RELAY_PIN_4, HIGH);
digitalWrite(RELAY_PIN_5, LOW);
}
else if (tdsValue < 1000) {
Serial.print("Poor TDS Value:");
Serial.print(tdsValue, 0);
Serial.println("ppm");
digitalWrite(RELAY_PIN_2, HIGH);
digitalWrite(RELAY_PIN_3, HIGH);
digitalWrite(RELAY_PIN_4, HIGH);
digitalWrite(RELAY_PIN_5, HIGH);
}
else if (tdsValue == 1000) {
Serial.print("Unacceptable TDS Value:");
Serial.print(tdsValue, 0);
Serial.println("ppm");
digitalWrite(RELAY_PIN_2, HIGH);
digitalWrite(RELAY_PIN_3, HIGH);
digitalWrite(RELAY_PIN_4, HIGH);
digitalWrite(RELAY_PIN_5, HIGH);
}
else {}
if (nowTime.day == 7) {
digitalWrite(PUMP_PIN_6, HIGH);
}
else if (nowTime.day == 14) {
digitalWrite(PUMP_PIN_6, HIGH);
}
else if (nowTime.day == 21) {
digitalWrite(PUMP_PIN_6, HIGH);
}
else if (nowTime.day == 28) {
digitalWrite(PUMP_PIN_6, HIGH);
}
else if (nowTime.day == 14) {
digitalWrite(PUMP_PIN_6, HIGH);
}
else {
digitalWrite(PUMP_PIN_6, LOW);
}
if (nowTime.hour == 8) {
servo.write(servoDegree);
}
else if (nowTime.hour == 18) {
servo.write(servoDegree);
}
else {
servo.write(0);
}
//print current time
Serial.print(nowTime.day);
Serial.print("/");
Serial.print(nowTime.month);
Serial.print("/");
Serial.print(nowTime.year);
Serial.print(" ");
Serial.print(nowTime.hour);
Serial.print(":");
Serial.print(nowTime.minute);
Serial.print(":");
Serial.println(nowTime.second);
delay(100);
}
}
}
Who can help me?
Please:
- don't make posts with a single sentence or word. Explain what you mean in a synthetic but clear way
- don't delete your posts (it's futile, they're still readable on edit history...) but just edit and change what you need
- Don't cross-post problems over multiple threads, that makes hard to follow and against forum rules.
And then
Ok sir
Are you kidding us, or are you so on your own?
I think he just wants to solve his problem as quickly as possible. But you have to be patient.
Good work takes time.
The PCF8563 uses a different I2C address (0x51) (find your RTC on this page:
And some examples for your RTC
9400? Really?
I'm sorry but this code looks like a "Frankenstein" with a lot of changes made by more than one person, at least one of them doesn't seem to be familiar enough with coding, so it isn't an easy task.
I think he's not using that library, I see an #include <PCF8563.h> when the library you linked requires "#include <I2C_RTC.h>".
So we actually don't know what library the OP is using (maybe this?), he just liked us a Mediafire file sharing header file, we don't know how the PCF8563 is connected (and if its interface is I2C), he haven't provided more information about its code (except posting just the code after we have explicitly requested), we don't know what is his programming skill level, replies with single sentences... It's a hard task for us to understando and try to give some help, we'd be happy to help others, but IMHO as this is not our (paid) job, a few answers could be given to him.
...
int day = 1;
int month = 1;
int century = 0;
int year = 23;
int hour = 12;
int minute = 0;
int second = 0;
...
//set time to to 31/3/2018 17:33:0
pcf.setYear(year);//set year
pcf.setMonth(month);//set month
pcf.setDay(day);//set day
pcf.setHour(hour);//set hour
pcf.setMinut(minute);//set minute
pcf.setSecond(second);//set second
...
Other than what I said before, however, I think that, not knowing which library he was using, at least "year" should be 2023 not 23.
Then I don't get why when turned on he always sets the RTC a specific date, in this case January 1, 2023 at 12:00:00. Why need an RTC if the date/time will be always the same when starts?
And the "century" variable defined, set to zero (!), and never used, is also wonderful.
Ok, I'll stop now... ![]()
Haha. Fair assessment, Docdoctor. This is a shopping list of devices to keep a body alive...


