Does anyone have an idea on the best way to time stamp an arduino sketch that contains a data packet which is to be read out using a java IDE (IntelliJ) for graphic visualization of the packet. I tried declaring the time variable on the arduino section but having problem unpacking the unsigned long variable in java. Below is a snippet of the arduino code.
unsigned long codeTime = 0; //The time the sketch takes to execute
static unsigned long char dataArray [1]; // a variable to hold both sensor data
void setup(){
Serial.begin(9600);
Serial1.begin(9600);
}
void state1(){
if (Serial1.available()){
while (Serial1.available() > 0){
codeTime = millis(); //using the millis() instead of micros()
//memcpy should copy the whole data into position 0 ????
memcpy(&dataArray[0], &codeTime,sizeof(codeTime));
uint16_t newByte1 = Serial1.read(); // read in the first byte
uint16_t newByte2 = Serial1.read(); //read in the second byte
uint16_t dataByte = newByte2 >> 8;
dataByte = (dataByte | newByte1);
dataArray[1] = dataByte;
Serial1.write((uint16_t*)dataArray, sizeOf(dataArray));
}
}
}
void loop(){
state1();
Serial1.flush(); //clear the serial port
delay(100);
}
The question now is how do i unpack the data in java to ensure that the time variable (unsigned long time variable is well unpacked in the correct order)????. The second variable i could easily unpack in java as
int variable1 = variable2 >> 8; //as it is a 16 bit integer I shifted
// the msb/lsb accordingly
variable1 = (variable1 | variable2); // and finally casting it.