Abstracting SET COLOR — and Connecting to Prior Learning

Have you ever had one of those learning experiences where you thought you knew something going in (and knew it pretty well)…but then someone looked at a problem/task/question in a different way…and their approach broadened and deepened your understanding of what you thought you “knew”…? Those are some of my favourite learning experiences! Where I add connections and understanding to my knowledge by learning from others who think differently and/or who approached a task in a different way.

Here’s an example of just such a learning experience where a fellow educator recently deepened and broadened my understanding of the SET COLOR command on the TI-Innovator.

First, some background. Earlier this month, during a Teaching Computer Science with TI-Nspire workshop, we spent lots of time exploring different data types (including strings, floats, ints, etc.) and working with input and output commands related to these data types.

We learned how to Display output to the program’s user, including output made of different data types such as in the hypotenuse program shown below:

Running the hypotenuse program with actual parameters of 3 and 4 yields:

We informally called this use of the comma (,) in the Disp command concatenation, but then we were challenged to think more deeply about our use of this term later on. For now, we realized that the Disp command had syntax of:

Disp __, __, __, …

where any given blank could contain a string (in quotes) or a variable (string variable or numeric variable).

We later realized that the comma wasn’t our only (or even best?) way to concatenate, as we discovered when we ran into a program like this:

In this case, with the Request statement, we couldn’t use the comma to “concatenate” as we had done before with the Disp statement. We realized that this was due to the syntax of the Request statement, which looks something like this:

Request “[message to user]”,variable_to_store_response

The comma wouldn’t work to “concatenate”/connect output together here, since the Request statement was only expecting one comma in its syntax. (Side note: Request can actually receive a second comma followed by a 0 to suppress the user input, but that’s another story…! :))

Instead, we used the ampersand (&) to concatenate within the Request statement shown above.

From there, we wondered whether the comma (,) really had been concatenating for us earlier. We set off to investigate this further.

What do you think the output of this program would be?

Notice how the numeric variable age displays without a problem when using the comma (,) to separate Disp elements, but must be converted into a string (using the string() command) in order to be displayed using the ampersand (&) for concatenation. We had many interesting discussions about these details…!

Here is the output from running this program:

Notice anything interesting…?

We noticed some spacing issues. Namely, that the Disp __,__,__ approach inserted a space between each __ element, while the Disp [string]&[string]&[string] approach did not insert spaces between the various strings (as is true of typical concatenation).

Interesting… We were stretching our thinking and making connections about data types, output formatting, and concatenation methods.

Then, came this activity late in the second day…

We were learning how to code output to the RGB LED that is built into the TI-Innovator. We quickly learned that the syntax for sending colours to the RGB LED is:

Send “SET COLOR # # #”

where each of the three #’s is an integer from 0 to 255. We explored the effects of changing these numbers in order to determine what each of the three numbers controlled and how it controlled it.

Here is a screenshot of a program that turns on the red LED within the RGB LED at full intensity:

However, we had a problem. Our program, as currently written, wasn’t all that helpful. Specifically, our program coded only one particular colour — and if we wanted to display another colour, we had to go into the code for the program and make changes there. This wasn’t very user-friendly!

We decided to abstract our program so that the user could display whatever colour they wanted by entering three values for the red, green, and blue intensities.

For the sake of expediency in coding (at the sake of user-friendliness for users who didn’t write the program), we decided to pass these values into the program as formal parameters (i.e. via colour(r,g,b)). We updated our three colour intensity values in the Send “SET COLOR…” line with the variables that represented these colour intensities (r, g, and b)…and quickly figured out that our program didn’t work. Hmmm…

We had a great discussion about what was going on and going wrong here… Specifically, we were sending a string to the Innovator to be translated into visual output. The r, g, and b variables were being read as r, g, and b characters within the string — rather than reading the values these variables actually represented!

No worries, as I knew where this was going. (This is that “knew” thing I was talking about earlier…) I swooped in and saved the day by introducing the eval() command, which evaluates the value of the variable and pastes that into the Innovator string output/send command, instead. Our code now looked like this and worked as intended:

But then…the magic. Someone looked at this problem differently…

T, one of the participants in the room, piped up with “I found another way to do it…! An even more efficient way!” We pulled T’s screen up on the Navigator system and considered his code:

Discussion abounded.

“Wait — doesn’t Send “SET COLOR…” need three values in it?”

“Oh, he’s concatenating with the commas!”

“Don’t we need spaces between the numbers?”

“Could you use ampersands instead?”

“This is like how we used the Disp statement!”

“Wait — does that work?!”

I was surprised by T’s discovery (I had never seen this approach before!) and delighted (of course that works! How fun!). It was a wonderful moment of learning that cemented many ideas we had considered over the course of two days — and provided us with additional flexibility for coding a new command we were just learning to use.

What are your thoughts?

What might the value be in using syntax of Send “SET COLOR eval(r) eval(g) eval(b)” for a program like this?

What might the value be in using syntax of Send “SET COLOR ”,r,g,b ?

What does each method add to our coding arsenal and/or to our understanding of CS topics?

And…is there another way?