Stories on maker education and innovation 

Home making Using Android, JavaScript, and Arduino to control your robot. #makered #javascript #android
formats

Using Android, JavaScript, and Arduino to control your robot. #makered #javascript #android

DroidScript

Let’s say you want to tinker with making a robot controller on your Android device, but you don’t have a lot of time to learn Java.  Perhaps you just love JavaScript and want to write Android Apps.   In our maker education programs, we enjoy introducing students to JavaScript since the language helps students go from idea to prototype quickly.

Consider checking DroidScript on the Google Play Store.  DroidScript enables you to quickly build simple Android apps using JavaScript.

I greatly appreciate DroidScript enabling you to edit programs directly from a Wifi connected desktop computer.  All you need is a desktop web browser.   You don’t need to install Eclipse, Java, simulators, Netbeans or anything.   You press a button in DroidScript and the app fires up a web server on your Android device.   From your web browser, you can start making new apps, exploring and running sample programs, and checking out the documentation.

What features of Android can you access using DroidScript?

  • You can use the GPS, Compass, Camera, and Accelerometer.
  • DroidScript can do basic graphics functions.
  • According to the documentation, you can send and receive emails and SMS.
  • You can control Arduino and Lego NXT.
  • On a personal project, I used DroidScript to send commands to an Arduino through serial communication.
  • You can also fire up a custom web server so that your phone can respond to HTTP requests.

I think users will appreciate the effective samples and documentation.

Docs screen

For the young programmer, hobby programmer or someone who needs some quick code duck tape on Android, DroidScript is worth checking out.  If you need help, they have an active forum community at https://groups.google.com/forum/#!forum/androidscript

Let’s Build a Robot Control program

In our previous blog post, we showed you how to build your own DIY servo robot using Arduino.   Let’s assume that the Arduino code follows the following protocol when it receives bytes on the serial port:

  • When w is received, the robot moves forward.
  • When  s is received, the robot moves backward.
  • When a is received, the robot moves left.
  • When d is received, the robot moves right.
  • When the space character is received, the robot stops all motion.

Here’s your test robot using an Arduino Nano.

Android Bot

For this robot design, I want to use my Android device and a blue tooth keyboard to remotely control the Android device.   The blue tooth keyboard helps me control the robot at a distance.  You will also need to obtain a USB to micro-USB adapter like this one to connect your Android device to Arduino.  Here’s the code needed to control the robot.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<html>
<head>
<script src='file:///android_asset/app.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<script>
    //Called after application is started.
    function OnStart()
    {
        app.ShowPopup( "Robot control active" );
        usb = app.CreateUSBSerial(9600);
    }
</script>

<script>
function forward()
{
usb.Write( "w" );
}

function back()
{
usb.Write( "s" );
}

function left()
{
usb.Write( "a" );
}

function stop()
{
usb.Write( " " );
}

function right()
{
usb.Write( "d" );
}

$(document).keypress(function(event) {
    switch(event.charCode)
    {
        case 119: //w was pressed
            forward();
            break;
        case 115: //s was pressed
            back();
            break;
        case 97: //a was pressed
            left();
            break;
        case 100: //d was pressed
            right();
            break;
        case 32: //space was pressed
            stop();
            break;
        case 49:
            var pitch = 1.0, speed = 1.0;
            app.TextToSpeech( "Resistance is futile. You will be assimilated.", pitch, speed );    
            break;
        default:
            var pitch = 1.0, speed = 1.0;
            app.TextToSpeech( "Unknown command.", pitch, speed );      
            break;
    }
     
});
</script>

<body onload="app.Start()">
<h1>Robot control program active</h1>    
</body>
</html>

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’s tear down the code. We need to import a few script files at the top of the code.

1
2
3
4
5
<html>
<head>
<script src='file:///android_asset/app.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

When the application starts, we need to configure the serial port to a baud rate of 9600.

1
2
3
4
5
6
    //Called after application is started.
    function OnStart()
    {
        app.ShowPopup( "Robot control active" );
        usb = app.CreateUSBSerial(9600);
    }

The following functions define our protocol for sending messages to the Arduino. In each case, we’re just sending the appropriate character to the serial port.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function forward()
{
usb.Write( "w" );
}

function back()
{
usb.Write( "s" );
}

function left()
{
usb.Write( "a" );
}

function stop()
{
usb.Write( " " );
}

function right()
{
usb.Write( "d" );
}

To accept keyboard input from the user, we use the following switch statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$(document).keypress(function(event) {
    switch(event.charCode)
    {
        case 119:
            forward();
            break;
        case 115:
            back();
            break;
        case 97:
            left();
            break;
        case 100:
            right();
            break;
        case 32:
            stop();
            break;

DroidScript has a really easy function for text to speech too.

1
2
var pitch = 1.0, speed = 1.0;
app.TextToSpeech( "Resistance is futile. You will be assimilated.", pitch, speed );

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

 

Top Stories on InspiredToEducate.NET

Learning To Code

Science Education

 

Join the Spark Macon Maker Space Community on Facebook

 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
No Comments  comments 
© Inspired To Educate
credit