The output is coming slowly to serial monitor

hello guys. i have this part of my code below which causing slow output on serial monitor. i thought it was because of delay function so i removed it out but it is still the same. i am thinking maybe it is beacuse of while. i know it should be some unblocking structure but i couldn't find what i can put instead. i used if but it kept writing the 1 command and never changed. what can i do? i am not sharing rest of the code because i think it is irrelevant but i if you wish i can. thank you.

unsigned long start = millis();

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)1);
          //delay(20);
        }

        start = millis();

        while (millis() - start < 700){
          motorControlChar.writeValue((byte)3);
          //delay(20);
        }

        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)1);
          //delay(20);
        }

        start = millis();

        while (millis() - start < 700){
          motorControlChar.writeValue((byte)4);
          //delay(20);
        }

        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)1);
          //delay(20);
        }

        start = millis();

        while (millis() - start < 700){
          motorControlChar.writeValue((byte)3);
          //delay(20);
        }
 
        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)2);
          //delay(20);
        }

        start = millis();

        while (millis() - start < 700){
          motorControlChar.writeValue((byte)3);
          //delay(20);
        }
 
        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)2);
          //delay(20);
        }

        start = millis(); 

        while (millis() - start < 700){
          motorControlChar.writeValue((byte)4);
          //delay(20);
        }

        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)0);
          //delay(20);
        }

Your question is about slow output on serial monitor. Nothing in the code you posted outputs anything to serial monitor. So the code you posted is irrelevant to your question. Only the complete code would be relevant. Please read the forum guide in the sticky post before you post again.

        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)2);
          //delay(20);
        }

Your while loops will take the same time to execute with or without the delay() function. Without the delay() function, data will be written to whatever motorControlChar is more frequently.

3 Likes

It appears that you do not fully understand what you are doing. You have chosen not to provide the necessary information to answer your question, leaving us to guess the location of the print statements.

2 Likes

The code that you provided is blocking code. while (millis() - start < 2000) will block any other processing of code for two seconds. Next 700ms, next 2 seconds again and so on.

There are a number of topics on the forum that will help you to solve the problem; except the first one all come from the tutorial section of the forum.

1 Like

I didn't share the rest because the slowdown happened after i add this part of the code. but if it will be helpful, here is the whole code. thank you.

#include <ArduinoBLE.h>

//PASTEBEGIN
#include <Arduino_LSM9DS1.h>
//PASTEFINISH

// ------------------------------------------ BLE UUIDs ------------------------------------------
#define BLE_UUID_PERIPHERAL               "19B10000-E8F2-537E-4F6C-D104768A1214"  //please change to a unique value that matches BLE_IMU_PERIPHERAL
#define BLE_UUID_CHARACT_LED              "19B10001-E8F2-537E-4F6C-E104768A1214"  //please change to a unique value that matches BLE_IMU_PERIPHERAL
#define BLE_UUID_CHARACT_GYROX             "29B10001-E8F2-537E-4F6C-a204768A1215"  //please change to a unique value that matches BLE_IMU_PERIPHERAL
#define BLE_UUID_CHARACT_GYROY             "39B10001-E8F2-537E-4F6C-a204768A1215"  //please change to a unique value that matches BLE_IMU_PERIPHERAL
#define BLE_UUID_CHARACT_GYROZ             "49B10001-E8F2-537E-4F6C-a204768A1215"  //please change to a unique value that matches BLE_IMU_PERIPHERAL

//PASTEBEGIN
#define BLE_UUID_CHARACT_ACCA             "59B10001-E8F2-537E-4F6C-a204768A1215" //please chnage to a unique value that matches BLE_IMU_CENTRAL
#define BLE_UUID_CHARACT_ACCB             "69B10001-E8F2-537E-4F6C-a204768A1215" //please chnage to a unique value that matches BLE_IMU_CENTRAL
#define BLE_UUID_CHARACT_ACCC             "79B10001-E8F2-537E-4F6C-a204768A1215" //please chnage to a unique value that matches BLE_IMU_CENTRAL

#define BLE_UUID_CHARACT_MAGK             "89B10001-E8F2-537E-4F6C-a204768A1215" //please chnage to a unique value that matches BLE_IMU_CENTRAL
#define BLE_UUID_CHARACT_MAGL             "99B10001-E8F2-537E-4F6C-a204768A1215" //please chnage to a unique value that matches BLE_IMU_CENTRAL
#define BLE_UUID_CHARACT_MAGM             "09B10001-E8F2-537E-4F6C-a204768A1215" //please chnage to a unique value that matches BLE_IMU_CENTRAL
//PASTEFINISH

//begin
BLEService motorService("19B10000-E8F2-537E-4F6C-E104768A1214");
BLEByteCharacteristic motorControlChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
//end

// ------------------------------------------ VOID SETUP ------------------------------------------
void setup() {
  Serial.begin(9600);
  while (!Serial);

  // configure the button pin as input
  pinMode(LED_BUILTIN, OUTPUT);

  // initialize the BLE hardware
  BLE.begin();

  Serial.println("BLE Central - Gyroscope control");

  // start scanning for peripherals
  BLE.scanForUuid(BLE_UUID_PERIPHERAL);//
}

// ------------------------------------------ VOID LOOP ------------------------------------------
void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();

    if (peripheral.localName() != "BLE_IMU") {
      return;
    }

    // stop scanning
    BLE.stopScan();

    LED_IMU(peripheral);
    // peripheral disconnected, start scanning again
    BLE.scanForUuid(BLE_UUID_PERIPHERAL);

  }

}

// ------------------------------------------ FUNCTIONS ------------------------------------------
void LED_IMU(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

  // retrieve the LED characteristic
  BLECharacteristic ledCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_LED);
  BLECharacteristic gyroXCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_GYROX);
  BLECharacteristic gyroYCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_GYROY);
  BLECharacteristic gyroZCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_GYROZ);

  //PASTEBEGIN
  BLECharacteristic accACharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_ACCA);
  BLECharacteristic accBCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_ACCB);
  BLECharacteristic accCCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_ACCC);
  
  BLECharacteristic magKCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_MAGK);
  BLECharacteristic magLCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_MAGL);
  BLECharacteristic magMCharacteristic = peripheral.characteristic(BLE_UUID_CHARACT_MAGM);
  //PASTEFINISH

  //begin
  BLECharacteristic motorControlChar = peripheral.characteristic("19B10001-E8F2-537E-4F6C-D104768A1214");
  //end

  // check if an specific BLE characteristic exists
  if (!ledCharacteristic) {
    Serial.println("Peripheral does not have LED characteristic!");
    peripheral.disconnect();
    return;
  } else if (!ledCharacteristic.canWrite()) {
    Serial.println("Peripheral does not have a writable LED characteristic!");
    peripheral.disconnect();
    return;
  }

  //begin
  if (!motorControlChar) {
    Serial.println("Peripheral does not have motor control characteristic!");
    return;
  }
  //end

  int buttonState = 0;
  float x, y, z, a, b, c, k, l, m;
   
  while (peripheral.connected()) {

    // while the peripheral is connected
    // read the gyroscope values
    gyroXCharacteristic.readValue( &x, 4 );
    gyroYCharacteristic.readValue( &y, 4 );
    gyroZCharacteristic.readValue( &z, 4 );

    //PASTEBEGIN
    accACharacteristic.readValue( &a, 4 );
    accBCharacteristic.readValue( &b, 4 );
    accCCharacteristic.readValue( &c, 4 );

    magKCharacteristic.readValue( &k, 4 );
    magLCharacteristic.readValue( &l, 4 );
    magMCharacteristic.readValue( &m, 4 );
    //PASTEFINISH

    Serial.print(millis());
    Serial.print('\t');
    Serial.print(x);
    Serial.print('\t');
    Serial.print(y);
    Serial.print('\t');       
    Serial.print(z);
    Serial.print('\t');
    //PASTEBEGIN
    Serial.print(a);
    Serial.print('\t');
    Serial.print(b);
    Serial.print('\t');       
    Serial.print(c);
    Serial.print('\t');

    Serial.print(k);
    Serial.print('\t');
    Serial.print(l);
    Serial.print('\t');       
    Serial.println(m);
    //PASTEFINISH

    //begin
    //end
    
    //begin
  
      unsigned long start = millis();

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)1);
          //delay(20);
        }

        start = millis();

        while (millis() - start < 700){
          motorControlChar.writeValue((byte)3);
          //delay(20);
        }

        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)1);
          //delay(20);
        }

        start = millis();

        while (millis() - start < 700){
          motorControlChar.writeValue((byte)4);
          //delay(20);
        }

        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)1);
          //delay(20);
        }

        start = millis();

        while (millis() - start < 700){
          motorControlChar.writeValue((byte)3);
          //delay(20);
        }
 
        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)2);
          //delay(20);
        }

        start = millis();

        while (millis() - start < 700){
          motorControlChar.writeValue((byte)3);
          //delay(20);
        }
 
        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)2);
          //delay(20);
        }

        start = millis(); 

        while (millis() - start < 700){
          motorControlChar.writeValue((byte)4);
          //delay(20);
        }

        start = millis(); 

        while (millis() - start < 2000){
          motorControlChar.writeValue((byte)0);
          //delay(20);
        }
    //end
    

    // make the LED blink
    if (buttonState == 0)
    {buttonState = 1;}
    else if (buttonState == 1)
    {buttonState = 0;}
    
    digitalWrite(LED_BUILTIN, buttonState);
      if (buttonState == 0) {
        // write 0x01 to turn the LED on
        ledCharacteristic.writeValue((byte)0x01);
      } else {
        // write 0x00 to turn the LED off
        ledCharacteristic.writeValue((byte)0x00);
      }
  }
  Serial.println("Peripheral disconnected");
}

1 Like

As was mentioned above, your code from the start of the thread is a blocking code. Even though you are using millis(), you are doing it wrong. Your code from the beginning of the topic contains a six while loops with a timeout of 2000 milliseconds and a five with a timeout of 700 mS of each. Accordingly, this code is equivalent to inserting a delay of 15.5 seconds. I assume that this is the delay you see when outputting to a serial.

1 Like

Thanks for posting the sketch.

Serial 9600, you can use higher speed, 115200 shouldn't be a problem.

1 Like

yes, that's true. the delay is about 16 seconds. how can i rearrange this part of code? thank you.

Post #4 ?

Well, there's one thing you could easily improve. 9600 baud was cutting edge back in the 1980's. I generally use 115200.

EDIT: @ledsyn already mentioned this

At mentioned by others, you need to implement a state-machine.

The reason is, your Arduino has, as far as we know, one CPU core and no operating system to support multiple processes running simultaneously on a single core. So if you write "blocking code", nothing can be written to serial monitor until the blocking code has finished executing.

Blocking code is what beginners write. Like drawing with wax crayons or riding a bicycle with stabilisers ("training wheels"). It's time to level-up your coding skills. The forum will be glad to help you get there!

Some crayon art:

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.