Hello,
I have written the following sketch and I am facing few issues
-
while power on device irrespective of input pins state relay1 is switching ON and OFF for twice actual behaviour should be if both input1 and 2 are high(not connected to GND pin) then the relay 1 should be on and after 1 second relay1 should off along with relay1 led should ON. this whole behaviour is not consistent while power OFF and ON the device
-
this is more troublesome logic in my sketch i.e. input monitoring logic. once relay1 is ON then the monitor timer starts i.e 30 secs and within this time if this pin is not low then relay2 should trigger ON and wait for a second and then OFF and finally will go to block state for 45 mins once lapse this 45 mins time frame then the whole device should reset automatically.
Sometimes after reset the actual functionality is not working at all, like blink led dont respond, relay1 triggers but relay1 led wont lit up etc…
Please help me to resolve this. I tried to fix through AI tools but no luck.
I am using arduino UNO to test this code but my final goal to use ATTiny2313 MCU.
```
// implemented auto timer reset logic after 45 mins
#include <avr/wdt.h>
#define OPENED 1 // Pin not connected (HIGH, due to pull-up resistor)
#define CLOSED 0 // Pin connected to GND (LOW)
const int BLINK_LED = 4;
const int Input1 = 5;
const int Input2 = 6;
const int monitor_input = 7;
const int mode = 8;
const int RELAY1 = 9;
const int RELAY2 = 10;
const int Relay1_Led = 11;
const int monitor_input_LED = 13;
const int ind1_LED = 14;
const int ind2_LED = 15;
bool relay1Activated = false;
bool monitor_input_Active = false;
bool stopHandled = false;
unsigned long monitor_input_StartTime = 0;
unsigned long previousMillis = 0; // Tracks the last time the LED blinked
const unsigned long blinkInterval = 600; // Blink interval in milliseconds
bool blinkState = false;
const unsigned long waitTime = 2700000; // Wait time 45 minutes
unsigned long relayStartMillis = 0;
const unsigned long ledWaitTime = 1800000; // Wait time 30 minutes
unsigned long startTime = 0;
bool timerStarted = false;
bool timeoutReached = false;
bool isMagMode = false;
bool sce1 = false;
bool sce2 = false;
void setup() {
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(monitor_input_LED, OUTPUT);
pinMode(ind2_LED, OUTPUT);
pinMode(ind1_LED, OUTPUT);
pinMode(Relay1_Led, OUTPUT);
pinMode(BLINK_LED, OUTPUT);
pinMode(Input1, INPUT_PULLUP);
pinMode(Input2, INPUT_PULLUP);
pinMode(monitor_input, INPUT_PULLUP);
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);
digitalWrite(monitor_input_LED, LOW);
digitalWrite(ind2_LED, LOW);
digitalWrite(ind1_LED, LOW);
digitalWrite(Relay1_Led, LOW);
pinMode(mode, INPUT_PULLUP);
isMagMode = (digitalRead(mode) == HIGH);
}
void loop() {
// Blink LED (Pin 15) continuously
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= blinkInterval) {
previousMillis = currentMillis; // Update the last blink time
blinkState = !blinkState; // Toggle the blink state
digitalWrite(BLINK_LED, blinkState ? HIGH : LOW); // Set LED state
} // Wait for 500 milliseconds
// LED section
int IN2 = digitalRead(Input2);
int IN1 = digitalRead(Input1);
if(isMagMode){
if (IN2 == CLOSED && IN1 == CLOSED) {
digitalWrite(ind1_LED, LOW); // Yellow OFF
digitalWrite(ind2_LED, HIGH); // Green ON
}
else if (IN1 == CLOSED) {
digitalWrite(ind1_LED, HIGH); // Yellow ON
digitalWrite(ind2_LED, LOW); // Green OFF
}
else {
digitalWrite(ind1_LED, LOW); // Yellow OFF
digitalWrite(ind2_LED, LOW); // Green OFF
}
}
else
{
if ((digitalRead(Input1) == HIGH && digitalRead(Input2) == LOW)) {
// Start timer only once
if (!timerStarted) {
startTime = millis();
timerStarted = true;
timeoutReached = false;
}
// Check timeout
if (!timeoutReached && millis() - startTime >= ledWaitTime) {
timeoutReached = true;
}
// LED control (NO conflict now)
if (!timeoutReached) {
digitalWrite(ind2_LED, HIGH); // Green ON
digitalWrite(ind1_LED, LOW); // Yellow OFF
} else {
digitalWrite(ind2_LED, LOW); // Green OFF
digitalWrite(ind1_LED, HIGH); // Yellow ON
}
}
else {
// Reset everything
timerStarted = false;
timeoutReached = false;
digitalWrite(ind2_LED, LOW);
digitalWrite(ind1_LED, LOW);
}
}
//for scenario 1
if(isMagMode){
sce1 = (digitalRead(Input1) == HIGH && digitalRead(Input2) == HIGH);
}
else{
sce1 = (digitalRead(Input1) == LOW);
}
if (sce1 && !relay1Activated) {
if (relayStartMillis == 0) {
digitalWrite(monitor_input_LED, LOW);
relayStartMillis = millis();
}
if (millis() - relayStartMillis >= 3000) {
delay(2000);
digitalWrite(RELAY1, LOW);
digitalWrite(Relay1_Led, HIGH);
delay(1500); // Wait for 4 seconds
digitalWrite(RELAY1, HIGH);
relay1Activated = true;
relayStartMillis = 0; // Reset the timer to allow re-triggering the delay
}
}
// for monitoring
if (relay1Activated && digitalRead(monitor_input) == HIGH) {
if (!monitor_input_Active) {
monitor_input_StartTime = millis();
monitor_input_Active = true;
}
unsigned long monitor_input_currentMillis = millis();
if (monitor_input_currentMillis - monitor_input_StartTime >= 30000) {
if (digitalRead(monitor_input) == HIGH) {
digitalWrite(RELAY2, LOW);
digitalWrite(monitor_input_LED, HIGH);
digitalWrite(Relay1_Led, LOW);
delay(1000);
digitalWrite(RELAY2, HIGH);
delay(waitTime); // Wait for 45 mins
relay1Activated = false;
// After the delay, reset the timer and allow the sce1 logic to trigger
relayStartMillis = 0;
} else {
monitor_input_Active = false;
}
}
} else {
monitor_input_Active = false;
}
//for scenrio2
if(isMagMode){
sce2 = (digitalRead(Input1) == LOW && digitalRead(Input2) == LOW);
}
else{
sce2 = (digitalRead(Input1) == HIGH && digitalRead(Input2) == LOW);
}
// Trigger STOP for BOTH scenarios (auto + external)
if (sce2 && !stopHandled) {
stopHandled = true;
digitalWrite(monitor_input_LED, LOW);
delay(2000);
digitalWrite(RELAY2, LOW);
digitalWrite(Relay1_Led, LOW);
delay(1000);
digitalWrite(RELAY2, HIGH);
// Reset internal states
relay1Activated = false;
monitor_input_Active = false;
relayStartMillis = 0;
}
if (!sce2) {
stopHandled = false;
}
delay(500);
}