//#include "blynkiot.h"
#include "commons.h"
#include "uppertank.h"
#include "lowertank.h"
#include "sump.h"
#include "ultrasonic.h"
//#include "buttons.h"
//Set Water Level Distance in CM
int emptyTankDistance = 400 ; //Distance when tank is empty
int fullTankDistance = 25 ; //Distance when tank is full
int emptySumpDistance = 400;
int fullSumpDistance = 25;
int offset = 10; //offset to add/subtract for emptyTankDistance or fullTankDistance
/*Motor & buzzer controls*/
int triggerPer = 25 ; //Set trigger value in percentage ;
int cutoffPer = 100 ; //Set cutoff value in percentage ;
int buzzerReset = LOW;
int buzzerEnable = HIGH;
int uppertankEnable = HIGH;
int lowertankEnable = HIGH;
int sumpEnable = HIGH;
int motorState = LOW;
int modeflag = AUTOMODE;
int bpselectvalue = 100;
int wifiState = LOW;
unsigned int uppertank_beepcnt=0;
unsigned int sump_beepcnt=0;
extern void button_init(void);
extern void check_buttons(void);
extern void blynkiot_init(void);
extern void blynkiot_run(void);
void setup()
{
Serial.begin(9600);// Starts the serial communication
#if WLMS_DEBUG
Serial.println("setup() ");
#endif
pinMode(MODESWITCH, INPUT); // Sets the MODESWITCH as an input
pinMode(MOTORSWITCH, INPUT_PULLUP); // Sets the MOTORSWITCH as an input
pinMode(BUZZERCONTROLSWITCH, INPUT_PULLUP);
pinMode(BUZZERPOINTSELECTSWITCH, INPUT);
pinMode(BUZZER, OUTPUT); // Sets the BUZZER as an Output
pinMode(RELAYMOTOR, OUTPUT); // Sets the MOTOR as an Output
pinMode(MODEINDICATOR_LED, OUTPUT);
pinMode(RELAYMOTOR, OUTPUT);
pinMode(BUZZERPOINTSELECT_100_INDICATOR_LED, OUTPUT);
pinMode(BUZZERPOINTSELECT_75_INDICATOR_LED, OUTPUT);
pinMode(BUZZERPOINTSELECT_50_INDICATOR_LED, OUTPUT);
pinMode(WIFI_INDICATOR_LED, OUTPUT);
bpselectvalue = cutoffPer = 100 ;
modeflag = AUTOMODE;
motorState = LOW;
wifiState = LOW;
buzzerReset = LOW;
buzzerEnable = HIGH;
uppertankEnable = HIGH;
lowertankEnable = HIGH;
sumpEnable = HIGH;
digitalWrite(MODEINDICATOR_LED, modeflag);
digitalWrite(RELAYMOTOR, motorState);
//TODO: RELAY MOTOR HANDLING
digitalWrite(BUZZERPOINTSELECT_100_INDICATOR_LED, HIGH);
digitalWrite(WIFI_INDICATOR_LED, wifiState);
#if BUTTONS_ENABLE
button_init();
#endif
#if LOWERTANK_ENABLE
lowertank_init();
#endif
#if UPPERTANK_ENABLE
uppertank_init();
#endif
#if SUMP_ENABLE
sump_init();
#endif
#if BLYNK_ENABLE
blynkiot_init();
#endif
}
void loop()
{
#if BLYNK_ENABLE
blynkiot_run();
#endif
#if LOWERTANK_ENABLE
check_lowertank();
#endif
#if UPPERTANK_ENABLE
check_uppertank();
#endif
#if SUMP_ENABLE
check_sump();
#endif
#if BUTTONS_ENABLE
check_buttons();
#endif
}