I have my Arduino collecting data and transmit via SPI wire using USB to my desktop computer. But I will like each point of data collects the time taken. But I will liek this time clock to be synchronized with my Desktop client . I know how to send the data via Serial port to the Arduino, but I not sure how to do the synchronization .
I will like my desktop client computer send a time to synchronized every x minutes or hours. Any idea hot to do this?
I have my Arduino collecting data and transmit via SPI wire using USB to my desktop computer.
You have to describe that system in more detail. You send the data from the Arduino to some other device by SPI and that other device sends it to the desktop computer by USB. Please provide a wiring diagram for that setup.
I know how to send the data via Serial port to the Arduino, but I not sure how to do the synchronization .
If a synchronization accuracy of about 1 second is sufficient you can simply send the time to the Arduino from time to time and let it count up the seconds until the next time stamp is received. A synchronization to the millisecond is much harder and involves realtime capabilities on the desktop computer too, so Windows is ruled out then.
What I am not sure how to read the time send to the Arduino. I can successfully send a time from the Mathematica Software via the USB serial but what I am not sure how this time is set in the Arduino. How to i set the Arduino sketch to read a serial monitor received tiem data.
What I am not sure how to read the time send to the Arduino. I can successfully send a time from the Mathematica Software via the USB serial but what I am not sure how this time is set in the Arduino.
Do your pc send only time data? or other data combined?
If pc send only time data, all the arduino board have to do is just read time data from serial buffer.
If pc send time data with other data, arduino should distinguish what part of data is time data after receiving data from serial
Thank yo for your help.. My computer software is capable to write to the buffer. But not sure what command Arduino reads this received data and how to monitor. I have the limitation that once the PC is connected to the Arduino and captures the Serial COM port, I am no longer able to use the IDE serial monitor.
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
unsigned char inByte;
// If data were given from PC, read received bytes(in buffer)
// Then echo back bytes to PC
if(Serial.available()) {
inByte=Serial.read();
Serial.write(inByte);
}
}
This code is the simplest form. There are many useful functions related to serial communication.
For more information, see here Serial - Arduino Reference
Anyway, you should note that if Mathmetica software is communicating with Arduino, you can't monitor by serial monitor in Arduino IDE because COM port can be accessed by one software at a time. If you want to monitor data sent from Arduino, you should monitor it through Mathmetica.
Once you have solved the problem of sending data from the pc to the Arduino, you create and encode a timestamp on the pc and send it. You receive it on the Arduino, parse it, and with the following Time library function, set the time:
Encoding data and sending
You should set form of timestamps to send to Arduino and encode timestamps in Mathmetica. The form is up to you but it is recommended to include symbols to indicate start and end of data string.
Receiving data in Arduino
Then, arduino can identify time stamp seeing start symbol and read data until the end symbol is found(using readSerialUntil function).
Parsing data
Now, from received string, extract information and store it to variables. It is simple if data length is fixed. If not, strtok function in C header <string.h> is useful.
setting Time
Example
1.
Assuming that Mathmetica shows timestamps like, 20190121172333 for Jan 21, 2019, 17:23:33. Then attach start and end symbol as following, $TIME,20190121172333;