I need to send how many milliseconds elapsed since the beginning of the time the arduino runs to java using Serial.write(). However, the largest integer that I can throw in there is 255, naturally since the largest 1 byte value we can send is 0xFF.
But if we have an integer like 6000 miliseconds that I want to send into java, how can I do this? I was initially thinking of just checking if the number is greater than 255 then bit shift it by 4 to the left then doing number<<4 & 0xFF00
and also writing (number<<4)>>8 &0xFF
But
- that doesn't work for numbers really large and
- I believe when I stream these int values into Java they get read as characters...
how do I fix this?
EDIT I've created a second version as the following
void sendData(int a ){
while(a>0){
Serial.write(a&255);
a = a>>8;
}
}
on the java side I have to somehow multiply the correct x for 16^(x) to the value that we receive since everything is received backwards I'm not sure if this is the best way to go since we don't necessarily know how many bytes that we'll receive