Loop until?

Can't seem to get my head around this one.
I've got the following code which is part of the main loop. Every 5 mins I want to read a sensor via the serial port. The sensor however only sends data every 5 seconds. (continuously)

So I need to so another loop until pms.read data is read. So something like this? (Loop until code is incorrect, but how would I accomplish this?)

//-------------------------------------------------------------------------------------------------------------------
  // Every 5 mins read air qualify and send to OpenHab
  //-------------------------------------------------------------------------------------------------------------------
  if (now - lastSampleTime >= fiveMinutes)
  {
    lastSampleTime += fiveMinutes;

    loop until (pms.read(data))
    {
      airquality = data.PM_AE_UG_1_0;
      dtostrf(airquality, 2, 2, tempString);

      Serial.println(tempString);
      client.publish(mqtt_topic_pm1, tempString);

    }
}

Could you post ALL compiler errors ?

Usually, a while loop would be used for something like this. You can, in the parens, use a complex conditional, to keep looping until something happens, like the end of the data is found, or until a certain amount of time has passed.

I think you mean you want to Serial.print every 5 minutes the last value received from the sensor. Assuming this, you want to read every byte received, validate it, and if valid, update your storage for it, then print it once your 5 minute argument is true.

char sensorValue;
unsigned long lastTime = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  If(Serial.available > 0){
    char c = Serial.read();
    if (isDigit(c)){
      sensorValue = c;
    }
  }
  if(millis() - lastTime >= 5000UL){
    Serial.print(sensorValue);
    lastTime = millis();
  }
}

I think you mean you want to Serial.print every 5 minutes the last value received from the sensor. Assuming this, you want to read every byte received, validate it, and if valid, update your storage for it, then print it once your 5 minute argument is true.

Perfect. That's exactly what I meant :slight_smile: Now why didn't I see that.