wait for response for x time, send a message by x time, or when response receive

i am hoping to let my arduino wait for an x amount of time, and if x time is up, the mcu will send a message.

or, if the message were to arrive anytime during x time, the mcu will send the message.

is there some method i can use? delay is not going to work here..

Use millis().

Pseudo code:

timestamp = now
Repeat
{
if message received or (millis - timestamp)>interval
{
send message
}
}

timestamp = now
Repeat
{
  if message received or (millis - timestamp)>interval
  {
     send message
  }
}

Yes, like that. I would add a "break;" after sending the message so the Repeat loop doesn't continue forever. :slight_smile:

also have to clear the "message received" flag, and have a way to reset the timestamp.

My android app is going to send a stream of bytes, 2 byte at a time.

The arduino is to receive them 2 byte at a time, and reply "w" when it receives them, telling android to send then next 2.

The problem i got it that sometimes, the stream pauses due to android not receiving a "w", as "w" might be sent during the times the Android might be processing the next byte, and flags in there were reset, ignoring the "w" received.

So I want to let the arduino, whenever the serial buffer is 0, to
1)send a "w"
2)send a "w" again, after 1millisecond, if no response is received.

Is this the right way to do it?

unsigned long Timer;
unsigned long inteval = 1;


while( s0 != '|' || s1 != '|' ){//terminates if '|' is received

			if(s0 == '|' || s1 == '|')break;
			
			else if(HWSERIAL.available() == 0){
                                HWSERIAL.print("w");//tell Android to send next byte

				Timer = millis();
				if(HWSERIAL.available() == 0 && millis() - Timer > 1){
					HWSERIAL.print("w");//tell Android to send next byte
				}
			}
			else if(HWSERIAL.available() >= 2) {
				
				s0=HWSERIAL.read();
				s1=HWSERIAL.read();
                                <some IO operations>
while( s0 != '|' || s1 != '|' ){//terminates if '|' is received

This will only stop the loop when you receive "||", not a single "|" as the comment suggests. So one of the two is wrong and needs to be corrected.

if(HWSERIAL.available() == 0 && millis() - Timer > 1){

You should also seriously look into why the message gets lost, as that shouldn't happen to begin with. I bet that device of yours will have some kind of serial buffer.

wvmarle:

while( s0 != '|' || s1 != '|' ){//terminates if '|' is received

This will only stop the loop when you receive "||", not a single "|" as the comment suggests. So one of the two is wrong and needs to be corrected.

while( s0 != '|' || s1 != '|' ){//terminates if '|' is received

doesnt this mean the loop will run when s0 or s1 are not equal to '|'.

meaning, if either s0 or s1 is equals to '|' , it will stop?

if(HWSERIAL.available() == 0 && millis() - Timer > 1){

You should also seriously look into why the message gets lost, as that shouldn't happen to begin with. I bet that device of yours will have some kind of serial buffer.

the "w" sent to android might've gotten lost while the flags are being set.

Android app: on thread 1: receives "w" sets its flag to true
on thread 2: processes and sends 2bytes to Arduino, set flag to false

Arduino: receives the 2 bytes, replies with "w", wait for next 2 bytes.

on multiple occasions, the stream stops completely. and i cant make the arduino keep sending "w" without some sort of control. the Android app will crash due to too many internal broadcasts(broadcast is sent whenever incoming message).

tzijie:

while( s0 != '|' || s1 != '|' ){//terminates if '|' is received

doesnt this mean the loop will run when s0 or s1 are not equal to '|'.

meaning, if either s0 or s1 is equals to '|' , it will stop?

No - if either s1 or s1 is not equal to '|' the loop runs, it will only stop when both s0 and s1 equal '|'.

If you want it to stop when either equals '|' you have to use the logical and operator - then the loop runs if both are not equal to '|':

while( s0 != '|' && s1 != '|' ){ // terminates when a '|' is received in either s0 or s1

Be aware that now you will lose the information in s0 when s1 equals '|'.