void loop, using "if" function, unable to run code continuously

Hi all,

I am using void loop to run the code continuously, by showing temperature and pressure on serial monitor for every 5 Seconds. However, I am unable to do so because using the "if" function, once the condition met it does not run for every 5 seconds, despite me adding another loop(), delay(5000) and even use while function etc. It only showed once on serial monitor.

I need to show the temperature and pressure continuously for 5 seconds on OP of serial monitor. Anyone can help with this?

Attaching the code here:

void loop() // run over and over
{ 
  recvData();
  parseNewData();
  
  if (i > 6) {
    Serial.print("The temperature of the vacuum pump is LP Temp(degC): ");
    Serial.println(splitdata[6]);
  }

  if (i > 2) {
    Serial.print("The pressure of the vacuum pump is(mBar): ");
    Serial.println(splitdata[2]);
  }

  //data was received and processed
  if (newData == true) {
    newData = false;
    getPumpStatus();                                 //new pump status request
    delay(5000);                                     //arbitrary delay for requesting periodicaly
    {
        loop();
        delay(5000);
    }
  }
}

thanks in advance!

Hello,
Your code is incomplete, please post all your code.

You are calling loop() within loop, you can't do this. Loop loops, that's why it's called loop.

Never ever call loop() from within loop() unless you know what you're doing. It's called recursion and can even bring PC applications on their knees, not to mention an Arduino with limited memory.

Please post your full code.

If you want to continuously collect data but only every 5 seconds write to the serial console, then you have to set a timer when you write the data but you check the timer continuously to see if 5 seconds has elapsed since the last write operation. Don’t use delay().

. . . 
uint32_t myTimer = 0 ;
. . .
if ( millis() - myTimer > 5000 ) { // 5 seconds 
    myTimer = millis() ;
    // write to Serial 
}