// Define the pins used for the sensors and buzzer
const int motionPin = 2;
const int blinkPin = A0;
const int buzzerPin = 9;

// Define the blink threshold
const int blinkThreshold = 3;

// Define variable for counting blinks
int blinkCount = 0;

// Set up the system
void setup() {
  pinMode(motionPin, INPUT);
  pinMode(blinkPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
}

// Check for motion and blink detection
void loop() {
  // Check for motion
  if (digitalRead(motionPin) == HIGH) {
    // Reset blink count
    blinkCount = 0;

    // Wait for a moment to let the person settle in
    delay(5000);
  }

  // Check for blinks
  if (analogRead(blinkPin) > 700) {
    // Increment blink count
    blinkCount++;

    // Print blink count to serial for debugging
    Serial.println("Blink count: " + String(blinkCount));

    // Check if blink threshold has been exceeded
    if (blinkCount >= blinkThreshold) {
      // Sound the buzzer
      digitalWrite(buzzerPin, HIGH);

      // Wait for a moment to let the person react
      delay(5000);

      // Turn off the buzzer
      digitalWrite(buzzerPin, LOW);

      // Reset blink count
      blinkCount = 0;
    }
  }

  // Wait for a moment to avoid flooding the system with readings
  delay(100);
}