#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#include <RTClib.h>
RTC_DS1307 rtc;
#include <SD.h>
#define CS_pin 10
File myFile;
#define ACTIVE true
#define DEACTIVE false
byte FT_tankFill_flag[4];
const int FT_tanks_LowLevelinput[4]={3,4,5,6};
boolean Active_FT_tanks[4] = {DEACTIVE,DEACTIVE,DEACTIVE,DEACTIVE};
long mills = 60000;
int tankA_FT_ReleaseTime_inMin = 1;// minuts
int tankB_FT_ReleaseTime_inMin = 10;// minuts
int tankC_FT_ReleaseTime_inMin = 10;//minuts
int tankD_FT_ReleaseTime_inMin = 10;//minuts
String FT_name[4];
String tankA_FT_dateSchedule[] = {};
String tankB_FT_dateSchedule[] = {};
String tankC_FT_dateSchedule[] = {};
String tankD_FT_dateSchedule[] = {};
String tankA_FT_timeSchedule[] = {};
String tankB_FT_timeSchedule[] = {};
String tankC_FT_timeSchedule[] = {};
String tankD_FT_timeSchedule[] = {};
byte tankA_FT_perDayCount = 0;
byte tankB_FT_perDayCount = 0;
byte tankC_FT_perDayCount = 0;
byte tankD_FT_perDayCount = 0;
boolean tankA_FT_on = false;
boolean tankB_FT_on = false;
boolean tankC_FT_on = false;
boolean tankD_FT_on = false;
boolean wateringplants = false;
void check_FT_tanksLow();
void check_FT_dateSchedule();
void check_FT_timeSchedule();
void setup(){
lcd.init();
rtc.begin();
if (! rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
for(int i=0; i<=3; i++){
pinMode(FT_tanks_LowLevelinput[i], INPUT_PULLUP);
}
Serial.begin(9600);
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("TronicAGR");
lcd.setCursor(0,1);
lcd.print("Fertilizer Unit v1.0");
delay(2000);
lcd.clear();
//Serial.println(sizeof(tankA_FT_dateSchedule)/6);
if (!SD.begin(CS_pin)) {
Serial.println(F("SD CARD FAILED, OR NOT PRESENT!"));
while (1); // don't do anything more:
}
Serial.println(F("SD CARD INITIALIZED."));
// open file for reading
//while(1){
myFile = SD.open("FTDLoger.csv", FILE_READ);
if (myFile) {
int line_count = 0;
while (myFile.available()) {
char line[100]; // maximum is 100 characters, change it if needed
int line_length = myFile.readBytesUntil('\n', line, 100); // read line-by-line from Micro SD Card
line_count++;
Serial.print(F("Line "));
Serial.print(line_count);
Serial.print(F(": "));
Serial.write(line, line_length); // print the character to Serial Monitor
// \n character is escaped by readBytesUntil function
Serial.write('\n'); // print a new line charactor
}
myFile.close();
} else {
Serial.print(F("SD Card: error on opening file arduino.txt"));
}
// }
}
void loop() {
// watering plans
if(wateringplants){
}
// fertilizing plants
else{
check_FT_tanksLow();
check_FT_dateSchedule();
check_FT_timeSchedule();
}
}
void check_FT_tanksLow(){
for(int i=0; i<=3; i++){
FT_tankFill_flag[i] = digitalRead(FT_tanks_LowLevelinput[i]);
//Serial.print(String(FT_tankFill_flag[i]) + ", ");
}
//Serial.println();
}
void check_FT_dateSchedule(){
DateTime now = rtc.now();
String ScheduleDate = String(now.year())+String(now.month())+String(now.day());
//Serial.println(ScheduleDate);
// check for tank A schedule
if((sizeof(tankA_FT_dateSchedule)/6 ) > 0){ // check how many schedules for tank A
for(int i=0; i < sizeof(tankA_FT_dateSchedule)/6 ; i++ ){
if(ScheduleDate == tankA_FT_dateSchedule[i] ){
Active_FT_tanks[0] = ACTIVE;
// load tank A time schedule for the date
}
else{
Active_FT_tanks[0] = DEACTIVE;
}
}
}
// check for tank B schedule
if((sizeof(tankB_FT_dateSchedule)/6 ) > 0){ // check how many schedules for tank A
for(int i=0; i < sizeof(tankB_FT_dateSchedule)/6 ; i++ ){
if(ScheduleDate == tankB_FT_dateSchedule[i] ){
Active_FT_tanks[1] = ACTIVE;
// load tank B time schedule for the date
}
else{
Active_FT_tanks[1] = DEACTIVE;
}
}
}
// check for tank C schedule
if((sizeof(tankC_FT_dateSchedule)/6 ) > 0){ // check how many schedules for tank A
for(int i=0; i < sizeof(tankC_FT_dateSchedule)/6 ; i++ ){
if(ScheduleDate == tankC_FT_dateSchedule[i] ){
Active_FT_tanks[2] = ACTIVE;
// load tank B time schedule for the date
}
else{
Active_FT_tanks[2] = DEACTIVE;
}
}
}
// check for tank D schedule
if((sizeof(tankD_FT_dateSchedule)/6 ) > 0){ // check how many schedules for tank A
for(int i=0; i < sizeof(tankD_FT_dateSchedule)/6 ; i++ ){
if(ScheduleDate == tankD_FT_dateSchedule[i] ){
Active_FT_tanks[3] = ACTIVE;
// load tank C time schedule for the date
}
else{
Active_FT_tanks[3] = DEACTIVE;
}
}
}
}
void check_FT_timeSchedule(){
DateTime now = rtc.now();
String ScheduleTime = String(now.hour())+String(now.minute());
//Serial.println(ScheduleTime.toInt());
//////////////////////////////////////////////////// tank A
if(Active_FT_tanks[0]){ // check tank A active
int startTime ;
if(FT_tankFill_flag[0] == LOW){// if fertilizer tank a filled
//Serial.println("tank A Active");
if( tankA_FT_perDayCount >= sizeof(tankA_FT_timeSchedule)/6){
Active_FT_tanks[0] = DEACTIVE;
// genarate report
Serial.println("Tank A fertilizing Schedule End");
Serial.println("Fertilized " +String(tankA_FT_perDayCount) + " times for the day");
tankA_FT_perDayCount = 0;
}
if(ScheduleTime == tankA_FT_timeSchedule[tankA_FT_perDayCount]){
startTime = ScheduleTime.toInt();
tankA_FT_on = true;
}
if(tankA_FT_on){
if((ScheduleTime.toInt() - startTime) >= tankA_FT_ReleaseTime_inMin){
// Disable tank A output
Serial.println("FT tankA off");
tankA_FT_perDayCount++;
tankA_FT_on = false;
}
else{
// Enable tank A output
Serial.println("FT tank on");
}
}
}
else{ // if tank A empty
Serial.println("ERROR tank A / Fertilizer LOW");
Active_FT_tanks[0] = DEACTIVE;
// Disable tank A output
}
}
}