#include <TimeLib.h>
#include <Wire.h>
#include <RTClib.h>
// Pin Definitions
const int inputPin = 2; // On/Off switch
const int outputLowPin = 3; // Run speed low
const int outputHighPin = 4; // Run speed high
const int powerKillPin = 5; // Power kill relay
// Mode and State Variables
bool isLowSpeed = false;
bool isHighSpeed = false;
bool toggleState = false;
bool toggledOnce = false; // Flag to track if toggle has occurred
unsigned long lastChangeTime = 0;
unsigned long lastInputTime = 0;
unsigned long lastInputTimeHighLowToggle = 0;
unsigned long powerKillTime = 0;
unsigned long cnt1 = 0;
//RTC_DS3231 rtc; // Initialize RTC object
// Define the timer values (in 24-hour format)
int start_time_1 = 1200;
int stop_time_1 = 1300;
int start_time_2 = 800;
int stop_time_2 = 1700;
// Define variables to indicate if the timers are active
bool timerOneActive = false;
bool timerTwoActive = false;
// Define a variable to keep track of the last update time
unsigned long previousMillis = 0;
const unsigned long interval = 10000; // 10 seconds
void setup() {
pinMode(inputPin, INPUT);
pinMode(outputLowPin, OUTPUT);
pinMode(outputHighPin, OUTPUT);
pinMode(powerKillPin, OUTPUT);
// Initialize outputs
digitalWrite(outputLowPin, LOW);
digitalWrite(outputHighPin, LOW);
digitalWrite(powerKillPin, LOW);
// Initialize Serial Monitor
Serial.begin(15200);
toggleState = 1;
// Set up Time library
Serial.print("Current UNIX time: ");
Serial.println(now());
setTime(now());
}
void loop() {
// Get current time
int currentHour = hour();
int currentMinute = minute();
// Simulate hours (0-23)
currentHour = (millis() / 3600000) % 24 + 14;
currentMinute = (millis() / 60000) % 60 + 15; // Simulate minutes (0-59)
//Serial.println(millis());
// Read input state
bool inputState = digitalRead(inputPin);
unsigned long currentTimeMills = millis();
// Get the current time in 24-hour format
int currentTime24hourFormat = currentHour * 100 + currentMinute;
// Print the current time for debugging
//Serial.print("Current Time (24-hour format): ");
//Serial.println(currentTime24hourFormat);
// Handle Manual input
handleInput(inputState, currentTimeMills);
// Power kill delay
handlePowerKill(currentTimeMills);
// Get the current millis
unsigned long currentMillis = millis();
// Check if 10 seconds have passed
if (currentMillis - previousMillis >= interval) {
// Save the last time you updated
previousMillis = currentMillis;
// Call the functions to check if the timers are active
timerOneActive = isTimerActive(currentTime24hourFormat, start_time_1, stop_time_1);
timerTwoActive = isTimerActive(currentTime24hourFormat, start_time_2, stop_time_2);
Serial.print("Timer One Active: ");
Serial.println(timerOneActive ? "Yes" : "No");
Serial.print("Timer Two Active: ");
Serial.println(timerTwoActive ? "Yes" : "No");
}
}
void handleInput(bool inputState, unsigned long currentTime) {
// If input is turned on
if (inputState == HIGH) {
if(toggleState == 0){
digitalWrite(outputLowPin, HIGH);
digitalWrite(outputHighPin, LOW);
isLowSpeed = false;
isHighSpeed = true;
}
if(toggleState == 1){
digitalWrite(outputLowPin, LOW);
digitalWrite(outputHighPin, HIGH);
isLowSpeed = true;
isHighSpeed = false;
}
}
// Check if input state has changed and it's true
if (inputState == HIGH && !toggledOnce) {
// Check if the change occurred within 1 second
if (millis() - lastChangeTime < 1000) {
// Toggle the state variable
toggledOnce = true;
toggleState = !toggleState;
Serial.print("Toggle state: ");
Serial.println(toggleState);
}
}
if (inputState == HIGH){
lastChangeTime = millis();
}
// If input is off for more than 1 second, turn off both speeds
if (inputState == LOW) {
toggledOnce = false;
if (currentTime - lastInputTime > 1000) {
digitalWrite(outputLowPin, LOW);
digitalWrite(outputHighPin, LOW);
isLowSpeed = false;
isHighSpeed = false;
toggleState = 1;
}
delay(500);
}else{
lastInputTime = currentTime; // Update last input change time
}
//}
}
void handlePowerKill(unsigned long currentTime) {
// Check if both speeds have been off for 6 seconds
if (!isLowSpeed && !isHighSpeed && (currentTime - powerKillTime >= 6000)) {
digitalWrite(powerKillPin, LOW); // Turn off power kill relay
}
if (isLowSpeed || isHighSpeed) {
digitalWrite(powerKillPin, HIGH); // Turn on power kill relay
powerKillTime = currentTime;
}
}
// Function to check if the timer is active
bool isTimerActive(int currentTime, int startTime, int stopTime) {
// Print the values for debugging
Serial.print("Current Time: ");
Serial.println(currentTime);
Serial.print("Start Time: ");
Serial.println(startTime);
Serial.print("Stop Time: ");
Serial.println(stopTime);
if (startTime < stopTime) {
// Normal case: timer does not span over midnight
return (currentTime >= startTime && currentTime < stopTime);
} else {
// Spanning midnight case
return (currentTime >= startTime || currentTime < stopTime);
}
}