Last active
October 26, 2023 08:44
-
-
Save DigantaDey/2a815a098a1aa126769e43e2cc7da43f to your computer and use it in GitHub Desktop.
Arduino Time-based Relay Control: Automates relay based on IST hours with a manual override button. Includes status LED & serial feedback using TimeLib.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <TimeLib.h> | |
const int relayPin = 7; | |
const int buttonPin = 2; // Button for manual override | |
const int ledPin = 13; // On-board LED to indicate relay status | |
// Adjustable start and end times | |
int startTime = 4; // 4 AM | |
int endTime = 19; // 7 PM | |
bool manualOverride = false; // Flag to track if lights are manually overridden | |
void setup() { | |
pinMode(relayPin, OUTPUT); | |
pinMode(buttonPin, INPUT_PULLUP); // Use internal pullup for button | |
pinMode(ledPin, OUTPUT); | |
digitalWrite(relayPin, LOW); | |
attachInterrupt(digitalPinToInterrupt(buttonPin), toggleLight, FALLING); // Interrupt for button press | |
Serial.begin(9600); | |
Serial.println("System Started"); | |
} | |
void loop() { | |
int currentHour = hour() + 5; // Add 5 for IST | |
int currentMinute = minute() + 30; // Add 30 minutes for IST | |
if (currentMinute >= 60) { | |
currentMinute -= 60; | |
currentHour += 1; | |
} | |
if (currentHour >= 24) { | |
currentHour -= 24; | |
} | |
if (!manualOverride) { // If not manually overridden | |
if (currentHour >= startTime && currentHour < endTime) { | |
turnOnLight(); | |
} else { | |
turnOffLight(); | |
} | |
} | |
delay(60000); | |
} | |
void turnOnLight() { | |
digitalWrite(relayPin, HIGH); | |
digitalWrite(ledPin, HIGH); // Indicate relay status | |
Serial.println("Light Turned ON"); | |
} | |
void turnOffLight() { | |
digitalWrite(relayPin, LOW); | |
digitalWrite(ledPin, LOW); // Indicate relay status | |
Serial.println("Light Turned OFF"); | |
} | |
void toggleLight() { // Interrupt service routine for button press | |
if (manualOverride) { // If currently overridden | |
manualOverride = false; | |
} else { | |
manualOverride = true; | |
digitalWrite(relayPin, !digitalRead(relayPin)); // Toggle light state | |
digitalWrite(ledPin, !digitalRead(ledPin)); // Indicate relay status | |
} | |
delay(200); // Debounce delay | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment