Serial.available();

Hello.
I have one oled display for my project and I want to display connection status.
When arduino gets a serial port signal,I want to print something like “online” and if it hasn’t receive data for a while to print “offline”

void loop() {
 
  if (Serial.available() > 0) {
    wait = 0;
  }
  
  if (wait <= 810) {
    wait++;
  }
  
  if (wait >= 800) {
    disp.clearScreen();
    disp.drawStr(2, 2, "offline");
  } 
  else if (wait < 800) {
    disp.clearScreen();
    disp.drawStr(2, 2, "online");      
  } 

}

The thing is that when I get “online” screen after 1 sec I get “offline” screen and etc. It’s a loop

The serial writes bytes to arduino every 2 seconds.
The variable “wait” to climb to 810 (in if loop) needs 4 seconds.

Thanks

Firstly you should be using millis() to find out how much time has elapsed, not loop counting - you’ll then be consistent.

Secondly you need to be aware of states. Your code will have a number of states it is in. Such as “waiting for a serial
character”, “failed to get a serial character in a given time”, “seen a serial character recently”.

You then need to figure out the logic for moving between states. Google state transition diagram or similar - its not
hard. Once you know what events cause transitions between states then you code them all up in loop().

Loop keeps looping and checking for transitions between states (such as a character being available, or a certain
amount of time has elapsed (if in a waiting state)).

Often a good structure is to use switch-case construct - switch on the current state, and each case then looks for
the conditions that cause that state to change, and change it (doing whatever needs to be done at that transition,
which can be a separate routine you call - this keeps the code readable and concise).

Thanks for your help.

If I use millis() I won't be able to reset the counter when arduino finds serial data:(

I was thinking something like when arduino finds serial data to hold this state for 2-3 seconds and then searching for data again. This can't be done with delay(); unfortunately because this way will decrease the speed and the stability of the rest of the program :(

Jek90: If I use millis() I won't be able to reset the counter when arduino finds serial data:(

When you're keeping up with the many things you have to do during the day, how many times have you had to reset your watch so you'd know how long it had been? Never. Just note the time when something happens (with a call to millis() and save the result) and then use subtraction to find out how long it has been since that time (again with a call to millis()).

  if (wait >= 800) {
    disp.clearScreen();
    disp.drawStr(2, 2, "offline");
  } 
  else if (wait < 800) {

If the first statement is not true, is there any possible way the else will not be true?

The second if statement is not needed.

By the way, your whole premise of online/offline is incorrect. All that reading serial.available() will tell you is that the device at the other end of the serial port has, or has not, sent (or been able to send) data.