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

           


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

2. READING from the Hub



Download supporting file for this tutorial

Texas Instruments TI-Nspire Scripting Support Page

       
  

Scripting for the Innovator Hub: Lesson 2: READING from the Hub

Top of Page

I have heard the two key principles of STEM described as feedback and control.

If SENDING to the Hub is the basis of control, then reading is all about feedback: asking for information, collecting that information, organising and making sense, and then acting on what you have learned.

The wide range of sensors and other peripherals supported by the Innovator™ Hub give students and teachers a wealth of possibilities for gathering information and seeking to understand the world around them. They also give many opportunities for the creation of great dynamic and engaging lesson materials.

  
  
  

There are very few changes between this script and that of Lesson 1 - but this makes it worthwhile studying those changes closely and seeking to better understanding the key workings of our Innovator platform.

  
  1. What do you notice in the initial values and functions?

    We will continue to use variables hubStrOn and hubStrOff, even though the latter does not play a part here - this will determine the focus of the interaction, whether that be the default in-built brightness sensor, or one of the many others available.

    The other key change lies in the menu offering: you will see quite a different "sample" menu here: instead of turning LEDs on and off, and playing sounds, we CONNECT a range of peripherals (I have selected just a few that I had available) and instruct the Hub to READ that device - or, more correctly, to read the voltage from that port.

    NOTE that only brightness does not require a "connect" instruction to be sent - all other peripherals need to be linked to an appropriate port on the Hub. You should note that ports IN1, IN2, OUT1 and OUT2 are all 3.3V ports, while IN3 and OUT3 carry 5V. Some sensors are very particular about their voltage - so be careful with this.

    You will see that, for each of the READ Samples (other than brightness), a CONNECT command is sent to the Hub: this needs to only occur once; subsequent calls to read that device will understand where it is to be found.

    I would finally draw your attention to the last two "READ" options: reading generic peripherals ANALOG.IN and DIGITAL.IN - as introduced in the previous lesson, these generic commands can be used to advantage in many situations, and stand in for specific peripherals.

  2.   
      

  3. As previous, with the addition of a display line for the readings to be shown.
  4.   
      
  5. Here we most certainly require the three Innovator functions - study them closely and see why!

    In particular, the important TI_InnovatorReadCallback function, which read and defines rxValue ("received value"), which may be numeric (such as brightness or ranger distance), string (such as 'READY') or even list (such as DHT - Digitial Temperature and Humidity, Accelerometer or Barometer).

    You will see the utility function list2str defined to grab just the first value of a list separated by carriage returns (string.char(10)).

    For interest, the voltage is also calculated, since this is how varying values are actually read by the Hub.

  6.   
      
  7. A challenge: how might we use the oneShotTimer to read the current sensor repeatedly, instead of just the once when the mouse is pressed?

  8.   
      

  9. These functions are as previously defined.

  
  

Once again, if you have a Hub, you can test this script yourself by copying and pasting from this page. Of course, prepared versions are also available from the download link for this page.

 
 

-- 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

function addMsg(input)

msgStr = input
print(msgStr)
screen:invalidate()

end

function on.construction()

--the next three lines are required for Hub connection
TI_Innovator.init(TI_InnovatorStateCallback)
TI_Innovator.setReadListener(TI_InnovatorReadCallback)
TI_Innovator.connect()

Menu={

{"About",
{" ©2017 Texas Instruments", function() end},
{" Version "..date, function() end},
{" Contact: steve.arnold@ti.com ", function() end},
},
{"Controls",
{"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" hubStrOff = "SET LIGHT OFF" TI_Innovator.Send(hubStrOn) end},
{"SET COLOR.RED ON/OFF", function() hubStrOn = "SET COLOR.RED ON" hubStrOff = "SET COLOR.RED OFF" TI_Innovator.Send(hubStrOn) end},
{"SET COLOR.GREEN ON/OFF", function() hubStrOn = "SET COLOR.GREEN ON" hubStrOff = "SET COLOR.GREEN OFF" TI_Innovator.Send(hubStrOn) end},
{"SET COLOR.BLUE ON/OFF", function() hubStrOn = "SET COLOR.BLUE ON" hubStrOff = "SET COLOR.BLUE OFF" TI_Innovator.Send(hubStrOn) end},
{"SET SOUND 220 5", function() hubStrOn = "SET SOUND 220 5" hubStrOff = "SET SOUND 0 1" TI_Innovator.Send(hubStrOn) 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" 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 OUT3") hubStrOn = "SET SERVO 1 CW 50 5" hubStrOff = "SET SERVO 1 CW 50 0" TI_Innovator.Send(hubStrOn) end},
{"SET ANALOG.OUT 1 100", function() hubStrOn = "SET ANALOG.OUT 1 100" 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" 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" TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn end},
{"READ RANGER 1", function() hubStrOn = "READ RANGER 1" 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" 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" 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" TI_Innovator.Send("CONNECT BAROMETER I2C") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() msgStr = rxValue or hubStrOn end},
{"READ ANALOG.IN", function() hubStrOn = "READ ANALOG.IN 1" TI_Innovator.Send("CONNECT ANALOG.IN 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = hubStrOn end},
{"READ DIGITAL.IN", function() hubStrOn = "READ DIGITAL.IN 1" TI_Innovator.Send("CONNECT DIGITAL.IN 1 IN1") TI_Innovator.Send(hubStrOn) TI_Innovator.Read() hubStrOff = hubStrOn end},
},
}
toolpalette.register(Menu)
screen:invalidate()
end

 
 

 
 


  

Back to Top

  

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