#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define LED_PIN 2        // Pin connected to the LED
#define PIR_PIN 13       // Pin connected to the PIR sensor
#define BUZZER_PIN 4     // Pin connected to the buzzer

#define SCREEN_ADDRESS 0x3C  // I2C address for the OLED display
#define OLED_RESET    -1      // Reset pin not used

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); //OLED_RESET

void setup() {
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");

  pinMode(LED_PIN, OUTPUT);
  pinMode(PIR_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  // Initialize the display
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  //display.begin(SCREEN_ADDRESS, OLED_RESET);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.display();
}

void loop() {
  //int pirValue = digitalRead(PIR_PIN);  // Read PIR sensor value

  int pirValue = HIGH;
  if (pirValue == HIGH) {  // Motion detected
    // Display message on the OLED
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print("Time to Take pills!");

    display.display();
    
    digitalWrite(LED_PIN, HIGH);  // Turn LED on
    Serial.println("Time to     Take Pills!");  // Print message
    tone(BUZZER_PIN, 1000, 500);  // Play sound for 1 second, pin, frequency, duration

    delay(2000);  // Wait for 2 seconds to avoid multiple triggers
  } else {
    digitalWrite(LED_PIN, LOW);   // Turn LED off
  }

  delay(100);  // Small delay for stability
}