Let's Make Robots!

Making Smart robot

Hi all,

My plans are as follow: I want to make my RC car into fully automated version.

Top view of car: http://i1265.photobucket.com/albums/jj519/soni991/RC%20Car/IMG_6682.jpg

Front view of car: http://i1265.photobucket.com/albums/jj519/soni991/RC%20Car/IMG_6683.jpg

Back view of car: http://i1265.photobucket.com/albums/jj519/soni991/RC%20Car/IMG_6684.jpg

Bottom view of car: http://i1265.photobucket.com/albums/jj519/soni991/RC%20Car/IMG_6685.jpg

The function involves (I give it as Version 1.0)

1) Steer itself throughout the house without bumping into any object. 

2) when power goes down then take itself to the powerport for charging. also it should check the battery power charge if it is sufficient and not overcharge the battery.

3) Goes on sleepmode after every half an hr of operation and then powerbackon after 1 hr ( that is changeable) .

these 3 basic function i need follow. 

I have RC car with 2WD. The steering wheels in front are of angular movement(it moves only to certain angle) : http://i1265.photobucket.com/albums/jj519/soni991/RC%20Car/IMG_6672.jpg

What gadgets i have available :

Picture: http://i1265.photobucket.com/albums/jj519/soni991/RC%20Car/IMG_6689.jpg


1) MCU = Mega 2560.(not in picture)

2) HC-SR04 1pc

3) US-100 1pc

4) SD memmory card using SPI protocol 2Pcs

5)TCRT5000 2PC

6) Microphone 1pc

7) Light Sensor 2pc

8) Audio amplifier 1pc

9) Accelrometer 1pc 

10) Bluetooth 1pcs

11) USD to TTL 2pc

12) NRF 24L01 3pc

13) 10Pin to 6pin  AVRisp 2Pcs

14) logo sensor 1pc

15) CP 2102 USB to TTL 1pc (not in picture)

16) lot of jumper wires and solderless board. not in picture

17) HC-SR501.

More parts are coming so i would update those list.

So I am good to go with parts, how to start building smart robot with clean code.

Please can anyone tell me how do i configure the steering wheel of this car on arduino? i have put all the pics here: http://s1265.photobucket.com/albums/jj519/soni991/RC%20Car/ 

any one suggest any new parts please let me know so i can purchase it and use on this project.

1) First thing i need is suggestion about what parts i should include for the basic function in Version 1.

2) Second the Programing Code written from scratch with everyone contribution.

Hope i can have feedback from taleted people online.






Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

If i am using the feed from ultrasonic "Lultra" to detect the distance of an object and if i write the code as below. Would it over lap the conditioning ?

if (Lultra <=40){fast speed}

if (Lultra <=20){stop}

would the <=40 line would overlap the speed in <=20 when the sensor read the distance 10

1) how do i setup the if command that the condition remain in between range: here the range is 40 to 20 & another is 20 to 0  fast and stop speed respectively.

As you've written it, both the {fast speed} and {stop} sections will be executed when Lultra is <= 20 (Meaning that it, depending on implementation, will likely not move, but it's best to only execute one).

The simplest change would be to first check if Lultra is <= 20, and if not, check if it is <= 40. Thus:

if (Lultra <= 20
  if (Lultra <= 40)
    {fast speed}

Although it's not in the question you asked, I notice this means that with an Lultra value > 40, neither the stop nor fast speed sections will be executed. Is this intentional?

Hi Janvar, Thanks for the code, i have another related question.

if i am reading from two ultrasonic which is next to each other in certain angle which act like an imaginary cone shape path and hence any object come in between the path would be avoided.

this is an example: 

i have two Lultrasonic named Lultra & Rultra, D2:P2 & D1:P1 are the command for the motor conected to wheels.

if (Lultra >= 41 and Rultra >= 41)


           digitalWrite(D2 , HIGH); analogWrite(P2, 255);

           digitalWrite(D1 , HIGH); analogWrite(P1, 255);


My question is would this command only follwed if the two condition matched ( like the car with sensor move to a flat wall) at given moment, would it also be follwed if one is >=40 and other is not.

Yes, both operands must evaluate to true for the conditional section to be executed. As The Benjaneer says, you'll want '&&' instead of 'and'.

If you wanted it to execute if either of the operands are true (i.e. if Lultra or Rultra or both at once are >= 41), the symbol for logical OR is '||'.

I think you need to do this:

if (Lultra > 40 && Rultra > 40)


           digitalWrite(D2 , HIGH); analogWrite(P2, 255);

           digitalWrite(D1 , HIGH); analogWrite(P1, 255);

 Then if Both values are greater than 40 (or you could do >=41 but by habit i did > 40) it will run the digtialWrite code. If one is above 40 but the other isnt then it wont run it. ( '&&' means and, whereas '&' means bitwise adding, also i don't think 'and' is an operator in arduino but i could be wrong...)

Refernce Page for you:


I was just following some comment on Shout box and thought this might be useful:

void RollDistance()                                 // The Rolling Averge Calculator
  Total = Total - IRAverage[ThisRead];             // Minus the oldest read
  IRAverage[ThisRead] = analogRead(IRPin);  // Replaces the oldest Read
  Total = Total + IRAverage[ThisRead];           // Adds the New Read to the total
  ThisRead ++;                                           // Increment the Reading number
  if (ThisRead >= 5)                                     // Loops the Reading Number
    ThisRead = 0;
  float volts = (Total / 5)*0.0048828125;  // Converts it back into a voltage, volts (5) / Analog steps (1024)
  Distance = 65 * pow(volts, -1.1);       // Volts to the power of -1.1, times 65 gives a rough distance in cm

I use it with an Sharp IR, but it should be similar, this way it just add's the lastest reading, saves timing looping 5 times before deciding what to do.

Also, If you use this, you need to Prime it: Call this function 5 times(or how ever many readings your averaging) from the setup loop  to make the average have data (cheers birdmun for reminding me to add this)

Take a given number of readings and add them to a total as you go. When you are done, divide by the total number of readings.

Well i had changes the structure of the RC car and practically using the minature one for beigining .

I want to take the reading of my ultrasonic.

i want to take the average reading of 10-15 counts at once , how do i do it in my below code.

at moment reading for my Rultra i am not using, also i didnt setup my another motor D1,


#include <Ultrasonic.h>


  #define TRIGGER_PIN  24

  #define ECHO_PIN     25

  #define TRIGGER  22

  #define ECHO     23



  Ultrasonic leftultrasonic(TRIGGER_PIN, ECHO_PIN);

  Ultrasonic rightultrasonic(TRIGGER, ECHO);


  int D2 = 42;

  int P2 = 44;

  int D1 = 38;

  int P1 = 40;



  void setup()









  void loop()




    // left ultrasonic

    float Lultra;

    long Lmicrosec = leftultrasonic.timing();

    Lultra = leftultrasonic.convert(Lmicrosec, Ultrasonic::CM);

    Serial.print(" LCM: ");


    // right ultrasonic

    float Rultra;

    long Rmicrosec = rightultrasonic.timing();

    Rultra = rightultrasonic.convert(Rmicrosec, Ultrasonic::CM);

    Serial.print(" RCM: ");




   if (Lultra <=40 or Lultra >=20)


           int val = ((Lultra*3.875)+100);

           digitalWrite(D2 , HIGH);analogWrite(P2, val);



hi all i am having trouble writing code for the steering.

the code is below:


  #include <Ultrasonic.h>

  #define TRIGGER_PIN  5

  #define ECHO_PIN     12

  #define TRIGGER  10

  #define ECHO     11

  int D1 = 6;

  int P1 = 7;

  int contactA; // contact A is the sensor which turns signal to '0' when the wheel is all the way to left.

  int contactB; // contact B is the sensor which turns signal to '0' when the wheel is all the way to left.

  int D2 = 8;

  int P2 = 9;


Ultrasonic front(TRIGGER_PIN, ECHO_PIN);

Ultrasonic rear(TRIGGER, ECHO);



  void setup()





    digitalWrite(2,HIGH); //<-- this turns the internal pull-up on

    digitalWrite(3,HIGH); //<-- this turns the internal pull-up on



    pinMode(D2 , OUTPUT);//this is for the front motor

    pinMode(P2 , OUTPUT);//this is for the front motor PWM

    pinMode(D1 , OUTPUT);//this is for the rear motor

    pinMode(P1 , OUTPUT);//this is for the rear motor PWM


  void loop()


{ // steering angle detection

    contactA=digitalRead(2); //yellow

    contactB=digitalRead(3); //blue


  Serial.print(" A ");


  Serial.print(" B ");


   {if (contactA == 1 and contactB ==0)





    if (contactA == 0 and contactB ==1)








  //front distance calculation


  float cmMsec, inMsec;

  long microsec = front.timing();


    cmMsec = front.convert(microsec, Ultrasonic::CM);


  Serial.print(", CM: ");



  // rear distance calculation


  float RcmMsec, RinMsec;

  long Rmicrosec = rear.timing();


    RcmMsec = rear.convert(Rmicrosec, Ultrasonic::CM);


  Serial.print(", RCM: ");



  /* this is for the movement upon detection logic*/


  if (cmMsec >= 45) // if ultra sonic see distance greater then 45cm go forward


    int value(70);


    analogWrite(P1, value);



  if (cmMsec < 25) // if the Front Ultrasonic detects distance less then 25cm then Brakes.


    if (RcmMsec > 25) // if the back distance is more then 25cm then go back


      int value(70);



        analogWrite(P1, value);




     if (RcmMsec < 10) //if the back distance is less then 10cm then stop


              analogWrite(P1 , 0);

          if (  //here i want to write command for the steering wheel so it can steer to left with steering detection and then go ahead)      



Is this the bit of code you are having trouble with? Can you describe the problem?

   {if (contactA == 1 and contactB ==0)





    if (contactA == 0 and contactB ==1)






Just based on a quick look, this code will detect if the steering swtiches have detected that the steering wheel is all the way left or all the way right. I assume D2 sets the direction for the motor to go, and P2 sets the speed. You have analogWrite(P2,0), which will stop the motor. Is that what you want?