Home STEM HQ TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 48


Scripting Tutorial - Lesson 48: Lua Scripting and the TI Innovator™ Hub:

8. Create a general Innovator™ Hub Document

Download supporting file for this tutorial

Texas Instruments TI-Nspire Scripting Support Page


Scripting for the Innovator Hub: Lesson 8: Create a general Innovator™ Hub Document

Top of Page

The previous example documents have been deliberately simplified in terms of user interface - just a point and click interaction, to make it easier to focus on the new Innovator Hub code.

Now it is time to generalise that interface and to allow the user to make changes and explore and experiment with the Innovator coding commands. This is easily achieved by simply using an interactive text box.


  1. Define the textbox and a short help text to initially go into it.

    Add text to the inBox for the various sample actions in the menu.


  3. Here we set up our inBox.

  5. The heavy lifting in this script lies in the tabKey function. We need to allow for those occasions when the user might want to include multiple send statements, and have these dealt with individually.

    Pay particular attention to the application of the little known Lua command loadstring. If a line in the inBox begins with SET, or READ or CONNECT, then it will be sent more or less directly to the Innovator for processing.

    If not, then if that command is a valid Lua function defined in your script (such as blink, or wait, etc) then it will attempt to carry out that command. This is very powerful and opens to the doors to many interesting applications.

  7. No changes from that previously defined.


  9. As previously defined.


-- TI Innovator Init Values

platform.apilevel = '2.7'
local screen = platform.window
local date = "021517"

require 'color'
pcall(function() require 'asi' end)

local isASIavailable = false
local hubStrOn, hubStrOff, msgStr, rxValue, timeStr, delay, repeats, drive
local tempo, key, I
local list1, list2 = {}, {}
local index, dataList = {}, {}
var.store("index", index)
var.store("dataList", dataList)
local inBox = D2Editor.newRichText()
local help = "Connect your Hub, and use the samples menu to enter different Hub commands. Press 'tabKey' to try these out: edit them and try your own."

function addMsg(input)

msgStr = input


function on.construction()

--the next three lines are required for Hub connection


{" ©2017 Compass Learning Technologies", function() end},
{" Version "..date, function() end},
{" Contact: steve@compasstech.com.au ", function() end},
{"Scan and Connect", function() TI_Innovator.connect() end},
{"Disconnect", function() TI_Innovator.disconnect() on.resize() end},
{"RESET", function() on.resize() end},
{"SET Samples",
{"SET LIGHT ON/OFF (default)", function() hubStrOn = "SET LIGHT ON" inBox:setText(hubStrOn) hubStrOff = "SET LIGHT OFF" on.tabKey() end},
{"SET COLOR.RED ON/OFF", function() hubStrOn = "SET COLOR.RED ON" inBox:setText(hubStrOn) hubStrOff = "SET COLOR.RED OFF" on.tabKey() end},
{"SET COLOR.GREEN ON/OFF", function() hubStrOn = "SET COLOR.GREEN ON" inBox:setText(hubStrOn) hubStrOff = "SET COLOR.GREEN OFF" on.tabKey() end},
{"SET COLOR.BLUE ON/OFF", function() hubStrOn = "SET COLOR.BLUE ON" inBox:setText(hubStrOn) hubStrOff = "SET COLOR.BLUE OFF" on.tabKey() end},
{"SET SOUND 220 5", function() hubStrOn = "SET SOUND 220 5" inBox:setText(hubStrOn) hubStrOff = "SET SOUND 0 1" on.tabKey() end},
{"SET SPEAKER 1 220 5", function() hubStrOn = "SET SPEAKER 1 220 5" TI_Innovator.Send("CONNECT SPEAKER 1 OUT1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET SPEAKER 1 0 1" end},
{"SET LED 1 ON", function() hubStrOn = "SET LED 1 ON" inBox:setText(hubStrOn) TI_Innovator.Send("CONNECT LED 1 OUT1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET LED 1 0FF" end},
{"SET SERVO 1 CW 50 5", function() TI_Innovator.Send("CONNECT SERVO 1 BB9") hubStrOn = "SET SERVO 1 CW 50 5" inBox:setText(hubStrOn) hubStrOff = "SET SERVO 1 CW 50 0" on.tabKey() end},
{"SET ANALOG.OUT 1 100", function() hubStrOn = "SET ANALOG.OUT 1 100" inBox:setText(hubStrOn) TI_Innovator.Send("CONNECT ANALOG.OUT 1 OUT1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET ANALOG.OUT 1 0" end},
{"SET DIGITAL.OUT 1 ON", function() hubStrOn = "SET DIGITAL.OUT 1 ON" inBox:setText(hubStrOn) TI_Innovator.Send("CONNECT DIGITAL.OUT 1 OUT1") TI_Innovator.Send(hubStrOn) hubStrOff = "SET DIGITAL.OUT 1 0FF" end},
}, {"READ Samples",
{"READ BRIGHTNESS (default)", function() hubStrOn = "READ BRIGHTNESS" inBox:setText(hubStrOn) TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn on.tabKey() end},
{"READ RANGER 1", function() hubStrOn = "READ RANGER 1" inBox:setText(hubStrOn) TI_Innovator.Send("CONNECT RANGER 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn end},
{"READ DHT 1", function() hubStrOn = "READ DHT 1" inBox:setText(hubStrOn) TI_Innovator.Send("CONNECT DHT 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn end},
{"READ ACCEL 1", function() hubStrOn = "READ ACCEL 1" inBox:setText(hubStrOn) TI_Innovator.Send("CONNECT ACCEL 1 I2C") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn end},
{"READ BAROMETER", function() hubStrOn = "READ BAROMETER" inBox:setText(hubStrOn) TI_Innovator.Send("CONNECT BAROMETER I2C") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn end},
{"READ ANALOG.IN 1", function() hubStrOn = "READ ANALOG.IN 1" inBox:setText(hubStrOn) TI_Innovator.Send("CONNECT ANALOG.IN 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = hubStrOn end},
{"READ DIGITAL.IN 1", function() hubStrOn = "READ DIGITAL.IN 1" inBox:setText(hubStrOn) TI_Innovator.Send("CONNECT DIGITAL.IN 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = hubStrOn end},
{"Select Sound",
{"C4", function() hubStrOn = "SET SOUND 262 TIME 1" inBox:setText(hubStrOn) TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff end},
{"A4", function() hubStrOn = "SET SOUND 440 TIME 1" inBox:setText(hubStrOn) TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff end},
{"C5", function() hubStrOn = "SET SOUND 523 TIME 1" inBox:setText(hubStrOn) TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff end},
{"No tone", function() hubStrOn = "SET SOUND 0 TIME 1" inBox:setText(hubStrOn) TI_Innovator.Send(hubStrOn) hubStrOff = "SET SOUND 0 1" msgStr = hubStrOff end},
{"Aliens", function() inBox:setText('Aliens') tempo = 1000 I=1 list1 = notes_aliens list2 = times_aliens playTone(list1,list2) hubStrOff = "Aliens" msgStr = hubStrOff end},
{"BallGame", function() inBox:setText('BallGame') tempo = 1000 I=1 list1 = notes_bgame list2 = times_bgame playTone(list1,list2) hubStrOff = "Take me out to the Ball Game" msgStr = hubStrOff end},
{"Happy Birthday", function() inBox:setText('Happy Birthday') tempo = 1000 I=1 list1 = notes_bday list2 = times_bday playTone(list1,list2) hubStrOff = "Happy Birthday" msgStr = hubStrOff end},
{"If I Only Had a Brain", function() inBox:setText('If I Only Had a Brain') tempo = 4000 I=1 list1 = notes_brain list2 = times_brain playTone(list1,list2) hubStrOff = "If I only had a brain" msgStr = hubStrOff end},
{"Fur Elise", function() inBox:setText('Fur Elise') tempo = 1000 I=1 list1 = notes_elise list2 = times_elise playTone(list1,list2) hubStrOff = "Fur Elise" msgStr = hubStrOff end},
{"Home on the Range", function() inBox:setText('Home on the Range') tempo = 1000 I=1 list1 = notes_home list2 = times_home playTone(list1,list2) hubStrOff = "Home on the Range" msgStr = hubStrOff end},
{"Well-tempered Scale", function() inBox:setText('Well-tempered Scale') tempo = 1000 I=1 list1 = notes_welltemp list2 = times_scale playTone(list1,list2) hubStrOff = "Well tempered scale" msgStr = hubStrOff end},
{"Harmonic Scale", function() inBox:setText('Harmonic Scale') tempo = 1000 I=1 list1 = notes_harmonic list2 = times_scale playTone(list1,list2) hubStrOff = "Harmonic scale" msgStr = hubStrOff end},
{"TIMER Samples",
{"wait(5)", function() wait(5) end},
{"blink(5,1)", function() blink(5,1) end},
{"alarm(5,0.5)", function() alarm(5,0.5) end},
{"Robot Samples",
{"CONNECT ROBOT SERVOS (BB9/BB10)", function() showKeyBoard = false
TI_Innovator.Send("CONNECT SERVO 1 BB9")
TI_Innovator.Send("CONNECT SERVO 2 BB10") end},
{"ROBOT FD 1 secs", function() fd(1) end},
{"ROBOT BK 1 secs", function() bk(1) end},
{"ROBOT LT 1 secs", function() lt(1) end},
{"ROBOT RT 1 secs", function() rt(1) end},
{"AUTO drive (BRIGHTNESS)", function() drive = "BRIGHTNESS" lightSeeker() end},
{"AUTO drive (DISTANCE)", function() TI_Innovator.Send("CONNECT RANGER 1 IN1") drive = "RANGER" auto() end},



We can put together much that has gone before and offer a glimpse of ways in which the Innovator Hub might prove a valuable tool within traditional lessons for building strong foundations for understanding and concept development.

The practical applications of sensors which measure distance, of course, are well established at all levels of schooling, from very young students (using bodily movement to build shapes and letters) through junior high school (where personal movement can be linked to key concepts such as gradient) and through to seniors (applied to velocity, rates of change and even a deeper understanding of the various functions which are the objects of study for so much of mathematics). The ability to use an ultrasonic motion sensor with our Innovator Hub invites all of these applications and more.

Other sensors prove valuable in building, not only STEM skills and understandings, but deep and rich mathematical and scientific ones. Consider for example, the in-built brightness sensor on the Hub. Practical questions such as Could a light sensor be used as a measure of distance from the light source?.

This is a question about calibration at its heart - but just as much it may be a question about linear relationships (a linear model is actually quite accurate over a distance of 1 - 2 metres), or more interesting functions for older students - What do students know about light intensity and its relationship to distance from the source? (science)

How might their readings be calibrated and converted: changing light intensity to metres?

Can you account for observed differences?

How might the most effective exploration be designed?

How accurate is the data they collect? How might they validate this?

The same questions might be applied to the use of barometric pressure as a measure of altitude - and how might a magnetometer serve as a compass?

The study of movement in all its forms comes alive when collecting data using an accelerometer - distance, velocity and acceleration become physical to students when their own motion generates immediate responses. Linking this motion to different functions then brings these functions to life, leading to deeper understanding.

Simple, personal applications are readily found: Where is the best place to sit in the classroom?

Like most of the best STEM-active questions, the answer is ... It depends!

Some people feel the cold more than others, and like to sit near a heater. For others, they want to be as far away from the heat source as possible. Some like to sit where the light is bright. There are numerous factors that determine our preferences, and these can be easily explored using the Innovator Hub to grab the readings at different points around the room.

This can even be set as a game challenge. In the past, I would go into the room before the students and collect temperature and light data from several points, then display the graph to the students and ask them to decide where these spots are located! It offers a fun and worthwhile challenge and is immediately applicable to building graph sense and interpretation skills.

In the final example shown, a rotary angle sensor is connected to OUT1, and read simply as ANALOG.IN 1 (a potentiometer could have been used just as easily). Values for the range of turns vary from 0 to 15864. These values may be eeasily converted to something more accessible - say values from -5 to 5 by grabbing the last value of the dataList list - and used to control the gradient of a straight line as shown (or the y-intercept, or any other relevant variable).

You may also notice the use of the wait function here to give time for the student to move to the graphs page and to explore changing values for the gradient.

Students then may control the gradient by turning the rotary angle sensor and explore the different effects, building a very concrete understanding of the concept under investigation. Even the calculation of the conversion factor required offers a lovely application of linear functions!

Applying the Innovator Hub to the Traditional Curriculum:
Bringing Mathematics and Science to Life
by Putting It into the Hands of Students

In our next lesson, we combine Innovator Hub scripting with BLE to develop general scripts that will work on all our TI-Nspire platforms.


Back to Top


Home STEM HQ TI-Nspire Authoring TI-Nspire Scripting HQ Scripting Tutorial - Lesson 48