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).
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).
« Previous 1 2 3 4 Next »
Buy this article as PDF
Pages: 6
(incl. VAT)