How to limit serial data send?. Please help

Hi guys, I want to send data through Serial channel. But my data is sending as a rate faster than I need it. How can I limit it to like 4 times / second. I’m thinking about using timer for sending function. Code included. Any help is appreciated

Edit.ino (2.09 KB)

#include <MsTimer2.h>
#include <LIDARLite.h>
#include <AccelStepper.h>
#include <Servo.h>
#include <Wire.h>

#define HALFSTEP 8
#define FULLSTEP 4
#define blue 9
#define pink 10
#define yellow 11
#define orange 12




int distance;
int angleScan = 0;
int angleRotate = 0;
int targetPosition = 4096; // 4096 for 1 full circle
int roundCount=0;
bool clockwise = true;

AccelStepper stepper(8, 2, 4, 3, 5);  
Servo servo;
LIDARLite myLidarLite;

void setup()
{  
//   MsTimer2::set(500, printOutput); // 250ms period
//   MsTimer2::start();
   Serial.begin(115200);
   servo.attach(8);
   stepper.setMaxSpeed(1000);
   myLidarLite.begin();
   
   //Moving to initial position
   stepper.moveTo(targetPosition);
   stepper.setSpeed(650);
   servo.write(angleScan);
}

void loop()
{ 
  if (angleScan <=90)
  {
    angleRotate=stepper.currentPosition()*360/4096;
    myLidarLite.beginContinuous(false,0x13,0xff,0x62);  //100Hz, infinite reading\
    servo.write(angleScan);
    distance = myLidarLite.distanceContinuous();
    if(distance>1300)
      {
      distance-=1300;
      }
      printOutput(angleRotate,angleScan,distance); 
        if(stepper.distanceToGo()==0)
        {
          roundCount++;
          stepper.setCurrentPosition(0);
          stepper.moveTo(targetPosition);
          stepper.setSpeed(650);
          if (roundCount=1) // when system revolve 1 times, elevate the Lidar by 5
          {
            roundCount=0;
            angleScan = (angleScan + 5);
            servo.write(angleScan);
          }
        }
  stepper.runSpeed();
  }
}

void printOutput(int angleRotate, int angleScan, int distance)
{
  Serial.print("T");
  if (angleRotate < 10)
    Serial.print("00");
  else if (angleRotate < 100)
    Serial.print("0");
  Serial.print(angleRotate);
  
  Serial.print("P");
  if (angleScan < 10)
    Serial.print("00");
  else if (angleScan < 100)
    Serial.print("0");
  Serial.print(angleScan);
  
  Serial.print("V");
  if (distance < 10)
    Serial.print("000");
  else if (distance < 100)
    Serial.print("00");
  else if (distance < 1000)
    Serial.print("0");
  Serial.println(distance);
  }

The following will allow the printing only every 250 milliseconds, no matter how often the printOutput function is called. Is that what you meant?

void printOutput(int angleRotate, int angleScan, int distance)
{
  static unsigned long timer = 0;
  unsigned long interval = 250;
  if (millis() - timer > interval)  // has a quarter second passed since last print?
  {
    timer = millis();
    Serial.print("T");
    if (angleRotate < 10)
      Serial.print("00");
    else if (angleRotate < 100)
      Serial.print("0");
    Serial.print(angleRotate);

    Serial.print("P");
    if (angleScan < 10)
      Serial.print("00");
    else if (angleScan < 100)
      Serial.print("0");
    Serial.print(angleScan);

    Serial.print("V");
    if (distance < 10)
      Serial.print("000");
    else if (distance < 100)
      Serial.print("00");
    else if (distance < 1000)
      Serial.print("0");
    Serial.println(distance);
  }
}

Yes, thank you both ^^. I will check if it worked