Home TI-Nspire Scripting HQ STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 6

 

Lesson 6 - Making Music via BLE

Energia Reference page

Use your Chrome browser on Mac, Android or ChromeBook or the LightBlue App for iOS or Android to test this sketch.

Texas Instruments TI-Nspire Scripting Support Page

Download the TI-Nspire LaunchPad document for this lesson

     
           
     

Sending Multiple Characters to the Board - and Making Music!

The nature of serial communication is essentially to send one character at a time. This works well for very simple situations, as we have just seen, but very soon we will need a better system. For example, suppose we wanted to send numbers like 220, and 440 (the frequencies for A3 and A4 on the musical scale)?

What we need to do is to catch each character as it is read, convert to a string, concatenate (glue) these together, until an end of string signal (like, say, a carriage return or enter key) is received. This way, instead of reading "2", then "2" then "0", we read "220".

Study the LaunchPad sketch below and see how this is accomplished.

Note some useful commands - like isDigit, string.toInt() and useful terms like \n (carriage return/new line). Note, too, the syntax for concatenating: inString += (char)recvChar;.

The Lua script is essentially the same script as that used for the last lesson - it searches for the enterKey() equivalent (string.char(10)) and uses this to recognise the BLE input.

For free options to test and explore our BLE code, we can use the LightBlue App for iOS or Android, or your Chrome browser on Mac, Android or ChromeBooks.

  

Hacking the Hub™

Note, in the sketch below the sound pin used is pin 34 - which is the pin allocated to the Innovator Hub speaker!

Remember: You can always reflash your Innovator Hub to return it to its original state, using the tools that TI has made available (just follow the Resources tab, and then "Keep your Innovator up to date"). So it is possible to explore coding the Hub in new ways!

  

  

TI Innovator™ Hub (with MSP432 LaunchPad)
  
  

LaunchPad Sketch
(Copy and paste into Energia)

String inString = "";
int pin = 34;
int duration = 2000;

void setup()
{

Serial1.begin(9600);
}

void loop()
{
while(Serial1.available()){

char recvChar = Serial1.read();
if (isDigit(recvChar)) { // check if the input is a digit
inString += (char)recvChar; // add that character to a string
}
if (recvChar == '\n') { // if you get a newline, play the string
int input = inString.toInt(); // convert the string to an integer
if (input == 0 ) {
noTone(pin); // stop playing the tone:
} else {
tone(pin, input, duration); // play that frequency eg 440
}
inString = ""; // clear the string for new input:
}
}
}

Lua Script (copy and paste full script into TI-Nspire Script Editor)

 
 

platform.apilevel = '2.5'

screen = platform.window
w = screen:width()
h = screen:height()
local date = "110216"

pcall(function () require 'bleCentral' end)

require "color"
local nameList = {'HMSoft'}
local bleState = ''
local bleStatus = 'Stand by'
local peripheralName = ''
local myPeripheral = nil

local groveBLE = 'FFE1'
local keyPress = 0
local alert = nil
local myChar = nil

local textBox = D2Editor.newRichText()
local boxX, boxY, boxWidth, boxHeight
local fontSize = 12
local button1 = "Scan for BLE"
local button2 = "Reset"

 
 

 
 

A Practical Application

By adding a Lua-based keyboard, these scripts become the basis for an interesting and extendable document. Using variables to transfer information across the windows means that the keyboard can send tones to the speaker, and can even be used to drive a graphical interpretation of the different notes.

All based upon a small and inexpensive LaunchPad board and BLE module!

  
  

We can add a whole extra dimension to our sound exploration by including a keyboard. Check out problem 2 of the attached document and you will find a Lua keyboard that lets you create your own Innovator OR BLE music!

This same document will work with both the TI Innovator Hub and with the TI-Nspire iPad Apps using BLE (BlueTooth Low Energy) and the TI MSP432 LaunchPad. Use the free Energia software to flash the attached sketch to the LaunchPad via USB, then follow the guides outlined in this and the previous LaunchPad lesson.

There are also options to explore some of the interesting applications of the harmonic mean to creating alternative tunings, as developed in Mathematics: A Search for Harmony.

  
  

Or (if you are using Chrome browser on Mac, Android or Chromebook, or the WebBLE app on iPad) you might just try this for yourself!

 

BLE LaunchPad Controls

©Daniel Loginov

 

   

  
  

BLE LaunchPad Arc Piano

© Mike Bostock


Back to Top

   
 

Back to Top

  

Home TI-Nspire Scripting HQ STEM HQ Getting Started with TI LaunchPads TI LaunchPad Lesson 6