Print

Variables On The NXT

Blueprint on LaunchPad(external link)

Variant Types


In NXT-G, a user can choose between three types of variables. I believe they are called "Text", "Logic" and "Number".

In Scratch, a user just uses a variable without ever specifying what type it is.

LeJOS, of course, needs a variable type specified.

In Enchanting, we make it so the user never specifies the type. It is coerced into whatever is needed.

Thus, the string "Hello" evaluates to 0 as a number and true as a logical value. "False" evaluates to 0 and false. The number 10 will be "10.0" as a string and True as a boolean.

The existing code seems to work well enough, but the variables are always stored as strings. It would be nice to optimise this with some sort of variant type(external link).

Changing Variables


The Scratch communication protocol allows us to send messages when variables change. We should add a timestamp or flag and set it whenever a variable changes. (Fortunately, they only change in one place). Then, whenever we are able to send messages to the computer, we should send all variables that have changed since the last time we sent messages.

Also, a user may change the variable in Enchanting. If that is the case, it needs to be updated on the NXT.

See also Communicating Between Scratch and The NXT.

Sliders And Reporters


In Scratch, a user can change the way a variable is displayed. It can be normal, a large readout, or a slider.

Image

Likewise, there are blocks in Scratch known as Reporters. Reporters allow you to click on a checkbox and then they show up on the stage.

Image

I would like reporters to work in Enchanting. This will make it really easy for a child to say, "What reading am I getting from the light sensor?" They check the appropriate reporter, run their program again, and the number is right there, on the screen!