#include <Ultrasonic.h>

class UltrasonicSensor {
  private:
    int trigPin;
    int echoPin;
    Ultrasonic ultrasonic;

  public:
    UltrasonicSensor(int trigPin, int echoPin):
    trigPin(trigPin),
    echoPin(echoPin), ultrasonic(trigPin, echoPin) {
      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
    }

    float getDistance() {
      return ultrasonic.distanceRead();
    }
};

class RelayModule {
  private:
    int relayPin;

  public:
    RelayModule(int relayPin) : relayPin(relayPin) {
      pinMode (relayPin, OUTPUT);
    }

    void turnOn() {
      digitalWrite(relayPin, HIGH);
    }
    
    void turnOff() {
      digitalWrite(relayPin, LOW);
    }
};

class LampToggle {
private:
    bool isOn;  // BOOL FOR STATE CHECKER

public:
    LampToggle() : isOn(false) {}

    void turnOn() {
        isOn = true;
    }
    void turnOff() {
        isOn = false;
    }

    // TOGGLE MODE
    void toggle() {
        isOn = !isOn;
        // BY INITIALIZATION, lamp toggle will go false or true based on LampState
    }

    // STATE CHECKER
    bool isLampOn() const {
        return isOn;
    }
};

const int trigPin = 13;
const int echoPin = 12;
const int relayPin = 2;

UltrasonicSensor usSensor(trigPin, echoPin);
RelayModule relayOne(relayPin);
LampToggle lampOne;

void setup(){
  Serial.begin(9600); // DEBUGGING ONLY
}

void loop(){
  float distance = usSensor.getDistance();

  //Lamp light checker to simulate a switch style toggling.
  if (distance < 15) {
    lampOne.toggle();

    if(lampOne.isLampOn()){
      relayOne.turnOn();

    } else {
      relayOne.turnOff();

    }  
  } 
  //THIS IS STRICTLY FOR DEBUGGING ONLY
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500); // 500 ms DELAY FOR STABILITY
}
$abcdeabcde151015202530fghijfghij
NOCOMNCVCCGNDINLED1PWRRelay Module