#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Initialize the I2C LCD with the I2C address (usually 0x27 or 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);  // 0x27 is the I2C address, 16x2 is the LCD size

// Define IR sensor pin
const int irPin = 6;           // IR sensor pin

// Define other pins
const int microphonePin = A0;  // Microphone input pin
const int ledPin = 9;          // LED connected to PWM pin 9
const int pistonPin = 3;       // Pneumatic piston control pin
const int motorPin = 10;       // Motor control pin
const int motorReversePin = 5; // Motor reverse control pin

bool objectDetected = false;   // Flag to track if an object was detected
bool wonPolaroid = false;      // Flag to track if the Polaroid was won
bool wonCandy = false;         // Flag to track if candy was won

void setup() {
  Serial.begin(9600);
  
  pinMode(irPin, INPUT_PULLUP);         // Set IR sensor pin as input
  pinMode(ledPin, OUTPUT);       // LED pin
  pinMode(pistonPin, OUTPUT);    // Pneumatic piston pin
  pinMode(motorPin, OUTPUT);     // Motor pin

  lcd.init();
  lcd.backlight();

  lcd.print("Put candy in top");
  lcd.setCursor(0, 1);
  lcd.print("  hole to begin");

  randomSeed(analogRead(0));
}

void loop() {
  int audioValue = analogRead(microphonePin);

  // Check if an object is detected by the IR sensor
  int irValue = digitalRead(irPin);

  Serial.print("IR Sensor: ");
  Serial.println(irValue);

  if (irValue == HIGH && !objectDetected) {
    lcd.clear();
    delay(1000);
    lcd.print("Scream \"Trick");
    lcd.setCursor(0, 1);
    lcd.print("or Treat!!!\"");
    objectDetected = true;
    Serial.println("Object detected!");
  }

  if (audioValue > 100 && objectDetected) {
    analogWrite(ledPin, 255);
    delay(50);
    analogWrite(ledPin, 0);

    int outcome = random(100);

    lcd.clear();
    delay(100);

    if (outcome < 50) {
      lcd.print("You get Tricked! :(");
      Serial.println("Outcome: Nothing won");
    } else if (outcome < 90) {
      lcd.print("You get a Treat!");
      Serial.println("Outcome: Candy won");
      wonCandy = true;
    } else {
      lcd.print("YOU GOT A");
      lcd.setCursor(0, 1);
      lcd.print("POLAROID!!! :)");
      Serial.println("Outcome: Polaroid won");
      wonPolaroid = true;
    }

    delay(2000);

    if (wonPolaroid) {
      Serial.println("Starting countdown for Polaroid...");
      for (int i = 5; i > 0; i--) {
        lcd.clear();
        lcd.print("Polaroid in:");
        lcd.setCursor(0, 1);
        lcd.print(i);
        lcd.print(" seconds...");
        delay(1000);
      }

      digitalWrite(pistonPin, HIGH);
      delay(1000);
      digitalWrite(pistonPin, LOW);
      Serial.println("Polaroid piston deactivated");
    }

    if (wonCandy) {
      delay(1500);
    
      digitalWrite(motorPin, HIGH);
      delay(2000);
      digitalWrite(motorPin, LOW);

      digitalWrite(motorReversePin, HIGH);
      delay(2000);
      digitalWrite(motorReversePin, LOW);

      wonCandy = false;
    }

    resetForNextRound();
  }

  delay(100);
}

void resetForNextRound() {
  Serial.println("Starting reset process...");
  delay(5000);
  lcd.init();
  lcd.clear();
  delay(100);
  lcd.print("Put candy in top");
  lcd.setCursor(0, 1);
  lcd.print("  hole to begin");

  objectDetected = false;
  wonPolaroid = false;
  wonCandy = false;

  Serial.println("System reset for next round.");
}
