sending different single characters in a row but with a delay

*i searched the forums + google but no answer

first we have this if condition , if true it will call 2 functions

 if(l_minute >kkk)
       {
        
        Wire.onRequest(dart);
  
        Wire.onRequest(mart);
        digitalWrite(qq,HIGH);
}

then we have those 2 functions being called

void dart() {
  
 Wire.write("H");
 Serial.println(" SENT H");
 }

 

void mart() {
 Wire.write("N");
 Serial.println(" SENT N");

}

the issue is that ONLY "N" will be sent and H will never be sent , i tried to add

delay();

and for loops but it will always send "N"
what i need it to do is to send "H" first then after small delay i need it to send "H"
if i do this :

void dart() {
  
 Wire.write("H");
 Serial.println(" SENT H");
 Wire.write("N");
 Serial.println(" SENT n");
 }

it will only send "N"
however if i do

void dart() {
  
 Wire.write("H");
 Serial.println(" SENT H");
delay(500);
 Wire.write("N");
 Serial.println(" SENT N");
 }

the program will crash and get no response

Don't post snippets (Snippets R Us!)

Wire.onRequest() registers a function (handler) to be called when a master requests data from this slave device.

So when you do this

Wire.onRequest(dart);
Wire.onRequest(mart);

the second call overwrites the first handler dart as you can only have one handler. that's why only mart() is called.

the handler is called in the context of an interrupt, so you should not do anything lengthy or requiring interruptions in their. Also I think the write() are actually stored into a buffer and sent in one go once your handler is done.

what i need it to do is to send "H" first then after small delay i need it to send "H"

what's the exact intent there? what are you trying to achieve?

am trying to send letters to an esp8266 and the esp will send wep requests based on that letter ,
i fixed it somehow and it works but am not sure if my fix is efficient
here is what i did:

if(l_minute >value1)
       {
        if(l_minute >kkk)
        {
          Wire.onRequest(shart);

        }
        else
        {
        
        Wire.onRequest(dart);
  
        
        digitalWrite(qq,HIGH);
        delay(500);
        }

i just nested the if condition to trigger on function at a time

then here is the functions

void dart() {
  
 Wire.write("H");
 Serial.println(" SENT H");
 
 }

 

void mart() {
 
 Wire.write("N");
 Serial.println(" SENT N");

}
void shart() {
 
 Wire.write("Q");
 Serial.println(" SENT Q");

}

N letter will be sent when there is nothing to be sent . the letter N will overwrite the previous letter i have sent to esp8266
when the esp receives "H" immediately the "H" will be replaced by "N " so it wont loop the function on esp thus not making multiple web requests

Why are you trying to delay sending values? I can't think of any good reason you will need to do this. You probably have a fundamental misunderstanding of how I2C works.

A slave device fundamentally cannot send data the master on its own initiative. The master controls the clock after all. The most you can do is have a separate digital pin connected to the master that turns on when information is ready for the master to retrieve. Then you can only wait for the master to request that information. That means the master device needs to monitor that signal, and send a request when it sees it on.

NEVER use Serial prints or delays in an ISR. Those functions require interrupts to be on to work, and interrupt are disabled when an ISR is running.

NEVER use Serial prints or delays in an ISR. Those functions require interrupts to be on to work, and interrupt are disabled when an ISR is running.

the advice is good

just a point: HardwareSerial got modified to be able to handle the case when interrupts are disabled though so it won’t fail