/*------------------------------------------------------------------------ Author: Steven Dunbar Code Description: Code for arduino for controlling the vertigrow hydroponic to be used with wiring diagram provided on the makerRepo acount. --------------------------------------------------------------------------*/ #include #include #include #include "Arduino.h" //define library array MCUFRIEND_kbv tft; //Define text fonts #include #include #include #include //pressure sensitivity screen #define MINPRESSURE 400 #define MAXPRESSURE 1000 //Touchscreen define pins TouchScreen ts = TouchScreen(9, A2, A3, 8, 300); //Define touch point buttons Adafruit_GFX_Button home_btn, nutrients_btn, settings_btn; Adafruit_GFX_Button tank1_btn, tank2_btn, pH_btn; Adafruit_GFX_Button Cwater_btn, min_btn, hr_btn, setday_btn, light_btn; Adafruit_GFX_Button IN_btn, min2_btn, hr2_btn, OUT_btn, PUMP_btn; Adafruit_GFX_Button UPtank1_btn, UPtank2_btn, UPpH_btn, DOWNtank1_btn, DOWNtank2_btn, DOWNpH_btn; Adafruit_GFX_Button SET1_btn, SET2_btn, yes_btn, no_btn; //Touch coordinates int pixel_x, pixel_y; //Touch_getXY() updates global vars //Global variables bool pump; bool waterIn, waterOut; bool lightON; char dayofweek[7][4] = { { 'M', 'O', 'N' }, { 'T', 'U', 'E' }, { 'W', 'E', 'D' }, { 'T', 'H', 'U', 'R' }, { 'F', 'R', 'I' }, { 'S', 'A', 'T' }, { 'S', 'U', 'N' } }; char time[8][10] = { { ' ', '1' }, { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }, { ':' }, { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }, { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }, { ' ' }, { 'A', 'P' }, { 'M' } }; int T1, T2, T3, T4, T5; int WT1, WT2, WT3, WT4, WT5; //SET TIME FOR WATER CHANGE int countDays; char changewater[15][7] = { { ' ', 'N', 'E', 'V', 'E', 'R' }, { ' ', '1', ' ', 'D', 'A', 'Y' }, { ' ', '2', ' ', 'D', 'A', 'Y', 'S' }, { ' ', '3', ' ', 'D', 'A', 'Y', 'S' }, { ' ', '4', ' ', 'D', 'A', 'Y', 'S' }, { ' ', '5', ' ', 'D', 'A', 'Y', 'S' }, { ' ', '6', ' ', 'D', 'A', 'Y', 'S' }, { ' ', '7', ' ', 'D', 'A', 'Y', 'S' }, { ' ', '8', ' ', 'D', 'A', 'Y', 'S' }, { ' ', '9', ' ', 'D', 'A', 'Y', 'S' }, { '1', '0', ' ', 'D', 'A', 'Y', 'S' }, { '1', '1', ' ', 'D', 'A', 'Y', 'S' }, { '1', '2', ' ', 'D', 'A', 'Y', 'S' }, { '1', '3', ' ', 'D', 'A', 'Y', 'S' }, { '1', '4', ' ', 'D', 'A', 'Y', 'S' } }; int tank1, tank2, pH; int type; int day, waterday; //prints the current day int waterevery; long int Tmill, minutes; char currentscreen; //tells the code which screen the user is on char t; int keeptime, pasttime; bool repeat; bool pass2; int countpass; int pHlow,pHHigh; // max and min of the pHlevel //Define colors #define BLACK 0x0000 #define BLUE 0x001F #define RED 0xF800 #define GREEN 0x07E0 #define OLGREEN 0x4624 #define CYAN 0x07FF #define MAGENTA 0xF81F #define YELLOW 0xFFE0 #define WHITE 0xFFFF #define GRAY 0xCE59 #define ORANGE 0xFCC0 //Sensor pins and holding variables const int light_sensor = A7; // assigning photosensor to pin A7 int lightval; //variable to get light sensor input int loop1; /*____________________________________________________________________________*/ float calibration_value = 21.34 + 0.1; int phval = 0; unsigned long int avgval; int buffer_arr[10],temp; const int pH_sensor = A6; // assigning pH probe to pin A6 float pHval; float Voltage; //variable to get ph sensor input /*____________________________________________________________________________*/ const int pingPin = 45; // assigning water sensor to pin A8 long RangeInCentimeters; //variable to get water level input //assigning pins to relays const int relay1 = 23; const int relay2 = 25; const int relay3 = 27; const int relay4 = 29; const int relay5 = 31; const int relay6 = 33; const int relay7 = 35; const int relay8 = 37; //Code for operating water sensor class Ultrasonic { public: Ultrasonic(int pin); void DistanceMeasure(void); long microsecondsToCentimeters(void); long microsecondsToInches(void); private: int _pin;//pin number of Arduino that is connected with SIG pin of Ultrasonic Ranger. long duration;// the Pulse time received; }; Ultrasonic::Ultrasonic(int pin) { _pin = pin; } void Ultrasonic::DistanceMeasure(void) { pinMode(_pin, OUTPUT); digitalWrite(_pin, LOW); delayMicroseconds(2); digitalWrite(_pin, HIGH); delayMicroseconds(5); digitalWrite(_pin, LOW); pinMode(_pin, INPUT); duration = pulseIn(_pin, HIGH); } /*The measured distance from the range 0 to 400 Centimeters*/ long Ultrasonic::microsecondsToCentimeters(void) { return duration / 29 / 2; } /*The measured distance from the range 0 to 157 Inches*/ long Ultrasonic::microsecondsToInches(void) { return duration / 74 / 2; } Ultrasonic ultrasonic(45); /*---------------------------------------------------------------------------*/ //Code for inital settings will only run once void setup(void) { uint16_t ID = tft.readID(); //Serial.print("TFT ID = 0x"); //Serial.println(ID, HEX); //Serial.println("Calibrate for your Touch Panel"); //ID = 0x9488; // write-only shield tft.begin(0x9488); tft.setRotation(1); //PORTRAIT //Define input/output pinMode(light_sensor, INPUT); pinMode(pH_sensor, INPUT); pinMode(pingPin, INPUT); //Relay pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); pinMode(relay3, OUTPUT); pinMode(relay4, OUTPUT); pinMode(relay5, OUTPUT); pinMode(relay6, OUTPUT); pinMode(relay7, OUTPUT); pinMode(relay8, OUTPUT); tank1 = 50; tank2 = 50; pH = 5; T1 = 1; T2 = 2; T3 = 0; T4 = 0; T5 = 0; Tmill = 0; minutes = 0; currentscreen = '0'; pump = 1; waterday = 0; WT1 = 1; WT2 = 2; WT3 = 0; WT4 = 0; WT5 = 0; countDays = 0; waterevery = 7; waterIn = false; waterOut = false; t = '0'; pass2 = 1; countpass = 0; //SENSORS lightON = 1; //TEST VALUES pHval = 7; //Serial1.begin(9600); //Serial1.begin(9600); repeat = 0; //Serial.begin(9600); pHlow = 6; pHHigh = 8; loop1 =0; Serial1.begin(9800); //start communication with HC-06 drawHomeScreen(); } /*________________________________________________________________________________________*/ //LOOPING code runs in a loop void loop(void) { clock(); CheckTime(); if(loop1 == 1000){ TestpH(); TurnOnOFFlights(); loop1=0; } else{ loop1 = loop1 + 1; } /*_____________________________________________________________________________________________________*/ //code for bluetooth HC-60 if (Serial1.available() > 0) { t = Serial1.read(); Serial1.println(t); switch (t) { case 'A': dispenseNutrients(0); break; case 'B': dispenseNutrients(1); break; case 'C': dispenseNutrients(2); break; case 'D': if (waterevery != 14) { waterevery = waterevery + 1; } else { waterevery = 0; } if(currentscreen == '2'){ uChangewater(); } break; case 'E': if (time[4][T4] != '9') { T4 = T4 + 1; } else if (time[3][T3] != '5') { T3 = T3 + 1; T4 = 0; } else if (time[0][T1] == '1' && time[1][T2] == '2' && time[3][T3] == '5') { T1 = 0; T2 = 1; T3 = 0; T4 = 0; } else if (time[1][T2] != '9') { if (time[0][T1] == '1' && time[1][T2] == '1') { if (T5 == 1) { T5 = 0; if (day == 6) { day = 0; } else { day = day + 1; } if (currentscreen == '0') { updateday(); } } else T5 = 1; } T2 = T2 + 1; T3 = 0; T4 = 0; } else if (time[0][T1] != '1') { T1 = 1; T2 = 0; T3 = 0; T4 = 0; } if(currentscreen == '2'){ updatetimeSettings(); } else if(currentscreen == '0') { updatetime(); } break; case 'F': if (time[0][T1] == '1' && time[1][T2] == '2') { T1 = 0; T2 = 1; T3 = 0; T4 = 0; } else if (time[1][T2] != '9') { if (time[0][T1] == '1' && time[1][T2] == '1') { if (T5 == 1) { T5 = 0; if (day == 6) { day = 0; } else { day = day + 1; } if (currentscreen == '0') { updateday(); } } else T5 = 1; } T2 = T2 + 1; T3 = 0; T4 = 0; } else if (time[0][T1] != '1') { T1 = 1; T2 = 0; T3 = 0; T4 = 0; } if(currentscreen == '2'){ updatetimeSettings(); } else if(currentscreen == '0') { updatetime(); } break; case 'G': if (day == 6) { day = 0; } else { day = day + 1; } if(currentscreen == '2'){ updateDay(); } else if(currentscreen == '0'){ updateday(); } break; case 'H': if (lightON == true) { lightON = false; } else { lightON = true; } if(currentscreen == '2'){ OnOFFSettings(); } break; case 'I': if (pump == true) { digitalWrite(relay8,HIGH); pump = false; } else if (pump == false) { pump = true; digitalWrite(relay8, LOW); } if(currentscreen == '3'){ Part2WaterPump();} else if (currentscreen == '0') { pumpONOFF(); } break; case 'J': if (waterIn == true) { waterIn = false; digitalWrite(relay5, LOW); } else { waterIn = true; digitalWrite(relay5, HIGH); } if(currentscreen == '3'){ Part2WaterIn(); } break; case 'K': if (waterOut == true) { waterOut = false; digitalWrite(relay4, LOW); } else { waterOut = true; digitalWrite(relay4, HIGH); } if(currentscreen == '3'){ Part2WaterOut(); } break; case 'L': if (time[4][WT4] != '9') { WT4 = WT4 + 1; } else if (time[3][WT3] != '5') { WT3 = WT3 + 1; WT4 = 0; } else if (time[0][WT1] == '1' && time[1][WT2] == '2' && time[3][WT3] == '5') { WT1 = 0; WT2 = 1; WT3 = 0; WT4 = 0; } else if (time[1][WT2] != '9') { if (time[0][WT1] == '1' && time[1][WT2] == '1') { if (WT5 == 1) { WT5 = 0; } else WT5 = 1; } WT2 = WT2 + 1; WT3 = 0; WT4 = 0; } else if (time[0][WT1] != '1') { WT1 = 1; WT2 = 0; WT3 = 0; WT4 = 0; } if(currentscreen == '3'){ Part2TIME(); } break; case 'M': if (time[0][WT1] == '1' && time[1][WT2] == '2') { WT1 = 0; WT2 = 1; WT3 = 0; WT4 = 0; } else if (time[1][WT2] != '9') { if (time[0][WT1] == '1' && time[1][WT2] == '1') { if (WT5 == 1) { WT5 = 0; } else WT5 = 1; } WT2 = WT2 + 1; WT3 = 0; WT4 = 0; } else if (time[0][WT1] != '1') { WT1 = 1; WT2 = 0; WT3 = 0; WT4 = 0; } if(currentscreen == '3'){ Part2TIME(); } break; case 'N': if (tank1 == 16960) { tank1 = 0; } else { tank1 = tank1 + 1; } if(currentscreen == '1'){ updateTank1();} break; case 'O': if (tank1 == 0) { tank1 = 0; } else { tank1 = tank1 - 1; if(currentscreen == '1'){ updateTank1();} } break; case 'P': if (tank2 == 16960) { tank2 = 0; } else { tank2 = tank2 + 1; } if(currentscreen == '1'){ updateTank2();} break; case 'Q': if (tank2 == 0) { tank2 = 0; } else { tank2 = tank2 - 1; if(currentscreen == '1'){ updateTank2();} } break; case 'R': if (pH == 16960) { pH = 0; } else { pH = pH + 1; } if(currentscreen == '1'){ updatepH();} break; case 'S': if (pH == 0) { pH = 0; } else { pH = pH - 1; if(currentscreen == '1'){ updatepH();} } break; case 'T': setup(); break; case 'a': Serial1.print(pHval); break; case 'b': if (pump == 1) { Serial1.print("ON"); } if (pump == 0) { Serial1.print("OFF"); } break; case 'c': switch(day) { case 0: Serial1.print("MON"); break; case 1: Serial1.print("TUE"); break; case 2: Serial1.print("WED"); break; case 3: Serial1.print("THUR"); break; case 4: Serial1.print("FRI"); break; case 5: Serial1.print("SAT"); break; case 6: Serial1.print("SUN"); break; } break; case 'e': Serial1.print(tank1); break; case 'f': Serial1.print(tank2); break; case 'g': Serial1.print(pH); break; case 'h': switch(waterevery) { case 0: Serial1.print("NEVER"); break; case 1: Serial1.print("1 DAY"); break; case 2: Serial1.print("2 DAYS"); break; case 3: Serial1.print("3 DAYS"); break; case 4: Serial1.print("4 DAYS"); break; case 5: Serial1.print("5 DAYS"); case 6: Serial1.print("6 DAYS"); break; case 7: Serial1.print("7 DAYS"); break; case 8: Serial1.print("8 DAYS"); break; case 9: Serial1.print("9 DAYS"); break; case 10: Serial1.print("10 DAYS"); break; case 11: Serial1.print("11 DAYS"); break; case 12: Serial1.print("12 DAYS"); break; case 13: Serial1.print("13 DAYS"); break; case 14: Serial1.print("14 DAYS"); break; } break; case 'i': Serial1.print(time[0][T1]); Serial1.print(time[1][T2]); Serial1.print(time[2][0]); Serial1.print(time[3][T3]); Serial1.print(time[4][T4]); Serial1.print(time[5][0]); Serial1.print(time[6][T5]); Serial1.print(time[7][0]); break; case 'j': switch(day) { case 0: Serial1.print("MON"); break; case 1: Serial1.print("TUE"); break; case 2: Serial1.print("WED"); break; case 3: Serial1.print("THUR"); break; case 4: Serial1.print("FRI"); break; case 5: Serial1.print("SAT"); break; case 6: Serial1.print("SUN"); break; } break; case 'q': if (waterIn == true) { Serial1.print("OFF"); } else { Serial1.print("ON"); } break; case 'm': if (waterOut == true) { Serial1.print("OFF"); } else { Serial1.print("ON"); } break; case 'n': Serial1.print(time[0][WT1]); Serial1.print(time[1][WT2]); Serial1.print(time[2][0]); Serial1.print(time[3][WT3]); Serial1.print(time[4][WT4]); Serial1.print(time[5][0]); Serial1.print(time[6][WT5]); Serial1.print(time[7][0]); break; } } /*__________________________________________________________________________________________________________*/ bool down = Touch_getXY(); if (currentscreen == '0' || currentscreen == '1' || currentscreen == '2' || currentscreen == '3') { home_btn.press(down && home_btn.contains(pixel_x, pixel_y)); if (home_btn.justReleased()) home_btn.drawButton(); if (home_btn.justPressed()) { home_btn.drawButton(true); drawHomeScreen(); currentscreen = '0'; } } if (currentscreen == '5') { yes_btn.press(down && yes_btn.contains(pixel_x, pixel_y)); no_btn.press(down && no_btn.contains(pixel_x, pixel_y)); if (no_btn.justReleased()) no_btn.drawButton(); if (yes_btn.justReleased()) yes_btn.drawButton(); if (yes_btn.justPressed()) { yes_btn.drawButton(true); dispenseNutrients(type); currentscreen = '0'; return (0); } if (no_btn.justPressed()) { no_btn.drawButton(true); currentscreen = '0'; drawHomeScreen(); return (0); } } if (currentscreen == '0' || currentscreen == '1') { nutrients_btn.press(down && nutrients_btn.contains(pixel_x, pixel_y)); settings_btn.press(down && settings_btn.contains(pixel_x, pixel_y)); if (nutrients_btn.justReleased()) nutrients_btn.drawButton(); if (settings_btn.justReleased()) settings_btn.drawButton(); if (nutrients_btn.justPressed()) { nutrients_btn.drawButton(true); drawNutrientsScreen(); currentscreen = '1'; } if (settings_btn.justPressed()) { settings_btn.drawButton(true); drawSettingsScreen(); currentscreen = '2'; return (0); } } if (currentscreen == '0') { tank1_btn.press(down && tank1_btn.contains(pixel_x, pixel_y)); tank2_btn.press(down && tank2_btn.contains(pixel_x, pixel_y)); pH_btn.press(down && pH_btn.contains(pixel_x, pixel_y)); if (tank1_btn.justReleased()) tank1_btn.drawButton(); if (tank2_btn.justReleased()) tank2_btn.drawButton(); if (pH_btn.justReleased()) pH_btn.drawButton(); if (tank1_btn.justPressed()) { tank1_btn.drawButton(true); addNutrients(); type = 0; currentscreen = '5'; } if (tank2_btn.justPressed()) { tank2_btn.drawButton(true); addNutrients(); type = 1; currentscreen = '5'; } if (pH_btn.justPressed()) { pH_btn.drawButton(true); addNutrients(); type = 2; currentscreen = '5'; } } if (currentscreen == '1') { UPtank1_btn.press(down && UPtank1_btn.contains(pixel_x, pixel_y)); UPtank2_btn.press(down && UPtank2_btn.contains(pixel_x, pixel_y)); UPpH_btn.press(down && UPpH_btn.contains(pixel_x, pixel_y)); DOWNtank1_btn.press(down && DOWNtank1_btn.contains(pixel_x, pixel_y)); DOWNtank2_btn.press(down && DOWNtank2_btn.contains(pixel_x, pixel_y)); DOWNpH_btn.press(down && DOWNpH_btn.contains(pixel_x, pixel_y)); if (UPtank1_btn.justReleased()) UPtank1_btn.drawButton(); if (UPtank2_btn.justReleased()) UPtank2_btn.drawButton(); if (UPpH_btn.justReleased()) UPpH_btn.drawButton(); if (DOWNtank1_btn.justReleased()) DOWNtank1_btn.drawButton(); if (DOWNtank2_btn.justReleased()) DOWNtank2_btn.drawButton(); if (DOWNpH_btn.justReleased()) DOWNpH_btn.drawButton(); if (UPtank1_btn.justPressed()) { UPtank1_btn.drawButton(true); if (tank1 == 16960) { tank1 = 0; } else { tank1 = tank1 + 1; } updateTank1(); } if (UPtank2_btn.justPressed()) { UPtank2_btn.drawButton(true); if (tank2 == 16960) { tank2 = 0; } else { tank2 = tank2 + 1; } updateTank2(); } if (UPpH_btn.justPressed()) { UPpH_btn.drawButton(true); if (pH == 16960) { pH = 0; } else { pH = pH + 1; } updatepH(); } if (DOWNtank1_btn.justPressed()) { DOWNtank1_btn.drawButton(true); if (tank1 == 0) { tank1 = 0; } else { tank1 = tank1 - 1; updateTank1(); } } if (DOWNtank2_btn.justPressed()) { DOWNtank2_btn.drawButton(true); if (tank2 == 0) { tank2 = 0; } else { tank2 = tank2 - 1; updateTank2(); } } if (DOWNpH_btn.justPressed()) { DOWNpH_btn.drawButton(true); if (pH == 0) { pH = 0; } else { pH = pH - 1; updatepH(); } } } if (currentscreen == '2') { Cwater_btn.press(down && Cwater_btn.contains(pixel_x, pixel_y)); min_btn.press(down && min_btn.contains(pixel_x, pixel_y)); hr_btn.press(down && hr_btn.contains(pixel_x, pixel_y)); setday_btn.press(down && setday_btn.contains(pixel_x, pixel_y)); light_btn.press(down && light_btn.contains(pixel_x, pixel_y)); if (Cwater_btn.justReleased()) Cwater_btn.drawButton(); if (min_btn.justReleased()) min_btn.drawButton(); if (hr_btn.justReleased()) hr_btn.drawButton(); if (setday_btn.justReleased()) setday_btn.drawButton(); if (light_btn.justReleased()) light_btn.drawButton(); if (Cwater_btn.justPressed()) { Cwater_btn.drawButton(true); if (waterevery != 14) { waterevery = waterevery + 1; } else { waterevery = 0; } uChangewater(); } if (min_btn.justPressed()) { min_btn.drawButton(true); if (time[4][T4] != '9') { T4 = T4 + 1; } else if (time[3][T3] != '5') { T3 = T3 + 1; T4 = 0; } else if (time[0][T1] == '1' && time[1][T2] == '2' && time[3][T3] == '5') { T1 = 0; T2 = 1; T3 = 0; T4 = 0; } else if (time[1][T2] != '9') { if (time[0][T1] == '1' && time[1][T2] == '1') { if (T5 == 1) { T5 = 0; if (day == 6) { day = 0; } else { day = day + 1; } if (currentscreen == '0') { updateday(); } } else T5 = 1; } T2 = T2 + 1; T3 = 0; T4 = 0; } else if (time[0][T1] != '1') { T1 = 1; T2 = 0; T3 = 0; T4 = 0; } updatetimeSettings(); } if (hr_btn.justPressed()) { hr_btn.drawButton(true); if (time[0][T1] == '1' && time[1][T2] == '2') { T1 = 0; T2 = 1; T3 = 0; T4 = 0; } else if (time[1][T2] != '9') { if (time[0][T1] == '1' && time[1][T2] == '1') { if (T5 == 1) { T5 = 0; if (day == 6) { day = 0; } else { day = day + 1; } if (currentscreen == '0') { updateday(); } } else T5 = 1; } T2 = T2 + 1; T3 = 0; T4 = 0; } else if (time[0][T1] != '1') { T1 = 1; T2 = 0; T3 = 0; T4 = 0; } updatetimeSettings(); } if (setday_btn.justPressed()) { setday_btn.drawButton(true); if (day == 6) { day = 0; } else { day = day + 1; } updateDay(); } if (light_btn.justPressed()) { light_btn.drawButton(true); if (lightON == true) { lightON = false; OnOFFSettings(); } else { lightON = true; OnOFFSettings(); } } } if (currentscreen == '3') { OUT_btn.press(down && OUT_btn.contains(pixel_x, pixel_y)); min2_btn.press(down && min2_btn.contains(pixel_x, pixel_y)); hr2_btn.press(down && hr2_btn.contains(pixel_x, pixel_y)); IN_btn.press(down && IN_btn.contains(pixel_x, pixel_y)); PUMP_btn.press(down && PUMP_btn.contains(pixel_x, pixel_y)); if (OUT_btn.justReleased()) OUT_btn.drawButton(); if (min2_btn.justReleased()) min2_btn.drawButton(); if (hr2_btn.justReleased()) hr2_btn.drawButton(); if (IN_btn.justReleased()) IN_btn.drawButton(); if (PUMP_btn.justReleased()) PUMP_btn.drawButton(); if (IN_btn.justPressed()) { IN_btn.drawButton(true); if (waterIn == true) { waterIn = false; digitalWrite(relay5, LOW); } else { waterIn = true; digitalWrite(relay5, HIGH); } Part2WaterIn(); } if (OUT_btn.justPressed()) { OUT_btn.drawButton(true); if (waterOut == true) { waterOut = false; digitalWrite(relay4, LOW); } else { waterOut = true; digitalWrite(relay4, HIGH); } Part2WaterOut(); } if (PUMP_btn.justPressed()) { PUMP_btn.drawButton(true); if (pump == true) { currentscreen = '4'; turnoffPUMP(); } else if (pump == false) { pump = true; digitalWrite(relay8, LOW); Part2WaterPump(); } } if (min2_btn.justPressed()) { min2_btn.drawButton(true); if (time[4][WT4] != '9') { WT4 = WT4 + 1; } else if (time[3][WT3] != '5') { WT3 = WT3 + 1; WT4 = 0; } else if (time[0][WT1] == '1' && time[1][WT2] == '2' && time[3][WT3] == '5') { WT1 = 0; WT2 = 1; WT3 = 0; WT4 = 0; } else if (time[1][WT2] != '9') { if (time[0][WT1] == '1' && time[1][WT2] == '1') { if (WT5 == 1) { WT5 = 0; } else WT5 = 1; } WT2 = WT2 + 1; WT3 = 0; WT4 = 0; } else if (time[0][WT1] != '1') { WT1 = 1; WT2 = 0; WT3 = 0; WT4 = 0; } Part2TIME(); } if (hr2_btn.justPressed()) { hr2_btn.drawButton(true); if (time[0][WT1] == '1' && time[1][WT2] == '2') { WT1 = 0; WT2 = 1; WT3 = 0; WT4 = 0; } else if (time[1][WT2] != '9') { if (time[0][WT1] == '1' && time[1][WT2] == '1') { if (WT5 == 1) { WT5 = 0; } else WT5 = 1; } WT2 = WT2 + 1; WT3 = 0; WT4 = 0; } else if (time[0][WT1] != '1') { WT1 = 1; WT2 = 0; WT3 = 0; WT4 = 0; } Part2TIME(); } } if (currentscreen == '2' || currentscreen == '3') { SET1_btn.press(down && SET1_btn.contains(pixel_x, pixel_y)); SET2_btn.press(down && SET2_btn.contains(pixel_x, pixel_y)); if (SET1_btn.justPressed()) { SET1_btn.drawButton(true); drawSettingsScreen(); currentscreen = '2'; } if (SET2_btn.justPressed()) { SET2_btn.drawButton(true); drawSettingsScreen2(); currentscreen = '3'; } if (SET1_btn.justReleased()) SET1_btn.drawButton(); if (SET2_btn.justReleased()) SET2_btn.drawButton(); } if (currentscreen == '4') { yes_btn.press(down && yes_btn.contains(pixel_x, pixel_y)); no_btn.press(down && no_btn.contains(pixel_x, pixel_y)); if (yes_btn.justPressed()) { yes_btn.drawButton(true); pump = false; digitalWrite(relay8, HIGH); drawSettingsScreen2(); currentscreen = '3'; } else if (no_btn.justPressed()) { no_btn.drawButton(true); drawSettingsScreen2(); currentscreen = '3'; } if (no_btn.justReleased()) no_btn.drawButton(); if (yes_btn.justReleased()) yes_btn.drawButton(); } } bool Touch_getXY(void) { TSPoint p = ts.getPoint(); pinMode(A2, OUTPUT); //restore shared pins pinMode(A3, OUTPUT); digitalWrite(A2, HIGH); //because TFT control pins digitalWrite(A3, HIGH); bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE); if (pressed) { pixel_x = map(p.y, 61, 953, 0, 480); //.kbv makes sense to me pixel_y = map(p.x, 901, 136, 0, 320); } return pressed; } /*_______________________________________ Test pH is used to get the pH value from the pH sensor and then tells the pumps to add acid or a base accordingly. _______________________________________*/ void TestpH() { for(int i=0;i<10;i++) { buffer_arr[i]=analogRead(A0); //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; pHval = 5.70 * volt - calibration_value; if(pHval < 6.5){ dispenseNutrients(1); } else if(pHval > 7.5) { dispenseNutrients(0); } } /*_______________________________________ Controls the smart lighting of the system. _______________________________________*/ void TurnOnOFFlights() { lightval = analogRead(light_sensor); //Serial.println(lightval); if(lightON == 1) { if(lightval<30) { digitalWrite(relay7, HIGH); } else if (lightval>100) { digitalWrite(relay7,LOW); } } else { digitalWrite(relay7,LOW); } if(lightON == 0){ digitalWrite(relay7,HIGH); } } /*_______________________________________ Function is called to tell change the water out of the system. _______________________________________*/ void ChangeWater() { int timedelay; int Wlevel; bool ix; ix = 0; drawWaitScreen(); digitalWrite(relay4, HIGH); //turn on relay for water out while(ix == 1){ Wlevel = Getwaterlevel(); if(Wlevel < 22) { ix = 1; } } digitalWrite(relay4, LOW); //turn off relay for water out; ix = 0; delay(1000); digitalWrite(relay8, HIGH); //turn off the pump pump = 0; delay(500); digitalWrite(relay5, HIGH); //Relay for water in while(ix == 1){ Wlevel = Getwaterlevel(); if(Wlevel < 14) { ix = 1; } } digitalWrite(relay5,LOW); //turn off relay for water in delay(1000); digitalWrite(relay8, LOW); //turn on the pump pump = 1; delay(500); if(tank1 != 0){ digitalWrite(relay1, HIGH); timedelay = tank1 / 1.66667; timedelay = timedelay * 1000; delay(timedelay); digitalWrite(relay1, LOW); delay(500); } if(tank2 != 0){ digitalWrite(relay2, HIGH); timedelay = tank2 / 1.66667; timedelay = timedelay * 1000; delay(timedelay); digitalWrite(relay2, LOW); delay(500); } if(pH != 0){ digitalWrite(relay3, HIGH); timedelay = pH / 1.66667; timedelay = timedelay * 1000; delay(timedelay); digitalWrite(relay3, LOW); } waterday = day; countDays = 0; drawHomeScreen(); } /*_______________________________________ This function is called to get the water level when the water is being changed to tell the pump when to stop emptying and filling the tank. _______________________________________*/ int Getwaterlevel(){ pinMode(45, OUTPUT); long RangeInInches; ultrasonic.DistanceMeasure(); // get the current signal time; RangeInCentimeters = ultrasonic.microsecondsToCentimeters(); //convert the time to centimeters delay(100); Serial.println(RangeInCentimeters); return(RangeInCentimeters); } /*_______________________________________ This function is called to see if the current time is the same as the pre-set time to change the water. _______________________________________*/ void CheckTime() { if (waterevery == 0) { return (0); } else if (waterevery == countDays && T1 == WT1 && T2 == WT2 && T3 >= WT3 && T3 <= WT3 + 1 && T4 == WT4 && T5 == WT5) { ChangeWater(); } } /*_______________________________________ This function is called dispense nutrients the function receives an integer and dispenses the nutrients that corresponds to that specific number. _______________________________________*/ void dispenseNutrients(int x) { if (x == 0) { drawWaitScreen(); digitalWrite(relay1, HIGH); delay(10000); digitalWrite(relay1, LOW); } else if (x == 1) { drawWaitScreen(); digitalWrite(relay2, HIGH); delay(10000); digitalWrite(relay2, LOW); } else if (x == 2) { drawWaitScreen(); digitalWrite(relay3, HIGH); delay(10000); digitalWrite(relay3, LOW); } drawHomeScreen(); } /*_______________________________________ This function converts the millis function into presenting time in standard time. _______________________________________*/ void updatetime() { if(pHval>pHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHval= Tmill + 60000) { minutes = minutes + (millis() - Tmill); Tmill = millis(); ix = minutes / 60000; minutes = minutes % 60000; do { if (time[4][T4] != '9') { T4 = T4 + 1; } else if (time[3][T3] != '5') { T3 = T3 + 1; T4 = 0; } else if (time[0][T1] == '1' && time[1][T2] == '2' && time[3][T3] == '5') { T1 = 0; T2 = 1; T3 = 0; T4 = 0; } else if (time[1][T2] != '9') { if (time[0][T1] == '1' && time[1][T2] == '1') { if (T5 == 1) { T5 = 0; if (day == 6) { day = 0; } else { day = day + 1; } if (currentscreen == '0') { countDays = countDays + 1; updateday(); } } else T5 = 1; } T2 = T2 + 1; T3 = 0; T4 = 0; } else if (time[0][T1] != '1') { T1 = 1; T2 = 0; T3 = 0; T4 = 0; } if (currentscreen == '0') { updatetime(); } ix = ix - 1; } while (ix == 1); } } void Part2TIME() { if(pHval>pHHigh || pHvalpHHigh || pHvalpHHigh || pHvalpHHigh || pHval