Second tutorial of Convert your RC car in a Smart Car with Arduino and Android

I create a new topic that shows how to reconvert an old RC car in a smart car using an Arduino UNO and Bluetooth connection. Before make this project I will create some easy and interesting tutorials that will help you to familiarise with create apps for Android and develop programs for Arduino.

The second lesson is controlling a DC Motor's rotation direction and his velocity with an application created for Android and an Arduino UNO.

Material Needed

  • DC Motor.
  • Protoboard.
  • 4 NPN Transistors.
  • 2 PNP Transistors.
  • Arduino UNO.
  • Modul Bluetooth HC-05 or HC-06.
  • SmartPhone.

The Android App it was created using appinventor.

Esquematic Circuit

Circuit

The link below shows the results:

// Program to control velocity and rotation sense of a DC Motor


#define clockWise 5 
#define antiClockWise 6


String state = ""; //Save data that come from Bluetooth
String lastState= "";
int inChar = 0;
char pwmChar[3];
int i = 0;
int pwm = 0, lastPwm=0;
void setup()
{
  Serial.begin(9600);
  analogWrite(clockWise,0);
  analogWrite(antiClockWise,0);
}
 
void loop()
{
  while (Serial.available()>0)  { // While there are data come frome bluetooth...
     inChar = Serial.read(); // Read data
     pwmChar[i]= (char)inChar; //Save data to array type char
     state += pwmChar[i]; //Save chars in a String
     Serial.println(pwmChar[i]);
     i++;
     
     delay(1);
  }
  if (lastState != state) // Control when to write the PWM 
  {
      pwm = state.toInt(); // Convert the String to a int value
      if (pwm >= 1){ // anticlockwise sense
        analogWrite(clockWise,0);
        analogWrite(antiClockWise,pwm); // Send the value to PWM Pin
      }
      else if (pwm <= -1){ // clockwise sense
        analogWrite(antiClockWise,0);
        analogWrite(clockWise,-pwm);
      }
      else{ // Stop DC Motor
        analogWrite(clockWise,0);
         analogWrite(antiClockWise,0);
      }
    
      // Reset variables value
      i = 0;
      state=""; 
      inChar =0;
      lastPwm = pwm;
  }
}