Pages

AndroidRCCar



This was a tough one. First of all, I made a mistake by opting out for cheaper parts. I should have gone for a complete motor shield instead of just a chip (L298). Also, currently the project is incomplete due to the lack of patience in dealing with the chip, and the lack of a proper chassis . However, I just did not feel like throwing everything out of the window. This post should help me later when I get time to gather all the parts I need.

The idea of the project was to control a RC car using an android device. At this point, I am 80% done. Well, it depends on how one defines "control". Since I have control over one only motor at this time, the car can go straight and reverse. The rest of the project is a cake once I get the motor shield.

Android:  

Uses "Forward" and "Reverse" buttons along with accelerometer to account for direction. So, to go forward in left direction, the user will have to push the "Forward" button and tilt the device left. Connecting to Arduino BT is explained in my previous blog (here). The motors rotate as long as the buttons are being pressed (They stop once the button is released).  



"Forward" button code:


 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
forward.setOnTouchListener(new View.OnTouchListener() {
 @SuppressWarnings("deprecation")
 public boolean onTouch(View v, MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_MOVE)
  {
   forward.setBackgroundColor(Color.GREEN);
   
   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;
 }

});

Now, once I get the shield I would have to come up with some "x" to be send over "send(x)" depending on accelerometer readings and program it appropriately in arduino side. That is not too bad at all. 

Arduino:

//Project: Android RC Car

#include <SoftwareSerial.h>// import the serial library
SoftwareSerial newPorts(6, 7); // RX, TX
int ENA = 9;
int IN1 = 10;
int IN2 = 11;

void setup()
{
  newPorts.begin(9600);
  pinMode (ENA, OUTPUT);
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
}

void loop()
{
  while (newPorts.available() > 0)
  {
    char ch = newPorts.read();
    executeReceivedCommand(ch);
  }
}

void executeReceivedCommand(char command)
{
  switch (command)
  {
    //Forward
    case '0':
    digitalWrite (IN1, HIGH);
    digitalWrite (IN2, LOW);
    analogWrite(ENA, 250);     
    digitalWrite(ENA, HIGH);  
    break;
    
    //Reverse
    case '1':
    digitalWrite (IN1, LOW);
    digitalWrite (IN2, HIGH);
    analogWrite(ENA, 250);
    digitalWrite(ENA, HIGH);  
    break;     

    //Stall
    case '2':
    digitalWrite (IN1, LOW);
    digitalWrite (IN2, LOW);
    break;
    
    //Forward while going left
    //case '3':
    //and so on
    
  }
}

The L298:

I do not know who the OP is but the following explanation was the most helpful thing on the internet regarding the L298. Here is the link.  Also, check out this youtube video.


Motor Demo:




Car Demo:



Ayush Subedi

Coffee Connoisseur