I'm trying to figure out how to send integers that are larger than one byte from Matlab to Arduino through the serial port. For starters, I'm just trying to see if I can send two byte integers and then try to generalize from there. My idea so far is to take a number in Matlab say 2344, and turn it into a 16 digit binary number 0000100100101000. I then break that up into two 8 digit binary numbers 00001001 and 00101000. I then convert those numbers back to decimal numbers 9 and 40, which I send to the Arduino with fwrite(s1,9,'uint8') and fwrite(s1,40,'uint8').
Now I'm trying to reconstruct these numbers back to the original 2344. My idea is to store them in two sequential bytes in the Arduino's memory. So I have 00001001 in one byte and 00101000 in the next byte. Then I tell it to reinterpret those 2 separate bytes as a single 2 byte integer. I'm trying to figure out how to use pointers to reference those memory addresses, but I'm not sure exactly how to code it. Is it even possible to do it this way? I'm kind of new to c++. If anyone has any better ideas I'm open to them.
Thanks worked great. Here's the code I ended up writing. I changed some of the data types. As a test I made the LED light up if the correct number was sent (I chose 506).
Can I ask you how you've done to separate and send the two byte in matlab?
Because my target is send angle and traslation values that are contain in a matrix. Pratically I isolate each value that could be between o-360 for angle and 0-3600 for tralsation. So you're program could be a really good solution.