Serial.write printing blank values
Problem 1:

You wish to send/display on the Serial Monitor the int and float values generated by your sensor, but Serial.print() uses too much memory.

Solution 1:

Optimize your sketch to use less memory.



Problem 2:

Serial.write() doesn't cause the values to show up on Serial Monitor, but something is getting sent, because I see scrolling activity.

Solution 2:

Serial.write() sends raw bytes, whereas Serial.print() converts a variable to a string (or series of ASCII bytes) and sends that string. For example, Serial.write(65) sends A to the Serial Monitor because the ASCII value of A is 65; Serial.print(65) sends 65 to the Serial Monitor because it converts the numeric value 65 into a string containing the character 6 and the character 5, and sends that string to Serial Monitor.

The Serial Monitor expects only ASCII data. You are using Serial Monitor for development/debugging, but your final application does not require ASCII, only to transmit the numbers.

Serial.write() comes in a few forms. One is Serial.write(val) which sends a single byte (named val, of type byte or compatible). Another is Serial.write(buf, len) which sends a series of len bytes, starting at the address of memory pointed by by buf. (This is often when buf is an array of bytes, so buf points to the first element of the array.)

If you wanted to send the 4 bytes of a float using Serial.write(), you could call:

Serial.write(&amp;(event.orientation.x), sizeof(event.orientation.x);


As an alternative, you can use bit shifting to send one byte at a time of the 4-byte float:

Serial.write((byte)((event.orientation.x &amp; 0xFF000000) &gt;&gt; 24);
Serial.write((byte)((event.orientation.x &amp; 0x00FF0000) &gt;&gt; 16);
Serial.write((byte)((event.orientation.x &amp; 0x0000FF00) &gt;&gt;  8);
Serial.write((byte)((event.orientation.x &amp; 0x000000FF)      );


On the receiving end, you need to reconstruct your float by doing the reverse.

But you have multiple 4-byte values to send, in rapid repeating loop, so what happens if your receiver and transmitter get out of sync? Then you might want to put in a "flag" value to indicate the start of a sequence of data and a different flag for the stop of a sequence of data. What happens if your start or stop "flag" value actually shows up as one of the bytes you send as part of a real data byte? Then you need to find some way to modify the "real" data so that it's not to be construed as a flag.

This looks to be getting complicated now? Fortunately, these problems have existed and been solved for decades. Unfortunately, there is no consensus on the best or right way.

I did a quick search and found this arduino library that facilitates packet-based serial communication:

https://github.com/bakercp/PacketSerial

Perhaps that can help you take a bundle of numbers and send them reliably to some other system. I don't know that library in particular, and there are probably others that do something similar. This one claims to be compatible with the PySerial, COBS, and SLIP modules for dealing with packetized serial data communications.