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

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.

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

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

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);
}

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

@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

#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

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

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

What is your native language?

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

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

اردو پڑھنے کے لیے مثلث پر کلک کریں۔ 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

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.

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.

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

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