The previous attempt was in shambles due to the lack of proper products. This time, I got myself a Sainsmart L293D motor driver (actually a cloned version of the ever so popular Adafruit L293D driver) and a handy four wheel drive chassis. Hereupon, the only adjustment required was the use of analog pins as digital pins. This is the consequence of the motor driver using up all the digital pins and leaving no pins for the Bluetooth shield. I also hooked up the Arduino to a USB power bank.
activity_main.xml
In a nutshell, the Android device sends Char type to Arduino which is used to rotate the motors to maneuver towards a desired direction.
Forward: 0
Reverse: 1
Stop: 2
Left: 3
Right: 4
The car turns left and right by implementing skid steering.
Tools used:
~ Arduino Uno
~ 4WD Chassis
~ SainSmart motor driver (L293D)
~ Sunkee 30ft Bluetooth Module
~ Anker 15000 mAh power bank
Android:
MainActivity.java1 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | package com.example.androidcar; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import android.app.Activity; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private boolean mConnected; private BluetoothSocket mBluetoothSocket; private OutputStream mOutputStream; @Override protected void onCreate(Bundle savedInstanceState) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button bluetoothConnect = (Button) findViewById(R.id.button1); final Button buttonDisconnect = (Button) findViewById(R.id.button2); final Button forward = (Button) findViewById(R.id.button3); final Button reverse = (Button) findViewById(R.id.button4); final Button left = (Button) findViewById(R.id.button5); final Button right = (Button) findViewById(R.id.button6); bluetoothConnect.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { //hard coded macAddress String macAddress = "20:13:06:26:10:39"; if (connect(macAddress)) { showToast("Connected"); } } catch (final Exception e) { showToast(e.getMessage()); } } }); buttonDisconnect.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { disconnect(); showToast("Disconnected"); } }); forward.setOnTouchListener(new View.OnTouchListener() { @SuppressWarnings("deprecation") public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE) { forward.setBackgroundColor(Color.GREEN); // myButton.setText("Pressed!"); try { send("0"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } if (event.getAction() == MotionEvent.ACTION_UP) { try { send("2"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } forward.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default)); return false; } return true; } }); reverse.setOnTouchListener(new View.OnTouchListener() { @SuppressWarnings("deprecation") public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE) { reverse.setBackgroundColor(Color.RED); // myButton.setText("Pressed!"); try { send("1"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } if (event.getAction() == MotionEvent.ACTION_UP) { reverse.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default)); try { send("2"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } return true; } }); left.setOnTouchListener(new View.OnTouchListener() { @SuppressWarnings("deprecation") public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE) { left.setBackgroundColor(Color.BLUE); // myButton.setText("Pressed!"); try { send("3"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } if (event.getAction() == MotionEvent.ACTION_UP) { try { send("2"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } left.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default)); return false; } return true; } }); right.setOnTouchListener(new View.OnTouchListener() { @SuppressWarnings("deprecation") public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE) { right.setBackgroundColor(Color.BLUE); // myButton.setText("Pressed!"); try { send("4"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } if (event.getAction() == MotionEvent.ACTION_UP) { try { send("2"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } right.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default)); return false; } return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override protected void onPause() { disconnect(); super.onPause(); } private void showToast(final String hint) { Context context = getApplicationContext(); if (context != null) { Toast.makeText(context, hint, Toast.LENGTH_SHORT).show(); } } private boolean connect(final String remoteMacAddress) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException { if (mConnected) { return true; } // Check that Bluetooth adapter exists. BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null) { showToast("Bluetooth adapter not found"); return false; } // If Bluetooth adapter exists but it is turned off then turn it on and // exit. if (!bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, 1); return false; } // Connection. BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(remoteMacAddress); Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); mBluetoothSocket = (BluetoothSocket) m.invoke(bluetoothDevice, 1); mBluetoothSocket.connect(); // Init output stream. mOutputStream will be used in send() method. mOutputStream = mBluetoothSocket.getOutputStream(); mConnected = true; return true; } private void disconnect() { if (mConnected) { try { if (mBluetoothSocket != null) { mBluetoothSocket.close(); mBluetoothSocket = null; } } catch (IOException e) { mBluetoothSocket = null; } } mConnected = false; } private void send(final String message) throws IOException { if (mConnected) { if (mBluetoothSocket != null) { mOutputStream.write(message.getBytes()); } } } } |
activity_main.xml
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 76 77 78 79 80 81 82 83 84 85 86 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f1faf3" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button3" android:layout_alignParentTop="true" android:layout_alignRight="@+id/button3" android:text="Connect to Arduino BT" android:textColor="#237031" android:textStyle="bold" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignRight="@+id/button1" android:text="Disconnect" android:textColor="#ff0000" android:textStyle="bold" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button2" android:layout_alignLeft="@+id/button3" android:layout_alignTop="@+id/button2" android:layout_toLeftOf="@+id/button4" android:textIsSelectable="true" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button4" android:layout_alignLeft="@+id/button3" android:layout_below="@+id/button3" android:text=" Left " android:textSize="30sp" android:textStyle="bold" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button5" android:layout_alignRight="@+id/button3" android:layout_alignTop="@+id/button5" android:text="Right" android:textSize="30sp" android:textStyle="bold" /> <Button android:id="@+id/button4" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_above="@+id/button2" android:layout_centerHorizontal="true" android:text="Reverse" android:textSize="50sp" android:textStyle="bold" /> <Button android:id="@+id/button3" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button4" android:layout_below="@+id/button1" android:layout_marginTop="15dp" android:text="Forward" android:textSize="50sp" android:textStyle="bold" /> </RelativeLayout> |
Arduino
//Project: Android RC Car //Author: Ayush Subedi #include <AFMotor.h> //import Adafruit Motor library #include <SoftwareSerial.h>// import the serial library SoftwareSerial newPorts(15, 17); // RX =15= A1, TX=17=A3 AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1, 1KHz pwm AF_DCMotor motor2(2, MOTOR12_1KHZ); // create motor #2, 1KHz pwm AF_DCMotor motor3(3, MOTOR34_1KHZ); // create motor #3, 1KHz pwm AF_DCMotor motor4(4, MOTOR34_1KHZ); // create motor #4, 1KHz pwm void setup() { newPorts.begin(9600); motor1.setSpeed(255); // set the speed to 200/255 motor2.setSpeed(255); // set the speed to 200/255 motor3.setSpeed(255); // set the speed to 200/255 motor4.setSpeed(255); // set the speed to 200/255 } void loop() { while (newPorts.available() > 0) { char ch = newPorts.read(); newPorts.println(newPorts.read()); executeReceivedCommand(ch); } } void executeReceivedCommand(char command) { switch (command) { //Forward case '0': motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(FORWARD); motor4.run(FORWARD); break; //Reverse case '1': motor1.run(BACKWARD); motor2.run(BACKWARD); motor3.run(BACKWARD); motor4.run(BACKWARD); break; //Left : skid steering case '3': motor1.run(FORWARD); motor4.run(FORWARD); motor2.run(RELEASE); motor3.run(RELEASE); break; //Right : skid steering case '4': motor2.run(FORWARD); motor3.run(FORWARD); motor1.run(RELEASE); motor4.run(RELEASE); break; //Stall case '2': motor1.run(RELEASE); motor2.run(RELEASE); motor3.run(RELEASE); motor4.run(RELEASE); break; } }
In a nutshell, the Android device sends Char type to Arduino which is used to rotate the motors to maneuver towards a desired direction.
Forward: 0
Reverse: 1
Stop: 2
Left: 3
Right: 4
The car turns left and right by implementing skid steering.