#define BLYNK_TEMPLATE_ID           "TMPL4Vs37GFAr""TMPL4Vs37GFAr"
#define BLYNK_TEMPLATE_NAME         "final"
#define BLYNK_AUTH_TOKEN            "CS2gtrmggRNot7rgg0cLDXcmTpGXDP7F"

#define BLYNK_PRINT Serial
//#define SIGNAL_PIN 33 //water sensor
#define TONE_OUTPUT_PIN 26
#define JOYSTICK_X_PIN 34
#define JOYSTICK_Y_PIN 35
#define JOYSTICK_SW_PIN 32
#define THRESHOLD 40

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "AP9";
char pass[] = "";

// Variables to manage states
bool isMedReminderActive = false;
bool isWaterReminderActive = false;
bool isCheckInActive = false;

void setup()
{
  // Debug console
  Serial.begin(115200);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  pinMode(13, OUTPUT); // define led as output
  pinMode(TONE_OUTPUT_PIN, OUTPUT); // define speaker as output
  pinMode(JOYSTICK_SW_PIN, INPUT_PULLUP); // HIGH when not pressed, LOW when pressed

}

void loop()
{
  Blynk.run();
  checkJoystick();
  updateBlinkingLED();
  
  delay(100);
}

unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 500; // interval at which to blink (milliseconds)

void updateBlinkingLED() {
  unsigned long currentMillis = millis();
  
  if (isWaterReminderActive && !isCheckInActive) {
    if (currentMillis - previousMillis >= interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;

      // if the LED is off, turn it on, and vice-versa:
      int ledState = digitalRead(13); // assuming LED is connected to pin 13
      digitalWrite(13, !ledState);
    }
  } else if(!isWaterReminderActive && !isCheckInActive){
    digitalWrite(13, LOW); // Ensure LED is off when not active
  }
}

// Blynk writes for reminders
BLYNK_WRITE(V0) { // Medication Reminder
  if (param.asInt()) {
    isMedReminderActive = true;
    tone(TONE_OUTPUT_PIN, 1000); // Start alarm
  }
}

BLYNK_WRITE(V1) { // Water Reminder
  if (param.asInt()) {
    isWaterReminderActive = true;
  }
}

BLYNK_WRITE(V2) { // Check-In Reminder
  if (param.asInt()) {
    isCheckInActive = true;
    digitalWrite(13, HIGH); // Turn on LED continuously
  }

}

void checkJoystick() {
  int xValue = analogRead(JOYSTICK_X_PIN);
  Serial.print("x value is :" );
  Serial.println(xValue);
  
  int yValue = analogRead(JOYSTICK_Y_PIN);
  Serial.print("y value is :");
  Serial.println(yValue);
  
  int buttonValue = digitalRead(JOYSTICK_SW_PIN);
  bool buttonPressed;
  
  if(buttonValue == 0){
   buttonPressed = true; // button is pressed
   Serial.print("buttonPressed value is :");
   Serial.println(buttonPressed);

  } else {
    buttonPressed = false; // button not pressed
    Serial.print("buttonPressed value is :");
    Serial.println(buttonPressed);

  }

  if (buttonPressed && isMedReminderActive) {
    isMedReminderActive = false;
    noTone(TONE_OUTPUT_PIN); // Stop alarm
    Blynk.logEvent("medication_taken");
  }

  if (xValue < 1000 && isWaterReminderActive) { 
    isWaterReminderActive = false;
    Blynk.logEvent("water_reminder_acknowledged");
  }

  if (yValue < 1000 && isCheckInActive) { 
    isCheckInActive = false;
    digitalWrite(13, LOW); // Turn off LED
    Blynk.logEvent("check-in_acknowledged");
  }
}