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(){
         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(){
                   Serial1.flush();  //clear the serial port 

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.
  • I don't see in your code where you are actually sending the time through serial. – Majenko May 4 '16 at 23:15
  • @majenko I saved the time variable 'codeTime' in position zero in the buffer (buffer[0]).. and i wrote both data through to serial using Serial.write(buffer,sizeof(buffer)); – PayneLaidBack May 4 '16 at 23:19
  • I don't see that in your code. – Majenko May 4 '16 at 23:20
  • @majenko I just saw the mistake.. fixing now – PayneLaidBack May 4 '16 at 23:21

There's a number of fundamentally wrong things in your sketch.

static char buffer[1]; // a variable to hold both sensor data

No - that has room for 1 entry - hence the 1. 1 means 1, not 2.

buffer[1] = newByte;

No. There is no buffer[1]. You count slices from 0. buffer[0] exists (the first one) but buffer[1] doesn't.

buffer[0] = millis;

You aren't calling the function, you are assigning the address of the function. And worse than that, buffer is an array of 8-bit values - neither the address of the function, nor the return value from the function (if you were to fix that) would fit in that space - since the return value is an unsigned long which is 4 bytes in size.

You need to have at least 5 bytes in your array - four for the time stamp and one for the value you read from serial. Then you place them in to the array in the order you see fit, and read them back in the same order in Java.

  • i replaced the buffer with a 16 bit array that could comfortably contain all 4 bytes but the problem still persist – PayneLaidBack May 5 '16 at 2:36
  • In what universe can you fit a 32 bit value and an 8 bit value into a 16 bit array with 1 slice? You can't count, can you? – Majenko May 5 '16 at 8:41
  • was a little bit buggered there, got that fixed now, is that fine by you?? – PayneLaidBack May 6 '16 at 5:51
  • No. You still can't count. Where is your dataByte going? Scrap the array. Don't use it. There's no point. Just send the 6 bytes you need to send as bytes. That is, split your unsigned long into 4 bytes sending each one as you go (bit-shift and mask), then you data bytes. 6 bytes. That is all. And then work out how you are going to know at the other end which byte is which. Your whole methodology is flawed. You have no protocol, only raw data. – Majenko May 6 '16 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.