Can someone shed some light on a query i have?
My interpretation of the documentation seems to suggest that
system.flush()
will pause following commands executing on a Arduino until all serial data has been transmitted.
Would that then mean that
system.print or system.println
operates in a asynchronous manner
i.e you call it, this copy's data to a send buffer and immediately starts executing other commands?
The reason i ask is that i'm writing a data-logger for Arduino that accepts inputs on the ADC pins and then outputs this to Java. Whilst i have a working prototype I am recording this data in a wave file.
To build the wave file i have to define the number of frames upfront (frames are derived from SMPS & duration).
Currently my sampling rate is 40 SMPS +- 2. Just about ok for Siesmometer work.
If possible i would like to get accurate timings of each time i call the serial command in order to get 40SMPS +- 0.
I have tried to measure the millis before and after each Serial transmission in order to work out a dynamic adjustment to pause the main loop the correct number of millis to ensure it triggers every 1/40 of a second however over 30 mins this is drifting by 20 - 30 seconds.
I may scrap txing data every loop of the program in favour of tx'ing a whole buffer similar to Girino if it seems that i cant get more accurate timings.
Code below for Arduino please note that the delay time was calibrated by measuring the time it took to execute the loop, i noted sometimes the serial transmission would take +- 1ms.
bool hello = false;
int inputA = 0;
int inputB = 0;
int inputC = 0;
int delayTime =0;
unsigned long millis1 = 0;
unsigned long millis2 = 0;
void setup() {
Serial.begin( 115200 ); // start the serial port
}
void loop() {
// put your main code here, to run repeatedly:
if(hello ==false){
Serial.print("HELLO:");
}
String input = Serial.readStringUntil(':');
if(hello==true){
Serial.print("I:");
}
if(input == "HELLO"){
hello = true;
}
if(input == "RESET"){
hello = false;
}
if(input == "STREAM"){
while(true){
millis1 = millis();
inputA = 0;
inputB = 0;
inputC = 0;
inputA = analogRead(1);
inputB = analogRead(2);
inputC = analogRead(3);
char buffer[9]= "";
Serial.print("DATA");
Serial.print(",");
sprintf(buffer,"A%04d",inputA);
Serial.print(buffer);
Serial.print(",");
sprintf(buffer,"B%04d",inputB);
Serial.print(buffer);
Serial.print(",");
sprintf(buffer,"C%04d",inputC);
Serial.print(buffer);
Serial.print(":");
millis2 = millis();
// Serial.print("Time to complete:");
delayTime = 25 - (millis2 - millis1);
// Serial.println(delayTime);
delay(delayTime);
}
}
}