// Pin Definitions
const int irSensorPin = 12;
const int trigPin = 33;
const int echoPin = 34;

// RGB LED Strip Pins
const int rgb1RedPin = 35;
const int rgb1GreenPin = 32;
const int rgb1BluePin = 25;
const int rgb2RedPin = 26;
const int rgb2GreenPin = 27;
const int rgb2BluePin = 14;
const int rgb3RedPin = 19;
const int rgb3GreenPin = 18;
const int rgb3BluePin = 5;

bool systemOn = false;
bool irState = LOW;

void setup() {
  Serial.begin(115200);

  pinMode(irSensorPin, INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(rgb1RedPin, OUTPUT);
  pinMode(rgb1GreenPin, OUTPUT);
  pinMode(rgb1BluePin, OUTPUT);

  pinMode(rgb2RedPin, OUTPUT);
  pinMode(rgb2GreenPin, OUTPUT);
  pinMode(rgb2BluePin, OUTPUT);

  pinMode(rgb3RedPin, OUTPUT);
  pinMode(rgb3GreenPin, OUTPUT);
  pinMode(rgb3BluePin, OUTPUT);
}

void loop() {
  bool currentIrState = digitalRead(irSensorPin);

  // Detect rising edge of IR sensor
  if (currentIrState == HIGH && irState == LOW) {
    systemOn = !systemOn; // Toggle system state
    delay(200); // Debounce delay
    
  }
  irState = currentIrState;
  
  if (systemOn) {
    float distance = measureDistance();
    
    if (distance <= 6) {
      setColorAllStrips(255, 255, 255); // White
    } else if (distance <= 9) {
      setColorStrip1(255, 0, 0); 	// Red_PAIR 1
      setColorStrip2(0, 0, 0); 		
      setColorStrip3(0, 0, 0); 		
    } else if (distance <= 12) {
      setColorStrip1(0, 0, 0);
      setColorStrip2(0, 255, 0); 	// Green_PAIR 2
      setColorStrip3(0, 0, 0); 
    } else if (distance <= 15) {
      setColorStrip1(0, 0, 0);
      setColorStrip2(0, 0, 0);
      setColorStrip3(0, 0, 255);	// Blue_PAIR 3
      
    } else if (distance <= 18) {
      blinkRandomColors();			// All Pair Blink Randomly
    } else if (distance <= 30) {
      displayRandomEffects(); 		// All Pair Random Effects
    }
  } else {
    setColorAllStrips(0, 0, 0); 	// Turn off all LEDs
  }
  delay(100);
}

float measureDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  float distance = duration * 0.034 / 2;
  return distance;
}

void setColorAllStrips(int red, int green, int blue) {
  setColorStrip1(red, green, blue);
  setColorStrip2(red, green, blue);
  setColorStrip3(red, green, blue);
}

void setColorStrip1(int red, int green, int blue) {
  analogWrite(rgb1RedPin, red);
  analogWrite(rgb1GreenPin, green);
  analogWrite(rgb1BluePin, blue);
}

void setColorStrip2(int red, int green, int blue) {
  analogWrite(rgb2RedPin, red);
  analogWrite(rgb2GreenPin, green);
  analogWrite(rgb2BluePin, blue);
}

void setColorStrip3(int red, int green, int blue) {
  analogWrite(rgb3RedPin, red);
  analogWrite(rgb3GreenPin, green);
  analogWrite(rgb3BluePin, blue);
}

void blinkRandomColors() {
  for (int i = 0; i < 5; i++) {
    setColorAllStrips(random(0, 256), random(0, 256), random(0, 256));
    delay(250);
    setColorAllStrips(0, 0, 0);
    delay(250);
  }
}

void displayRandomEffects() {
  for (int i = 0; i < 5; i++) {
    setColorStrip1(random(0, 256), random(0, 256), random(0, 256));
    setColorStrip2(random(0, 256), random(0, 256), random(0, 256));
    setColorStrip3(random(0, 256), random(0, 256), random(0, 256));
    delay(500);
  }
}