Tuesday, September 6, 2016

NXT Security System

MIT App Inventor 2 is a great tool to develop Android apps.  It also has some robotic functionality as there is built in support for LEGO NXTs and EV3s.  I just finished a very small test program to try out this functionality.



I wrote an app which uses a light sensor and sound sensor on an NXT to simulate detecting an intruder. The light sensor normally has a laser pointer flooding the input, but if something crosses the path of the laser, the light reading drops much lower. This triggers an alarm which is indicated by a moving motor, a beeping sound, and a visual indicator ("Alarm!") on the Android phone. Similarly, a sound above the threshold (set at 125) on the sound sensor trips the alarm. The Android interface allows you to turn on and off the alarm, shows the values of the sound and light sensors, and shows whether you are connected to the NXT.

When creating an app in MIT App inventor, you first create the interface (Designer) then create the code (Blocks).  Coding is drag and drop just like Scratch or Enchanting.   The interface for this app is shown below:

There are several button to connect and disconnect to an NXT over Bluetooth, and to turn on or off the security system.  There are also several NXT specific objects: an NXT Sound Sensor, an NXT Light Sensor, and an NXT Direct Commands (which gives access to motor control).  A clock is setup with a one second increment.  Finally, there are several "labels" which are places where text is displayed.  One label (status) shows whether the alarm is on, off, or triggered.  Another label (SoundReading) shows the sound sensor reading or "not connected" if there is no Bluetooth connection.  The last label (misnamed UltrasonicReading from previous experiments) shows the light reading or or "not connected".

The coding is all event driven.  Events can be triggered by pressing a button on the App interface, by a recurrent timer, or by certain sensor criteria being met.
The Connect to Robot and Disconnect buttons give Bluetooth functionality.  When you click the Connect To Robot button (which is actually a list picker), it brings up a list of available Bluetooth devices.  When you select one, the App makes a connection to that NXT.  The Disconnect button ends that connection.

Two global variables are used to control whether the alarm is on (on?), and whether it has been triggered (AlarmTripped?).  The OnButton sets on? to true.  Once that criteria is met, either a NXTSoundSensor.AboveRange or a NXTLightSensor.BelowRange event can make the second variable (AlarmTripped?) true.

A clock event is triggered every second.  This event displays the sound and light sensor readings if there is a Bluetooth connection.  Otherwise it displays "Not Connected".  If the value of AlarmTripper? is true, it plays a tone, and spins a motor.  Because the duration of the tone is half of a second and the clock event is triggered every second, the overall aural effect is a periodic beeping sound which sounds like an alarm.

The OffButton resets both global variables to false and stops the motor.

Overall, this was a simple but great way to learn MIT App Inventor 2.  The project has been published on the App Inventor 2 website:  http://ai2.appinventor.mit.edu/?locale=en#4626597671075840.


No comments:

Post a Comment