# 2 Relays operate separately on Time when the switches are on and off

**URL:** <https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491>\
**Category:** Programming\
**Created:** [January 8, 2024, 12:24pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491 "2024-01-08T12:24:10Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![qasim\_liaqat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/qasim_liaqat/32/1031768_2.png) [@qasim\_liaqat](https://forum.arduino.cc/u/qasim_liaqat)\
**Post date:** [January 8, 2024, 12:24pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/1 "2024-01-08T12:24:10Z")

</div>

i want to operate relays on time when i switch on the button it turn the relay and turn off automatically after the given time (the switch state is whatever On or Off it turn off the relay after time) i have no example sketch so please help me.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 8, 2024, 12:38pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/2 "2024-01-08T12:38:34Z")

</div>

> [@qasim\_liaqat](#):
>
> switch on the button

Welcome to the forum.

Generally you don't just get code for asking.

In particular in any case, your description raises questions.

Are you using one switch to control two relays?

Are you using push buttons or toggle switches?

Are you only turning on the relays by acting yourself, however it is, by using the switch, and the relay always goes off only because time has passed?

If the switch is a toggle switch, what happens if you turn the switch off before the time period elapses?

I could guess. That would waste time.

So try again with the description of your problem.

And write a sketch that turns on and off a relay, using any logic at all and maybe even because you press a button. Leave time out if it.

Post that sketch.

What kind of relays do you have? Post a link to where you bought them, or a link to any technical information you may have found or at least a few good picture.

a7

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [January 8, 2024, 12:43pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/3 "2024-01-08T12:43:13Z")

</div>

> [@qasim\_liaqat](#):
>
> i have no example sketch

i think the following demonstrates what you are asking for -- toggle a relay after a delay after a button changes

it shows a timer that is enabled when msecPeriod is set to a non-zero value and sets msec0 as the start of the timer period

this uses a single button and pressing it triggers changing the relay

```arduino
const byte PinLed = LED_BUILTIN;
const byte PinBut = A2;
byte butState;

#define OFF LOW
#define ON HIGH

unsigned long msecPeriod;
unsigned long msec0;

void loop ()
{
    unsigned long msec = millis ();

    // check timer
    if (msecPeriod && msec - msec0 >= msecPeriod) {
        msecPeriod = 0; // disable timer
        digitalWrite (LED_BUILTIN, ! digitalRead (LED_BUILTIN));
    }

    // check for button press
    byte but = digitalRead (PinBut);
    if (butState != but) {
        butState = but;
        delay (20); // debounce
        if (LOW == but) {
            msec0 = msec;
            msecPeriod = 1000;
        }
    }
}

void setup ()
{
    Serial.begin (9600);

    pinMode (PinLed, OUTPUT);
    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
}

```

---

<div class="post-metadata">

**Author:** ![qasim\_liaqat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/qasim_liaqat/32/1031768_2.png) [@qasim\_liaqat](https://forum.arduino.cc/u/qasim_liaqat)\
**Post date:** [January 8, 2024, 12:48pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/4 "2024-01-08T12:48:14Z")

</div>

Thank you so much for reply my English is not good i try to explain actually i am using mit app inverter and using ip address i turn on and off relays in mit i created switch i mean toggle switch when the switch give 1 i want to turn High the relay pin for, you can say 20mint and turn off automatically (not see the switch is aslo off it turn off automatically ) and as it is i want to operate 1 more relay with different button and in the running time i mean when the relay on, i between time the relay switch is off it turn off the relay

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 8, 2024, 12:57pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/5 "2024-01-08T12:57:02Z")

</div>

@qasim_liaqat ,

welcome to the arduino-forum.  
it is very important that you post a detailed description.  
If your english is limited use google-translate.

The grammar will not be brilliant but you will be 3 times better understandable if you have written a detailed description in your native language and google translated it  
than just posting a few words in english that came to your mind.

So please write in your native language and let do google-translate the translation

best regards Stefan

---

<div class="post-metadata">

**Author:** ![qasim\_liaqat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/qasim_liaqat/32/1031768_2.png) [@qasim\_liaqat](https://forum.arduino.cc/u/qasim_liaqat)\
**Post date:** [January 8, 2024, 1:16pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/6 "2024-01-08T13:16:28Z")

</div>

```arduino
#include <WiFi.h>
#include <Wire.h>

IPAddress local_IP(192, 168, 3, 80);
IPAddress gateway(192, 168, 4, 9);
IPAddress subnet(255, 255, 255, 0);

WiFiServer server(80);

#define load1Pin 14
#define load2Pin 33

String value = "-";

bool relayState1;
bool relayState2;

void setup() {
  // put your setup code here, to run once:
  pinMode(load1Pin, OUTPUT);
  pinMode(load2Pin, OUTPUT);

  Serial.print("Setting soft-AP configuration ... ");
  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");

  Serial.print("Setting soft-AP ... ");
  Serial.println(WiFi.softAP("ESP32") ? "Ready" : "Failed!");

  Serial.print("Soft-AP IP address = ");
  Serial.println(WiFi.softAPIP());

  relayState1 = false;
  relayState2 = false;
    
  digitalWrite(load1Pin, !relayState1);
  digitalWrite(load2Pin, !relayState2);

  server.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
        
    // Check if a client has connected.
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data.
  Serial.println("New client.");
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request.
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  if (request.startsWith("msg=13", 5)) {
    // Switch1 is on
  relayState1 = true; 
  digitalWrite(load1Pin, !relayState1);

  }
  if (request.startsWith("msg=14", 5)) {
    // Switch1 is off
  relayState1 = false; 
  digitalWrite(load1Pin, !relayState1);
  }
  if (request.startsWith("msg=15", 5)) {
    // Switch2 is on
  relayState2 = true; 
  digitalWrite(load2Pin, !relayState2);
  }
  if (request.startsWith("msg=16", 5)) {
    // Switch2 is off
  relayState2 = false; 
  digitalWrite(load2Pin, !relayState2);

  }

    if (request.startsWith("x", 15)) {
    // If an 'x' is present at position 15...
    value = request.substring(17); // ... a message starts at position 17.
    value.replace("+", " "); // Replace '+' with spaces.
    value.replace(" HTTP/1.1", " "); // Remove "HTTP/1.1".
  }

  Serial.println(value);

  // Send the response.
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); // Important to add an empty line.
  client.print(value);

  Serial.println("Client disconnected.");

}

```

Put the timer in this code for both relays

---

<div class="post-metadata">

**Author:** ![qasim\_liaqat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/qasim_liaqat/32/1031768_2.png) [@qasim\_liaqat](https://forum.arduino.cc/u/qasim_liaqat)\
**Post date:** [January 8, 2024, 1:17pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/7 "2024-01-08T13:17:47Z")

</div>

Thanks for replay . Help me if you understand my question now plz

---

<div class="post-metadata">

**Author:** ![qasim\_liaqat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/qasim_liaqat/32/1031768_2.png) [@qasim\_liaqat](https://forum.arduino.cc/u/qasim_liaqat)\
**Post date:** [January 8, 2024, 1:20pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/8 "2024-01-08T13:20:08Z")

</div>

If you comfortable plz put this code in my code (i provided in replies)

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 8, 2024, 1:21pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/9 "2024-01-08T13:21:41Z")

</div>

What is your native language?

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [January 8, 2024, 1:30pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/11 "2024-01-08T13:30:46Z")

</div>

> [@qasim\_liaqat](#):
>
> Put the timer in this code for both relays

doing it the way you structured your code, there would be 2 variables each for msec0 and msecPeriod and there would be 2 blocks of code for the 2 timers. this makes the program long an more difficult to maintain

i suggest you first modify your code to use arrays for the relay pins, their states and use separate functions to change the state of a relay using a array index and state called from your code recognizing the different msgs. for example

```arduino
    if (request.startsWith("msg=16", 5))
        setRelay (1, false);

```

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 8, 2024, 1:32pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/12 "2024-01-08T13:32:15Z")

</div>

> **اردو پڑھنے کے لیے مثلث پر کلک کریں۔ click the triangle to read urdu**
>
> میں نے یہ متن جرمن زبان میں آپ کو یہ دکھانے کے لیے لکھا ہے کہ گوگل ترجمہ کتنا اچھا کام کرتا ہے۔
> 
> ایک استثناء کے طور پر، میں آپ کو اردو میں لکھوں گا۔ براہ کرم ہمیشہ انگریزی میں پوسٹ کریں۔ یہ فورم اس طرح کام نہیں کرتا کہ آپ بہت مختصر سوال پوچھیں اور پھر جواب کے طور پر مکمل کوڈ حاصل کریں۔
> 
> آپ کی مختصر پوسٹنگ سے یہ تاثر ملتا ہے کہ آپ بہت خود غرض ہیں!  
> کیا آپ یہ تاثر دینا چاہتے ہیں کہ آپ خود غرض اور خود غرض ہیں؟  
> شاید نہیں۔  
> اگر آپ صرف ایسے ہی مختصر سوالات پوچھیں گے تو آپ کو صرف مختصر جوابات جوابات کے طور پر ملیں گے۔ یا لنکس جہاں آپ کچھ پڑھ سکتے ہیں۔
> 
> اگر آپ اپنے پروجیکٹ کو بہت سے الفاظ میں بیان کرنے کی کوشش کرتے ہیں، تو آپ کو تفصیلی جوابات ملیں گے۔ ورنہ نہیں۔
> 
> آپ گوگل کے ذریعہ پوری ویب سائٹس کا اردو میں ترجمہ بھی کروا سکتے ہیں۔

Same text in english  
I wrote this text in German to show you how well Google translation works.

As an exception, I will write to you in Urdu. Please ALWAYS post in English. This forum does NOT work in such a way that you ask a very short question and then get finished code as an answer.

Your short postings give the impression that you are very selfish!  
Do you want to give the impression that you are selfish and selfish?  
Probably not.  
If you only ask such short questions, you will only get short answers as answers. Or links where you can read something.

If you make an effort to describe your project precisely in many words, you will get detailed answers. Otherwise not.

You can also have entire websites translated into Urdu by Google.

best regards Stefan

---

<div class="post-metadata">

**Author:** ![qasim\_liaqat](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/qasim_liaqat/32/1031768_2.png) [@qasim\_liaqat](https://forum.arduino.cc/u/qasim_liaqat)\
**Post date:** [January 8, 2024, 1:43pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/13 "2024-01-08T13:43:55Z")

</div>

Thanks for the reply. I think I tried my best to tell the problem , But I think you don't understand batter , it's not my fault, so if you want to help, you can do, if you don't, you can go your way, thanks.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 8, 2024, 1:48pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/14 "2024-01-08T13:48:24Z")

</div>

I am very sure that using urdu and google translate you would be able explain with more words.  
Anyway if you don't like to. My estimation is: It will take some etxra time to finish your project if you go on in this posting style. Good luck with your project.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 8, 2024, 2:56pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/15 "2024-01-08T14:56:19Z")

</div>

> [@StefanL38](#):
>
> My estimation is:

Haha, yes. Except soon the code will be entirely written for the OP, which of course she will then study and learn a great deal from.

a7

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 6, 2024, 2:57pm UTC](https://forum.arduino.cc/t/2-relays-operate-separately-on-time-when-the-switches-are-on-and-off/1208491/16 "2024-07-06T14:57:09Z")

</div>

Closed on July 6, 2024, 2:57pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
