replace delay(); with millis ?

Hi everyone,
It is possible to use millis instead of delay function when you have an SD Card web server using ajax?
Or this is stupid?

My relays are only impulse relays that need to be on for 0.1 or 10 seconds, but when I press them quickly the arduino will freeze.

Can anyone will help me please with an example code, I cant figure out for days now...
Thank you very much!

    // RelayImpulse9 (pin 31)
    if (StrContains(HTTP_req, "RelayImpulse9=1")) {
        digitalWrite(31, LOW);
        delay(100);
        digitalWrite(31, HIGH); 
    }
    else if (StrContains(HTTP_req, "RelayImpulse9=0")) {
        digitalWrite(31, LOW);
        delay(100);
        digitalWrite(31, HIGH); 
    }

    
    // RelayImpulse10 (pin 32)
    if (StrContains(HTTP_req, "RelayImpulse10=1")) {
        digitalWrite(32, HIGH);
        delay(300);
        digitalWrite(32, LOW); 
    }
    else if (StrContains(HTTP_req, "RelayImpulse10=0")) {
        digitalWrite(32, HIGH);
        delay(300);
        digitalWrite(32, LOW); 
    }

    
    // RelayImpulse11 (pin 33)
    if (StrContains(HTTP_req, "RelayImpulse11=1")) {
        digitalWrite(33, LOW);
        delay(10000);
        digitalWrite(33, HIGH); 
    }
    else if (StrContains(HTTP_req, "RelayImpulse11=0")) {
        digitalWrite(33, LOW);
        delay(10000);
        digitalWrite(33, HIGH); 
    }

It is possible to use millis instead of delay function when you have an SD Card web server using ajax?

Of course it is. That the Arduino is, or is not, acting as a web server is completely irrelevant.

but when I press them quickly the arduino will freeze.

What do you mean? Why are YOU pressing the relays? Keep your fingers off the relays.

ALL of your code; not just the bits YOU think are relevant.

Separate out the command-parsing from the output functions. Study this loop function below and try to incorporate its ideas into your sketch...

void loop() {
  readInputs();
  doCalculations();
  setOutputs();
}

When you get an input command, set a variable so you know that the command was received but not yet carried out. When you carry out the first part of the command, change the variable so you know that the first part was done. You would often need to know when it was done, because the next part of the command has to happen 100 milliseconds later.

The demo Several Things at a Time illustrates the use of millis() to manage timing. It may help with understanding the technique. The program style is like what @MorganS recommends.

...R

My relays are only impulse relays that need to be on for 0.1 or 10 seconds, but when I press them quickly the arduino will freeze.

How are you driving these relays?

Thank you all for your help.
For me, because I dont know at all C+ seems very difficult.

I will try to follow MorganS sugestions if I found on web. but an example to set a second to an relay will be more usefull to me.

Nick, I dont realy understand your question. I switch the relays from webpage and when I press many of them without wait my arduino will freeze and after 1 min will "unlock", but I receive only a part of commands. I mean only half of them will understand the command.

sorry for my english. thank you again!

I switch the relays from webpage and when I press many of them without wait my arduino will freeze and after 1 min will "unlock", but I receive only a part of commands. I mean only half of them will understand the command.

You need to show ALL of your code. Each time you do something on the web page, a GET request is made. Each GET request is a completely separate event, from a completely separate client. Each takes time to process.

The Arduino's ethernet shield can only handle 4 concurrent client requests. The Arduino can only process one of them at a time.

This is my code, if anyone willing to help me really appreciate, because I cannot get it to work. Thank you!

relay_start_stop_0.1sec.ino (9.36 KB)

That code doesn't seem too large. You could probably have posted it inside [ code ] tags.

First, it seems that you've created your own functions for finding strings and clearing memory. It's much more efficient to use the standard string library. http://en.cppreference.com/w/c/string/byte Even though the library has a huge number of functions, the linker only includes the functions you actually used, so it is usually smaller and faster than writing your own.

Second, it looks like the only function you are doing with the relays is pulsing any relay on for 100 ms. So when you turn a relay on, set a timer. In the main loop, while you're waiting for another client request, check the timer. If it's older than 100ms then turn off all the relays. You don't even need to know which one was turned on - it takes a microsecond to turn them all off.

robertaccess:
Nick, I dont realy understand your question. I switch the relays from webpage and when I press many of them without wait my arduino will freeze

What sort of relays are they exactly? (link)

Are you using transistors or similar to operate the relay?

@ nick
Those ones:

I control them directly without tranzistors

@MorganS
I will try that!