Serial write number not working?

I am trying to simply print out a sensor reading to the serial monitor with Makecode. I’ve found that putting

serial write line(acceleration (mg) x)

inside the forever loop works just fine. I click on “Show data Device” and it prints out the readings and the graph works.

However, if I try the “serial write number” block instead, then when I click “Show data Device,” it just says “Values will be logged when the device sends data” and nothing else displays.

The “serial write value x = “ block also works, but I can’t get “serial write number” to work.

I tried googling around for this but haven’t found any other reports of this specific issue. Any suggestions? Trying to explain this to kids and it’s a little confusing since there are so many different options for serial writing, including “write string” and “write number,” so you’d think you would use “write number” to write a number.

I think you need to add (parse to number ()) to it.

I think there are two related things that could be a problem and explain what you’re experiencing:

  • serial write number will write just the number. If you have a loop that does serial write number 0, it will send repeated zeros with nothing in between. This will eventually be displayed as “00000000000000000000000000000…”. (It’s possible that if you wait long enough you will see a weird, large number)
  • The editor’s Show data does buffering. Many have experienced “buffering” when watching video — it’s when video playback stops until your computer has enough data to play the next segment of data. The above example with zeros won’t print anything until it has enough characters to fill up the buffer, which appears to be around 255. The other examples (write line and write value x = ) also send a new line (end the line), which forces the data display to immediately use the contents of the buffer rather than waiting for it to fill.

You can get better behavior from serial write number by combining it with a block that sends a new line and forces the data display to immediately use the contents of the buffer. Here’s an example that uses write number and may behave more like you’d like/expect by following it with an empty write line: https://makecode.microbit.org/_4fwc3uYLX7tL

Yep - I also contacted microbit support about this, and got the same explanation about how “serial write number” does not force a new line. The documentation isn’t always explicitly clear about which serial write options force a new line and which ones don’t, so I suggested that they might want to include that. Thank you!