With Arduino Uno Rev3, I am trying to maintain a delicate timing while handling data transmission. I want to send 6 bytes at a time, which takes around ~44 us when I time Serial.write(). The time it takes from Arduino Serial to computer is irrelevant in my case. I only mind the run time of the function.
My idea is sending not all 6 bytes but a few at a time, so that the total sending time overhead would be distributed between time-sensitive tasks. Note that I will never be exceeding the buffer size, so the Serial.write() will never wait for actually transmission to be completed.
const uint8_t MSG_LEN = 6;
uint8_t snd_buffer[MSG_LEN];
uint32_t us_now = 0L;
uint8_t snd_byte_cnt = MSG_LEN;
uint8_t dummy_var = 0;
void setup() {
Serial.begin(115200);
delay(1000);
}
void loop() {
us_now = micros();
operation_dummy();
com_dummy();
//the aim is to use no delay functions
}
void com_dummy(){ //ex communication
if (snd_byte_cnt < MSG_LEN){ //bytes need to be sent
Serial.write(snd_buffer[snd_byte_cnt]); //send the target byte
snd_byte_cnt++; //move to the next byte ind
} //else nothing to be sent
}
void operation_dummy() { //ex operation
dummy_var++; //do something (time sensitive in reality)
if (snd_byte_cnt == MSG_LEN){ //if no data pending to be sent
* (uint32_t *) &snd_buffer[1] = us_now; //modify buffer between 1st and 5th bytes (both inclusive)
snd_buffer[MSG_LEN - 1] = dummy_var;
snd_byte_cnt = 0; //trigger start sending
} //else wait for next cycle to send
}
My question is, how many bytes should be used with Serial.write() at a time?
I've heard the Serial library processes 16 bits (2 bytes) at a time. What happens if I try to send a single byte? Does it wait a certain time for the second byte and if doesn't arrive then it pushes a single byte? (Edit: this turned out to be a misunderstanding and only the FIFO pointer is 16 bits, not the data to be sent. * Source: https://forum.arduino.cc/t/serial-write-time/233360/23 )*
Would never calling a sleep function and using micros() OR the baudrate affect my time keeping? I think micros() and actually transmission uses interrupts. So different baudrates may delay micros() calls differently since both need to disable interrupts.