#include <Arduino.h>
class Relay {
private:
int pin;
public:
// Constructor to initialize relay pin
Relay(int relayPin) {
pin = relayPin;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW); // Initialize relay as OFF
}
// Method to turn relay ON
void turnOn() {
digitalWrite(pin, HIGH);
Serial.println("Relay is ON");
}
// Method to turn relay OFF
void turnOff() {
digitalWrite(pin, LOW);
Serial.println("Relay is OFF");
}
};
Relay relay1(7); // Create an object of Relay with pin 7
void setup() {
Serial.begin(9600);
}
void loop() {
// Example: Turn relay ON for 5 seconds, then OFF for 5 seconds
relay1.turnOn();
delay(1000);
relay1.turnOff();
delay(1000);
}