#include<Arduino.h>
#define toggleSwitchPin 2 // Define the toggle switch pin
const int relayPin = 4; // Output pin for relay
const int ledPin = 9;
const unsigned long debounceDelay = 50; // Adjust debounce delay as needed
unsigned long counter = 0, lastMillis= 0;
bool is12HourMode = true; // Initially, consider 12-hour mode
unsigned long counter24Hrs = 86400 , counter12Hrs = 43200;
/**
* @brief Reads the state of a toggle switch and initializes pin modes.
*
* This function reads the state of a toggle switch connected to a digital pin on the Arduino board,
* implements debounce logic, and sets a global variable (`is12HourMode`) based on the state of the switch.
* It also initializes pin modes for the toggle switch pin, relay pin, and the built-in LED.
* This function is typically called within the `setup()` function of an Arduino sketch.
*
* @param None
* @return None
*/
void handleToggleSwitch() {
static int previousToggleState = LOW; // Store previous state
int toggleState1, toggleState2;
toggleState1 = digitalRead(toggleSwitchPin);
delay(debounceDelay);
toggleState2 = digitalRead(toggleSwitchPin);
if (toggleState1 == toggleState2 && toggleState1 != previousToggleState) {
is12HourMode = (toggleState1 == LOW); // True for LOW (12-hour), False for HIGH (24-hour)
previousToggleState = toggleState1;
if(is12HourMode)
{
digitalWrite(ledPin, HIGH);
Serial.println("12 Hrs Mode");
}
else{
digitalWrite(ledPin, LOW);
Serial.println("24 Hrs Mode");
}
}
}
void setupTimer()
{
//For 1 Second Time Generate on Timer 1.
cli(); //stop interrupts
//set timer1 interrupt at 1Hz
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; //initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624; // = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); //allow interrupts
}
/**
* @brief Converts a counter value to a timestamp format and prints it to the serial monitor.
*
* This function takes a counter value (representing elapsed time in seconds) and converts it to
* hours, minutes, and seconds format. It then prints the timestamp to the serial monitor in the
* format "hh:mm:ss".
*
* @param None
* @return None
*/
void counter2TimeStamp()
{
uint32_t t = counter;
uint16_t h;
uint8_t m, s;
s = t % 60;
t = (t - s)/60;
m = t % 60;
t = (t - m)/60;
h = t;
Serial.print(h);
Serial.print(":");
Serial.print(m);
Serial.print(":");
Serial.println(s);
}
/**
* @brief Resets the counter variable to zero.
*
* This function sets the value of the global variable `counter` to zero, effectively resetting
* the counter to its initial state.
*
* @param None
* @return None
*/
void resetCounter()
{
counter = 0;
}
// /**
// * @brief Increments the counter variable every second.
// *
// * This function increments the global variable `counter` by one every second
// * based on the difference between the current time and the last recorded time.
// * Additionally, it prints the elapsed time to the serial monitor using the
// * `counter2TimeStamp()` function.
// *
// * @param None
// * @return None
// */
// void incSecCounter()
// {
// unsigned long currentMillis = millis();
// if (currentMillis - lastMillis >= 1000) {
// counter++;
// lastMillis = currentMillis;
// Serial.print("Time Elapsed: ");
// counter2TimeStamp();
// }
// }
/**
* @brief Controls the relay by turning it on for 6 seconds.
*
* This function controls the relay by first turning it on, printing a message to the serial monitor,
* and then waiting for 6 seconds. After 6 seconds, it turns off the relay and prints another message
* to the serial monitor.
*
* @param None
* @return None
*/
void switchRelay() {
Serial.println("Switching Relay ON");
digitalWrite(relayPin, HIGH); // Turn on the relay
delay(6000); // Wait for 6 seconds
Serial.println("Switching Relay OFF");
digitalWrite(relayPin, LOW); // Turn off the relay
}
/**
* @brief Checks if the counter has reached the specified limit and takes action accordingly.
*
* This function checks if the value of the global variable `counter` has exceeded the specified limit
* based on the current mode (12-hour or 24-hour). If the limit is reached, it triggers the `switchRelay()`
* function to turn the relay on for 6 seconds and resets the counter to zero using the `resetCounter()` function.
*
* @param None
* @return None
*/
void checkLimit()
{
if(is12HourMode)
{
if(counter > counter12Hrs)
{
switchRelay();
resetCounter();
}
}
else
{
if(counter > counter24Hrs)
{
switchRelay();
resetCounter();
}
}
}
/**
* @brief Initializes the Arduino board and sets up the initial state of the system.
*
* This function initializes the serial communication with a baud rate of 9600 and prints a welcome message
* to the serial monitor. It also sets the pin modes for the toggle switch pin, relay pin, and the built-in LED.
* Additionally, it calls the `handleToggleSwitch()` function to read the initial state of the toggle switch.
*
* @param None
* @return None
*/
void setup() {
Serial.begin(9600);
Serial.println("Welcome to the Serial Monitor!");
Serial.println("---------------------------------");
pinMode(toggleSwitchPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
pinMode(ledPin, OUTPUT);
handleToggleSwitch();
switchRelay();
setupTimer();
}
/**
* @brief Main loop of the Arduino sketch, continuously executes the program logic.
*
* This function serves as the main loop of the Arduino sketch and continuously executes
* the program logic. It calls the `handleToggleSwitch()` function to handle toggle switch
* state changes, the `incSecCounter()` function to increment the second counter, and the
* `checkLimit()` function to check if the counter has reached the specified limit. It also
* introduces a delay of 1000 milliseconds (1 second) between iterations.
*
* @param None
* @return None
*/
void loop() {
Serial.print("Time Elapsed: ");
counter2TimeStamp();
handleToggleSwitch();
// incSecCounter();
checkLimit();
delay(1000);
}
ISR(TIMER1_COMPA_vect) //Timer1 interrupt 1Hz toggles pin 10 (LED)
{
counter++;
}