Bit slow...

Hello, I am using an arduino Uno connected to an Sd card . I am taking the amount of free clusters remaining into sd card and also i use the millis function to take the time between each measurement. The problem is that it takes a long period to print to serial monitor the value each time. Is there any way to make it faster?

 uint32_t freeClusters = sd.vol()->freeClusterCount();
       float freeKB = 0.512*freeClusters*sd.vol()->blocksPerCluster();

and also i use the millis function to take the time between each measurement.

Not anywhere in that snippet.

The problem is that it takes a long period to print to serial monitor the value each time. Is there any way to make it faster?

To print what value, at what baud rate?

http://snippets-r-us.com might be able to help you. We need to see all of your code.

In 9600 bauds

#include <SdFat.h>
SdFat sd;
SdFile myFile;
const int chipSelect = 4;
unsigned long time;
void setup() {
  Serial.begin(9600);
  while (!Serial) {}  // wait for Leonardo
  Serial.println("Type any character to start");
  while (Serial.read() <= 0) {}
  delay(400);  // catch Due reset problem
  
  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  // open the file for write at end like the Native SD library
  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
  // if the file opened okay, write to it:
  Serial.print("Writing to test.txt...");
  myFile.println("testing 1, 2, 3.");

  // close the file:
  myFile.close();
  Serial.println("done.");

}

void loop() {
  myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END
  Serial.print("Time: ");
  time = millis();
  Serial.println(time);
  if (myFile) {
   myFile.println(time);
    }  
 uint32_t freeClusters = sd.vol()->freeClusterCount();
 float freeKB = 0.512*freeClusters*sd.vol()->blocksPerCluster();
  Serial.print("Free space: ");
  Serial.print(freeKB);
   myFile.println(freeKB);
    myFile.close(); 
}

The Serial Monitor application, and the Arduino, can send/receive data 8 times faster than you are sending it.

Get out of the stone age. Pick up the speed.

It isn't clear what is taking too long, though. Is it converting the time to a string, and sending that string? Or, is it all the other stuff you are doing between sending timestamps that is taking a long time?

I dont know that's why I am asking! But when you mean increase the speed, is it the baud rate or the clock frequency?

is it the baud rate

Yes. You can't make the clock tick faster.

Serial.begin (115200) ;

And change the baud setting in the serial monitor to 115200 baud.

Call micros() before and after the floating point calculation to get a good measurement of how long things really take. Now change the floating calculation to be integer only and see if it isn't at least 10 times faster. Also, don't include writing to the file or UART in your timings, unless you are measuring them specifically (which you should also do). You'll notice that when writing the UART, it will buffer something like 128 characters before it starts slowing your program down to wait for characters to be sent.

Something like this on the calculation:

unsigned long freeKB = (freeClusters * sd.vol()->blocksPerCluster()) >> 1;  // 1kB is really 1024 bytes anyway, not 1000

You'll notice that when writing the UART, it will buffer something like 128 characters

If you consider 64 something like 128... 8)

PaulS:

You'll notice that when writing the UART, it will buffer something like 128 characters

If you consider 64 something like 128... 8)

Yeah, I didn't go look. I knew there were two sizes and I thought it 128 or 64. It's 64 or 16 depending upon the CPU. My bad.

I changed the serial.begin to 115200 but the speed didn't change writing to the serial monitor.

Call micros() before and after the floating point calculation to get a good measurement of how long things really take. Now change the floating calculation to be integer only and see if it isn't at least 10 times faster

I really don't know how to use micros() function.

I need to write the timing to the file!

I really don't know how to use micros() function.

It takes no arguments. It returns a value. How can you possibly have problems using it?

You have not defined what it is you are trying to accomplish. All that you have said is that it (whatever it is) takes too long.

So, it's time to unwrap the mystery. Just what is "it"?

Haha when I delete this part of the code

uint32_t freeClusters = sd.vol()->freeClusterCount();
       float freeKB = 0.512*freeClusters*sd.vol()->blocksPerCluster();

the timing is displayed normally but when I add this it starts to give less values of time.

the timing is displayed normally but when I add this it starts to give less values of time.

So, you’ve discovered that it takes time to determine how much of the card is in use. I thought everybody knew that.

melis: Haha when I delete this part of the code

uint32_t freeClusters = sd.vol()->freeClusterCount();
       float freeKB = 0.512*freeClusters*sd.vol()->blocksPerCluster();

the timing is displayed normally but when I add this it starts to give less values of time.

Did you try the formula I gave you?

To use micros(), change all occurrences of millis() in your program to micros(). This will give you 1000 times more resolution. Milliseconds are not granular enough to measure this. micros() returns an unsigned long just like millis(). You can subtract the second value from the first to get the total time in microseconds.

If you changed the value in the Serial.begin() call, but it didn't change in the serial monitor, then you didn't successfully upload the new hex file.