{"id":1838,"date":"2015-07-03T12:39:07","date_gmt":"2015-07-03T12:39:07","guid":{"rendered":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1838"},"modified":"2015-07-04T05:01:28","modified_gmt":"2015-07-04T05:01:28","slug":"using-android-javascript-and-arduino-to-control-your-robot-makered-javascript-android","status":"publish","type":"post","link":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/using-android-javascript-and-arduino-to-control-your-robot-makered-javascript-android\/","title":{"rendered":"Using Android, JavaScript, and Arduino to control your robot. #makered #javascript #android"},"content":{"rendered":"\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=http%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fusing-android-javascript-and-arduino-to-control-your-robot-makered-javascript-android%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/droid-script.jpg\"><img loading=\"lazy\" class=\"alignnone wp-image-1768 size-large\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/droid-script-1024x608.jpg\" alt=\"DroidScript\" width=\"608\" height=\"361\" srcset=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/droid-script-1024x608.jpg 1024w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/droid-script-300x178.jpg 300w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/droid-script-900x534.jpg 900w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/droid-script.jpg 1236w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/a><\/p>\n<p>Let\u2019s say you want to tinker with making a robot controller on your Android device, but you don\u2019t have a lot of time to learn Java. \u00a0Perhaps you just love JavaScript and want to write Android Apps. \u00a0 In our maker education programs, we enjoy introducing students to JavaScript since the language helps students go from idea to prototype quickly.<\/p>\n<p>Consider checking <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.smartphoneremote.androidscriptfree&amp;hl=en\" target=\"_blank\">DroidScript on the Google Play Store<\/a>. \u00a0<a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.smartphoneremote.androidscriptfree&amp;hl=en\" target=\"_blank\">DroidScript <\/a>enables you to quickly build simple Android apps using JavaScript.<\/p>\n<p>I greatly appreciate DroidScript enabling you to edit programs directly from a Wifi connected desktop computer. \u00a0All you need is a desktop web browser. \u00a0\u00a0You don\u2019t need to install Eclipse, Java, simulators, Netbeans or anything. \u00a0\u00a0You press a button in DroidScript and the app fires up a web server on your Android device. \u00a0\u00a0From your web browser, you can start making new apps, exploring and running sample programs, and checking out the documentation.<\/p>\n<p>What features of Android can you access using DroidScript?<\/p>\n<ul>\n<li>You can use the GPS, Compass, Camera, and Accelerometer.<\/li>\n<li>DroidScript can do basic graphics functions.<\/li>\n<li>According to the documentation, you can send and receive emails and SMS.<\/li>\n<li>You can control Arduino and Lego NXT.<\/li>\n<li>On a personal project, I used DroidScript to send commands to an Arduino through serial communication.<\/li>\n<li>You can also fire up a custom web server so that your phone can respond to HTTP requests.<\/li>\n<\/ul>\n<p>I think users will appreciate the effective samples and documentation.<\/p>\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/droidscript_docs.png\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-1769\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/droidscript_docs-168x300.png\" alt=\"Docs screen\" width=\"168\" height=\"300\" srcset=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/droidscript_docs-168x300.png 168w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/04\/droidscript_docs.png 506w\" sizes=\"(max-width: 168px) 100vw, 168px\" \/><\/a><\/p>\n<p>For the young programmer, hobby programmer or someone who needs some quick code duck tape on Android, DroidScript is worth checking out. \u00a0If you need help, they have an active forum community at<a href=\"https:\/\/groups.google.com\/forum\/#!forum\/androidscript\"> https:\/\/groups.google.com\/forum\/#!forum\/androidscript<\/a><\/p>\n<p><strong>Let&#8217;s Build a Robot Control program<\/strong><\/p>\n<p>In <a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/moving-your-diy-robot-using-arduino-and-servos\/\" title=\"Moving Your DIY Robot using Arduino and Servos\" target=\"_blank\">our previous blog post<\/a>, we showed you how to build your own DIY servo robot using Arduino. \u00a0 Let&#8217;s assume that the Arduino code follows the following protocol when it receives bytes on the serial port:<\/p>\n<ul>\n<li>When w is received, the robot moves forward.<\/li>\n<li>When \u00a0s is received, the robot moves backward.<\/li>\n<li>When a is received, the robot moves left.<\/li>\n<li>When d is received, the robot moves right.<\/li>\n<li>When the space character is received, the robot stops all motion.<\/li>\n<\/ul>\n<p>\nHere&#8217;s your test robot using an Arduino Nano.\n<\/p>\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/07\/androidBot.jpg\"><img loading=\"lazy\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/07\/androidBot.jpg\" alt=\"Android Bot\" width=\"500\" height=\"281\" class=\"alignnone size-full wp-image-1842\" srcset=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/07\/androidBot.jpg 500w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/07\/androidBot-300x168.jpg 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>For this robot design, I want to use my Android device\u00a0and a blue tooth keyboard to remotely control the Android device. \u00a0 The blue tooth keyboard helps me control the robot at a distance. \u00a0You will also need to obtain a USB to micro-USB adapter <a href=\"http:\/\/www.amazon.com\/Samsung-EPL-AU10WEGXAR-USB-Adapter\/dp\/B00D5R38HW\/ref=sr_1_5?ie=UTF8&amp;qid=1435925211&amp;sr=8-5&amp;keywords=samsung+usb+to+microusb+adapter\" target=\"_blank\">like this one to<\/a> connect your Android device to Arduino. \u00a0Here&#8217;s the code needed to control the robot.<\/p>\n<p><code><br \/>\n<html><br \/>\n<head><br \/>\n<script src='file:\/\/\/android_asset\/app.js'><\/script><br \/>\n<script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.1.3\/jquery.min.js\"><\/script><br \/>\n<\/head><br \/>\n<script>\n    \/\/Called after application is started.\n    function OnStart()\n    {\n        app.ShowPopup( \"Robot control active\" );\n        usb = app.CreateUSBSerial(9600);\n    }\n<\/script><\/p>\n<p><script>\nfunction forward()\n{\nusb.Write( \"w\" );\n}<\/p>\n<p>function back()\n{\nusb.Write( \"s\" );\n}<\/p>\n<p>function left()\n{\nusb.Write( \"a\" );\n}<\/p>\n<p>function stop()\n{\nusb.Write( \" \" );\n}<\/p>\n<p>function right()\n{\nusb.Write( \"d\" );\n}<\/p>\n<p>$(document).keypress(function(event) {\n\tswitch(event.charCode)\n\t{\n\t\tcase 119: \/\/w was pressed\n\t\t\tforward();\n\t\t\tbreak;\n\t\tcase 115: \/\/s was pressed\n\t\t\tback();\n\t\t\tbreak;\n\t\tcase 97: \/\/a was pressed\n\t\t\tleft(); \n\t\t\tbreak;\n\t\tcase 100: \/\/d was pressed\n\t\t\tright();\n\t\t\tbreak;\n\t\tcase 32: \/\/space was pressed\n\t\t\tstop();\n\t\t\tbreak;\n\t\tcase 49:\n\t\t\tvar pitch = 1.0, speed = 1.0;\n\t\t\tapp.TextToSpeech( \"Resistance is futile. You will be assimilated.\", pitch, speed );\t\t\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tvar pitch = 1.0, speed = 1.0;\n\t\t\tapp.TextToSpeech( \"Unknown command.\", pitch, speed );\t\t\n\t\t\tbreak;\n\t}<\/p>\n<p>});\n<\/script><\/p>\n<p><body onload=\"app.Start()\"><\/p>\n<h1>Robot control program active<\/h1>\n<p><\/body><br \/>\n<\/html><\/p>\n<p><\/code><\/p>\n<p>DroidScript supports two modes of development: pure JavaScript and HTML mode.   This code sample uses the HTML style of application development making it natural for web developers. Let&#8217;s tear down the code. We need to import a few script files at the top of the code.<\/p>\n<p><code><br \/>\n<html><br \/>\n<head><br \/>\n<script src='file:\/\/\/android_asset\/app.js'><\/script><br \/>\n<script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.1.3\/jquery.min.js\"><\/script><br \/>\n<\/head><br \/>\n<\/code><\/p>\n<p>When the application starts, we need to configure the serial port to a baud rate of 9600.  <\/p>\n<p><code><br \/>\n    \/\/Called after application is started.<br \/>\n    function OnStart()<br \/>\n    {<br \/>\n        app.ShowPopup( \"Robot control active\" );<br \/>\n        usb = app.CreateUSBSerial(9600);<br \/>\n    }<br \/>\n<\/code><\/p>\n<p>The following functions define our protocol for sending messages to the Arduino.  In each case, we&#8217;re just sending the appropriate character to the serial port.<\/p>\n<p><code><br \/>\nfunction forward()<br \/>\n{<br \/>\nusb.Write( \"w\" );<br \/>\n}<\/p>\n<p>function back()<br \/>\n{<br \/>\nusb.Write( \"s\" );<br \/>\n}<\/p>\n<p>function left()<br \/>\n{<br \/>\nusb.Write( \"a\" );<br \/>\n}<\/p>\n<p>function stop()<br \/>\n{<br \/>\nusb.Write( \" \" );<br \/>\n}<\/p>\n<p>function right()<br \/>\n{<br \/>\nusb.Write( \"d\" );<br \/>\n}<\/p>\n<p><\/code><\/p>\n<p>To accept keyboard input from the user, we use the following switch statement:<\/p>\n<p><code><br \/>\n$(document).keypress(function(event) {<br \/>\n\tswitch(event.charCode)<br \/>\n\t{<br \/>\n\t\tcase 119:<br \/>\n\t\t\tforward();<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase 115:<br \/>\n\t\t\tback();<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase 97:<br \/>\n\t\t\tleft();<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase 100:<br \/>\n\t\t\tright();<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase 32:<br \/>\n\t\t\tstop();<br \/>\n\t\t\tbreak;<\/p>\n<p><\/code><\/p>\n<p>DroidScript has a really easy function for text to speech too.<\/p>\n<p><code><br \/>\nvar pitch = 1.0, speed = 1.0;<br \/>\napp.TextToSpeech( \"Resistance is futile. You will be assimilated.\", pitch, speed );<br \/>\n<\/code><\/p>\n<p>\nMake sure to download DroidScript and check out the other cool features it offers.   We love to hear from our readers!  We would enjoy seeing your robots or your apps!  Leave a comment below.   All the best!\n<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Top Stories on InspiredToEducate.NET<\/strong><\/p>\n<p><strong>Learning To Code<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1319\">Easy Data Visualization with Google Charts and JavaScript<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=795\">Learn to Build Your Own Conversational Bot using ChatScript<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1285\">10 Free Resources for Learning JavaScript and HTML5<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1238\">17 Fun Tools To Teach Kids To Code by @ChrisBetcher<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=623\">Benefits of Teaching Kids To Code That No One Is Talking About<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1349\">Easy Recipes for Building Android Apps using MIT App Inventor<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=738\">12 Steps To 3D Print Your Minecraft Creations<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=849\">How to Build Your Mobile App using HTML<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1079\" target=\"_blank\">Simple Minecraft Programming Using ScriptCraftJS<\/a><\/li>\n<\/ul>\n<p><strong>Science Education<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1093\">Why hate science?<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=374\">7 ideas for creating a student centered learning environment by Paul Andersen<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1022\">Using candy to teach DNA structure<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Join the\u00a0<a href=\"https:\/\/www.facebook.com\/groups\/1529734900588516\/\" target=\"_blank\">Spark Macon Maker Space Community on Facebook<\/a><\/strong><\/p>\n\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=http%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fusing-android-javascript-and-arduino-to-control-your-robot-makered-javascript-android%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s say you want to tinker with making a robot controller on your Android device, but you don\u2019t have a lot of time to learn Java. \u00a0Perhaps you just love JavaScript and want to write Android Apps. \u00a0 In our maker education programs, we enjoy introducing students to JavaScript since [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,15,16,23,11,8],"tags":[],"_links":{"self":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1838"}],"collection":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/comments?post=1838"}],"version-history":[{"count":6,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1838\/revisions"}],"predecessor-version":[{"id":1845,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1838\/revisions\/1845"}],"wp:attachment":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/media?parent=1838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/categories?post=1838"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/tags?post=1838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}