Skip to main content

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:



Popular posts from this blog

SalsaNight

Zorgania

The Zorganian Republic has some very strange customs. Couples only wish to have female children as only females can inherit the family's wealth, so if they have a male child they keep having more children until they have a girl. If they have a girl, they stop having children. What is the ratio of girls to boys in Zorgania?
The ratio of girls to boys in Zorgania is 1:1. This might be a little counter-intuitive at first. Here are some ways of tackling this problem. 1. Monte Carlo Simulation: Although, Monte Carlo simulation does not necessarily show why the result is 1:1, it is appropriate because of the very counter-intuitive nature of the problem. At the very least, it helps us see that the result is indeed 1:1. Therefore, this is a good start.
The following R code estimates the probability of a child being a boy in Zorgania. 
couples <-100000 boycount <-0for (i in1:couples){ # 0: boywhile (sample(c(0,1),1) ==0) { boycount=boycount+1 } } probability <- boycount/(co…

Simple Launcher

A simple minimal launcher application for Android devices that shows battery percentage using lzyzsd's CirclProgress library (ArchProgress used in this case) and BroadcastReciever for battery state, Android's clock widgets, a built-in flash light switch and an app list view that can be toggled. Currently, the toggle simply filters all the app that I am working on at present. Future implementation can allow users to select their favorite apps or populate second toggle based on the most used applications.