Make an Android phone app to control your littleBits projects

Arduino Program

Listing 1 [3] shows the program that controls serial communication through the Bluetooth module. Line 2 sets Bluetooth communications to ports 10 and 11. Most Bluetooth modules default to 9600 baud, so we also set this value in line 16. The DC motors on pins d5 and d9 need to be configured in the Arduino setup function as outputs (lines 11 and 12). After the motors are defined as outputs, you can start and stop them with the digitalWrite function, as seen in lines 29 and 30.

Listing 1

paddleboat.ino

01 #include <SoftwareSerial.h>
02 SoftwareSerial BT(10, 11);
03 //
04 // connect BT module TX to D10
05 // connect BT module RX to D11
06 // connect BT VCC to VCC, GND to GND on the proto module
07
08 void setup()
09 {
10   // set digital pin to control as an output
11   pinMode(5, OUTPUT);
12   pinMode(9, OUTPUT);
13    digitalWrite(9,0);
14    digitalWrite(5,0);
15   // set the data rate for the SoftwareSerial port
16   BT.begin(9600);
17   // Send test message to other device
18   BT.println("Hello from Arduino");
19 }
20 char a; // stores incoming character from other device
21 void loop()
22 {
23   if (BT.available())
24   // if text arrived in from BT serial...
25   {
26     a=(BT.read());
27     if (a=='l')
28     {
29       digitalWrite(9,0);
30       digitalWrite(5,1);
31       BT.println("turning left");
32     }
33     if (a=='r')
34     {
35       digitalWrite(9,1);
36       digitalWrite(5,0);
37       BT.println("turning right");
38     }
39      if (a=='g')
40     {
41       digitalWrite(9,1);
42       digitalWrite(5,1);
43       BT.println("going");
44     }
45      if (a=='s')
46     {
47       digitalWrite(9,0);
48       digitalWrite(5,0);
49       BT.println("stopping");
50     }
51     if (a=='?')
52     {
53       BT.println("Send 'l' to turn left");
54       BT.println("Send 'r' to turn right");
55       BT.println("Send 'g' to go");
56       BT.println("Send 's' to stop");
57     }
58     // you can add more "if" statements with other characters to add more commands
59   }
60 }

Characters sent from an Android phone via Bluetooth controlled the paddleboat as follows:

  • l – turn left
  • r – turn right
  • g – go forward
  • s – stop
  • ? – show commands

Bluetooth Phone Setup

The next step is to ensure that the Android phone finds and pairs with the Bluetooth serial module. We tested a few free Android-based Bluetooth terminal emulation programs and ended up using a nice package called Bluetooth Terminal by Querty [4].

Within the Android Bluetooth Terminal program, select Connect a device – Secure. The phone then scans for devices, and lets you select your Arduino device. Ours was called HC-06. After you select your device, a pairing request page comes up (Figure 5) where you need to enter the device's PIN. (A typical PIN is 1234, but check your documentation if it doesn't work. Another common default PIN is 0000).

Figure 5: Bluetooth pairing request.

After the phone and Arduino are paired, the red light on the Bluetooth module stops blinking and stays on. At this point, you can enter g, s, l, or r on the phone to control the paddleboat (Figure 6).

Figure 6: Control the paddleboat with a terminal program on your smartphone.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Build cool stuff with littleBits, a Pi, and some Lego Bricks

    Connect the littleBits Arduino module to a Raspberry Pi and you open a world of projects and a world of fun.

  • Control your littleBits projects with a homemade wireless remote

    Make a custom handheld wireless remote control with littleBits Wireless Transmitter and Receiver bits and slider, knob, button, or toggle bits.

  • Create a remote control motorized robot arm

    Use an Arduino Mega to manage a 4-motor robot arm and a 2-motor car chassis. The MIT App Inventor package was used to create an Android app that controlled-the robot.

  • New Products

    What's new in the SBC, IoT, and maker realm

  • Power to You

    Small-board computers (SBCs) are getting more and more powerful. The Raspberry Pi 3 (RPi3), which just came out in February of this year, is a case in point. This latest in the Rasp Pi line now has four cores running at 1200MHz with 1GB of RAM and on-board WiFi and Bluetooth. If you need even more power in a small computer, however, you are not left wanting. In this issue, we look at three SBCs with more cores, more memory, more ports, and more possibilities.