delay() not working

Hi everyone, I am trying to running a function which lights up the built in Led of a d1 mini after receiveing a TCP message, in the onData function of the client I'm running this

digitalWrite(LED_BUILTIN,LOW);
Serial.println("on");
 delay(1500);
digitalWrite(LED_BUILTIN,HIGH);
Serial.println("off");

(with d1 mini low means on). It remains of for a short while (like 10ms) and then it goes off, I tried increasing the value and nothing changes, I tried the blinking code with the same delay and it works. Any idea?

Do you know for a fact it’s calling that code?- are the serial prints showing “on” and “off” as expected?

(deleted)

spycatcher2k: See what I changed! :)

Crap, good catch. I've made that mistake a few times, should have noticed it in OP's code.

spycatcher2k: digitalWrite(LED_BUILTIN,LOW); Serial.println("on"); delay(1500); digitalWrite(LED_BUILTIN,HIGH); Serial.println("off"); delay(1500);

See what I changed! :)

But why do I need the second delay? It's not a loop, I need the led to be shut down until next time, does it matter?

Ah, right, now you point that out, you don't- that's for a blink of course.

But if the first delay()'s not working, are the serial prints working ok?

You don't need the second delay, but that answer teaches you to provide context and not just show a snippet. Please show a full example code (or your complete code) that exhibits the behaviour.

I'm not familiar with the D1 mini so probably can't help further.

ardy_guy:
Ah, right, now you point that out, you don’t- that’s for a blink of course.

But if the first delay()'s not working, are the serial prints working ok?

Yes, they are. And I also see the led turn on for a bunch of ms and then off again like my delay doesn’t exist.
But if I try a simple blink code the delay works and i don’t know khy

Yes, they are. And I also see the led turn on for a bunch of ms and then off again like my delay doesn't exist

Is the timing between the serial print statements 1500 ms? Maybe the issue is related to the led. Is the D1 built in led behavior normally inverted or did you modify something?

digitalWrite(LED_BUILTIN,LOW);
Serial.print("on  ");
Serial.println(millis());
 delay(1500);
digitalWrite(LED_BUILTIN,HIGH);
Serial.println("off  ");
Serial.println(millis());

Are you sure LOW for the builtin led is correct btw?

ardy_guy: Are you sure LOW for the builtin led is correct btw?

Yes, 100% sure

cattledog: Is the timing between the serial print statements 1500 ms? Maybe the issue is related to the led. Is the D1 built in led behavior normally inverted or did you modify something?

digitalWrite(LED_BUILTIN,LOW);
Serial.print("on  ");
Serial.println(millis());
 delay(1500);
digitalWrite(LED_BUILTIN,HIGH);
Serial.println("off  ");
Serial.println(millis());

Yes it is 1500 ms, and the d1 built in led normally works inverted. why did you put the line Serial.println(millis()); ?

giacomoporetti: why did you put the line Serial.println(millis()); ?

If I may answer that meantime: to check that they're 1500ms apart.

If the serial print statement are 1500 ms apart, then delay() is "working" and your problem is elsewhere.

But why do I need the second delay? It's not a loop,

You haven't posted any code to show this. My guess without seeing code is that you are calling the snippet again. What did you see when you added the second delay as suggested earlier?

cattledog: If the serial print statement are 1500 ms apart, then delay() is "working" and your problem is elsewhere.

You haven't posted any code to show this. My guess without seeing code is that you are calling the snippet again. What did you see when you added the second delay as suggested earlier?

The point is the two serial print are displayed in the same time, without any delay and adding a final delay doesn't change anything

See the request in reply #6

    client1.onConnect(
            [](void* client, AsyncClient* cli){
                cli->write("1");
                Serial.println("1");

                cli->onData(
                        [](void *number, AsyncClient *client, void *data, size_t len) {
                            if (((char *) data)[0] == '2') {
                                digitalWrite(LED_BUILTIN,LOW);
                                Serial.println("ON");
                                delay(1500);
                                digitalWrite(LED_BUILTIN,HIGH);
                                Serial.println("OFF");
                                delay(1500);

                            }

                            for (unsigned char i = 0; i < len; ++i) {

                                Serial.print(((char *) data)[i]);
                            }

                            Serial.println();
                        },NULL
                );
            },NULL);

Is the code block where delay() is ignored within an interrupt service routine?

cattledog: Is the code block where delay() is ignored within an interrupt service routine?

What do you mean? If you're guessing if the code stops, no is goes on printing the received data

Within an isr, further interrupts are disabled. If the code block is within an interrupt service routine, the delay() will be ignored, but the serial print statements are buffered, and will be printed when the code exits the isr.