55#include < LiquidMenu.h>
66
77#include < EEPROM.h>
8- // #include <RTClib.h>
8+ #include " uRTCLib.h"
9+ #include < OneWire.h>
910#include < Wire.h>
1011#include < SD.h>
1112
1213// *** Setup ***///
1314
14- #define PH_PIN A15
15- #define EC_PIN A14
16- #define T_PIN A13
17- #define pumpEc 3
18- #define pumpPh 4
15+ #define PH_PIN A14
16+ #define EC_PIN A13
17+ #define T_PIN A15
18+ #define pumpEc 4
19+ #define pumpPh 5
1920#define butMenu 3
2021#define butOk 2
2122#define butUp 18
@@ -30,18 +31,21 @@ float voltagePH,voltageEC,phValue,ecValue,temperature = 25;
3031float phSetpoint= 25 ;
3132float ecSetpoint = 25 ;
3233int pumpTime = 2000 ;
34+ unsigned long sdCurrentMillis,sdPreviousMillis;
35+ bool sdState = LOW;
3336
34-
35-
37+ OneWire ds (T_PIN);
3638// ****Screen and Menu Setting****//
3739
40+ uRTCLib RTC (0x68 );
41+
3842LiquidCrystal_I2C lcd (0x27 , 16 , 2 ); // Setting with Pin LCD
3943
4044LiquidLine welcome_1 (1 , 0 , " Nutri Auto 1.0" );
4145LiquidLine welcome_2 (5 ,1 , " Welcome" );
4246LiquidScreen welcomeScreen (welcome_1,welcome_2);
43- LiquidLine notiEc (0 ,0 ," EC: " ,ecSetpoint );
44- LiquidLine notiPh (0 ,1 ," PH: " ,phSetpoint );
47+ LiquidLine notiEc (0 ,0 ," EC: " ,ecValue );
48+ LiquidLine notiPh (0 ,1 ," PH: " ,phValue );
4549LiquidScreen notiScreen (notiEc,notiPh);
4650LiquidLine setupPh (0 ,0 ," Set pH:" ,phSetpoint);
4751LiquidLine setupEc (0 ,1 ," Set EC:" ,ecSetpoint);
@@ -203,9 +207,9 @@ void interruptDown(){
203207void setup ()
204208{
205209 Serial.begin (115200 );
206- // ph.begin();
207- // ec.begin();
208- // SDSetup();
210+ ph.begin ();
211+ ec.begin ();
212+ SDSetup ();
209213 pinMode (4 ,OUTPUT); // 2
210214 pinMode (5 ,OUTPUT); // 3
211215 pinMode (6 ,OUTPUT); // 4
@@ -239,129 +243,163 @@ void setup()
239243void loop ()
240244{
241245 screen_Update ();
242- // readEcph();
243- // SDLoop();
246+ readEc ();
247+ readPh ();
248+ controlEc ();
249+ controlPh ();
250+ SDLoop ();
244251}
245252// ************* Pump Control******************//
246253
247- // void controlEc(){
248- // while (readEc()< (ecSetpoint*0.97)){
249- // digitalWrite(pumpEc,HIGH);
250- // delay(pumpTime);
251- // digitalWrite(pumpEc,LOW);
252- // }
253- // }
254- // void controlPh(){
255- // while (readPh()< (phSetpoint*0.97)){
256- // digitalWrite(pumpPh,HIGH);
257- // delay(pumptime);
258- // digitalWrite(pumpPh,LOW);
259- // }
260- //
261- // }
254+ void controlEc (){
255+ readEc ();
256+ while (ecValue < (ecSetpoint*0.97 )){
257+ readEc ();
258+ digitalWrite (pumpEc,);LOW
259+ delay (pumpTime);
260+ digitalWrite (pumpEc,HIGH);
261+ }
262+ }
263+ void controlPh (){
264+ readPh ();
265+ while (phValue< (phSetpoint*0.97 )){
266+ readPh ();
267+ digitalWrite (pumpPh,LOW);
268+ delay (pumptime);
269+ digitalWrite (pumpPh,HIGH);
270+ }
271+
272+ }
262273
263274// ************* Read Sensor ******************//
264275
265- // float readEc(){
266- // voltageEC = analogRead(EC_PIN)/1024.0*5000;
267- // ecValue = ec.readEC(voltageEC,temperature); // convert voltage to EC with temperature compensation
268- // for(int i=0;i<9;i++){
269- // for(int j=i+1;j<10;j++){
270- // if(buffer_arr[i]>buffer_arr[j]){
271- // temp=buffer_arr[i];
272- // buffer_arr[i]=buffer_arr[j];
273- // buffer_arr[j]=temp;
274- // }
275- // }
276- // }
277- // for(int i=2;i<8;i++)
278- // avgval+=buffer_arr[i];
279- // float volt=(float)avgval*5.0/1024/6;
280- // float ph_act = -5.70 * volt + calibration_value;
281- // }
282- // return ph_act;
283- // }
284- //
285- // float readPh(){
286- // voltagePH = analogRead(PH_PIN)/1024.0*5000; // read the ph voltage
287- // phValue = ph.readPH(voltagePH,temperature); // convert voltage to pH with temperature compensation
288- // Serial.print("pH:");
289- // Serial.print(phValue,2);
290- // return phValue;
291- // }
276+ float readEc (){
277+ static unsigned long timepoint = millis ();
278+ if (millis ()-timepoint>1000U ) // time interval: 1s
279+ {
280+ timepoint = millis ();
281+ voltageEC = analogRead (EC_PIN)/1024.0 *5000 ; // read the voltage
282+
283+ readTemperature (); // read your temperature sensor to execute temperature compensation
284+ ecValue = ec.readEC (voltageEC,temperature); // convert voltage to EC with temperature compensation
285+ Serial.print (" temperature:" );
286+ Serial.print (temperature,1 );
287+ Serial.print (" ^C EC:" );
288+ Serial.print (ecValue,1 );
289+ Serial.println (" ms/cm" );
290+ }
291+ ec.calibration (voltageEC,temperature);
292+ }
293+
294+ void readPh (){
295+ voltagePH = analogRead (PH_PIN)/1024.0 *5000 ; // read the ph voltage
296+ phValue = ph.readPH (voltagePH,temperature); // convert voltage to pH with temperature compensation
297+ Serial.print (" pH:" );
298+ Serial.print (phValue,2 );
299+ }
292300
293301float readTemperature ()
294302{
295303 // add your code here to get the temperature from your temperature sensor
304+ float temperature = getTemp ();
296305}
297306
307+ float getTemp (){
308+ // returns the temperature from one DS18S20 in DEG Celsius
309+
310+ byte data[12 ];
311+ byte addr[8 ];
312+
313+ if ( !ds.search (addr)) {
314+ // no more sensors on chain, reset search
315+ ds.reset_search ();
316+ return -1000 ;
317+ }
318+
319+ if ( OneWire::crc8 ( addr, 7 ) != addr[7 ]) {
320+ Serial.println (" CRC is not valid!" );
321+ return -1000 ;
322+ }
323+
324+ if ( addr[0 ] != 0x10 && addr[0 ] != 0x28 ) {
325+ Serial.print (" Device is not recognized" );
326+ return -1000 ;
327+ }
328+
329+ ds.reset ();
330+ ds.select (addr);
331+ ds.write (0x44 ,1 ); // start conversion, with parasite power on at the end
332+
333+ byte present = ds.reset ();
334+ ds.select (addr);
335+ ds.write (0xBE ); // Read Scratchpad
336+
337+
338+ for (int i = 0 ; i < 9 ; i++) { // we need 9 bytes
339+ data[i] = ds.read ();
340+ }
341+
342+ ds.reset_search ();
343+
344+ byte MSB = data[1 ];
345+ byte LSB = data[0 ];
346+
347+ float tempRead = ((MSB << 8 ) | LSB); // using two's compliment
348+ float TemperatureSum = tempRead / 16 ;
349+
350+ return TemperatureSum;
351+
352+ }
298353// ************** SD Card ***************//
299354
300- // void SDSetup()
301- // {
302- // pinMode(53, OUTPUT);
303-
304- // if (!SD.begin(chipSelect))
305- // {
306- // return;
307- // }
308- // }
309-
310- // void SDLoop()
311- // {
312- // unsigned long sdCurrentMillis = millis();
313- // if (sdCurrentMillis - sdPreviousMillis > sdTime)
314- // {
315- // sdPreviousMillis = sdCurrentMillis;
316- // if (sdState == LOW)
317- // {
318- // sdState = HIGH;
319- // File dataFile = SD.open("datalog.csv", FILE_WRITE);
320-
321- // if (dataFile)
322- // {
323- // now = RTC.now();
324- // dataFile.print(now.day(), DEC);
325- // dataFile.print('/');
326- // dataFile.print(now.month(), DEC);
327- // dataFile.print('/');
328- // dataFile.print(now.year(), DEC);
329- // dataFile.print(' ');
330- // dataFile.print(now.hour(), DEC);
331- // dataFile.print(':');
332- // dataFile.print(now.minute(), DEC);
333- // dataFile.print(':');
334- // dataFile.print(now.second(), DEC);
335- // dataFile.print(", ");
336- // dataFile.print(pH);
337- // dataFile.print(", ");
338- // dataFile.print(pmem);
339- // dataFile.println();
340- // dataFile.close();
341- // }
342- // }
343- // else
344- // {
345- // sdState = LOW;
346- // }
347- // }
348- // }
349- // ******* Button Check ***********//
350-
351- // void buttonsCheck() {
352- // if (right.check() == LOW) {
353- // menu_system.next_screen();
354- // }
355- // if (left.check() == LOW) {
356- // menu_system.previous_screen();
357- // }
358- // if (up.check() == LOW) {
359- // menu_system.call_function(increase);
360- // }
361- // if (down.check() == LOW) {
362- // menu_system.call_function(decrease);
363- // }
364- // if (enter.check() == LOW) {
365- // menu_system.switch_focus();
366- // }
367- // }
355+ void SDSetup ()
356+ {
357+ pinMode (53 , OUTPUT);
358+
359+ if (!SD.begin (53 ))
360+ {
361+ return ;
362+ }
363+ }
364+
365+ void SDLoop ()
366+ {
367+ unsigned long sdCurrentMillis = millis ();
368+ if (sdCurrentMillis - sdPreviousMillis > 3000 )
369+ {
370+ sdPreviousMillis = sdCurrentMillis;
371+ if (sdState == LOW)
372+ {
373+ sdState = HIGH;
374+ File dataFile = SD.open (" datalog.csv" , FILE_WRITE);
375+
376+ if (dataFile)
377+ {
378+ // DateTime now = RTC.now();
379+ dataFile.print (RTC.day (), DEC);
380+ dataFile.print (' /' );
381+ dataFile.print (RTC.month (), DEC);
382+ dataFile.print (' /' );
383+ dataFile.print (RTC.year (), DEC);
384+ dataFile.print (' ' );
385+ dataFile.print (RTC.hour (), DEC);
386+ dataFile.print (' :' );
387+ dataFile.print (RTC.minute (), DEC);
388+ dataFile.print (' :' );
389+ dataFile.print (RTC.second (), DEC);
390+ dataFile.print (" , " );
391+ dataFile.print (phValue);
392+ dataFile.print (" , " );
393+ dataFile.print (ecValue);
394+ dataFile.print (" , " );
395+ dataFile.print (temperature);
396+ dataFile.println ();
397+ dataFile.close ();
398+ }
399+ }
400+ else
401+ {
402+ sdState = LOW;
403+ }
404+ }
405+ }
0 commit comments