A Simple Bluetooth Controlled Vehicle with Line Following and Obstacle Avoiding Capability.

The vehicle can be manually controlled from any Bluetooth enabled device. If commanded, it can also follow any dark line on a bright surface. It also has obstacle avoiding capability through its ultrasonic distance sensor which prevents any collision with any obstacle in front of the vehicle.

 

Code for Manual Bluetooth Control from Android Application

Arduino Code:
  1.  
  2. //Works only with Uno/Diavolino
  3. //Does not work with Leonardo/Duino 328 Custom
  4.  
  5. int dataIn = 'S'; //Character/Data coming from the phone.
  6.  
  7. int LeftMotorPlus = 8;
  8. int LeftMotorMinus = 9;
  9. int LeftMotorEnable = 10;
  10.  
  11. int RightMotorPlus = 12;
  12. int RightMotorMinus = 13;
  13. int RightMotorEnable = 11;
  14.  
  15. int determinant; //Used in the check function, stores the character received from the phone.
  16. int det; //Used in the loop function, stores the character received from the phone.
  17.  
  18. void setup()
  19. {
  20. //*************NOTE: If using Bluetooth Mate Silver use 115200 btu
  21. // If using MDFLY Bluetooth Module use 9600 btu
  22. Serial.begin(9600); //Initialize serial communication with Bluetooth module at 115200 btu.
  23. //while (!Serial) {
  24. // ; // wait for serial port to connect. Needed for Leonardo only
  25. // }
  26. pinMode(LeftMotorPlus, OUTPUT);
  27. pinMode(LeftMotorMinus, OUTPUT);
  28. pinMode(LeftMotorEnable, OUTPUT);
  29.  
  30. pinMode(RightMotorEnable, OUTPUT);
  31. pinMode(RightMotorMinus , OUTPUT);
  32. pinMode(RightMotorPlus , OUTPUT);
  33.  
  34.  
  35. }
  36.  
  37. void loop()
  38. {
  39.  
  40. det = check();
  41. while (det == 'F') //if incoming data is a F, move forward
  42. {
  43.  
  44. digitalWrite(LeftMotorEnable, HIGH);
  45. digitalWrite(RightMotorEnable, HIGH);
  46.  
  47. digitalWrite(LeftMotorPlus, HIGH);
  48. digitalWrite(LeftMotorMinus, LOW);
  49.  
  50.  
  51. digitalWrite(RightMotorPlus, LOW);
  52. digitalWrite(RightMotorMinus, HIGH);
  53.  
  54.  
  55.  
  56. det = check();
  57. }
  58. while (det == 'B') //if incoming data is a B, move back
  59. {
  60. digitalWrite(LeftMotorEnable, HIGH);
  61. digitalWrite(RightMotorEnable, HIGH);
  62.  
  63. digitalWrite(LeftMotorPlus, LOW);
  64. digitalWrite(LeftMotorMinus, HIGH);
  65.  
  66.  
  67. digitalWrite(RightMotorPlus, HIGH);
  68. digitalWrite(RightMotorMinus, LOW);
  69.  
  70.  
  71.  
  72. det = check();
  73. }
  74.  
  75. while (det == 'L') //if incoming data is a L, move wheels left
  76. {
  77. digitalWrite(LeftMotorEnable, HIGH);
  78. digitalWrite(RightMotorEnable, HIGH);
  79.  
  80. digitalWrite(LeftMotorPlus, HIGH);
  81. digitalWrite(LeftMotorMinus, LOW);
  82.  
  83.  
  84. digitalWrite(RightMotorPlus, HIGH);
  85. digitalWrite(RightMotorMinus, LOW);
  86.  
  87.  
  88. det = check();
  89. }
  90. while (det == 'R') //if incoming data is a R, move wheels right
  91. {
  92.  
  93. digitalWrite(LeftMotorEnable, HIGH);
  94. digitalWrite(RightMotorEnable, HIGH);
  95.  
  96. digitalWrite(LeftMotorPlus, LOW);
  97. digitalWrite(LeftMotorMinus, HIGH);
  98.  
  99.  
  100. digitalWrite(RightMotorPlus, LOW);
  101. digitalWrite(RightMotorMinus, HIGH);
  102.  
  103.  
  104.  
  105.  
  106. det = check();
  107. }
  108.  
  109.  
  110.  
  111.  
  112. while (det == 'S') //if incoming data is a S, stop
  113. {
  114. digitalWrite(LeftMotorEnable, LOW);
  115. digitalWrite(RightMotorEnable, LOW);
  116.  
  117. det = check();
  118. }
  119.  
  120. while (det == 'U') //if incoming data is a U, turn ON front lights
  121. {
  122. //digitalWrite(pinfrontLights, HIGH);
  123. det = check();
  124. }
  125.  
  126. while (det == 'u') //if incoming data is a u, turn OFF front lights
  127. {
  128. // digitalWrite(pinfrontLights, LOW);
  129. det = check();
  130. }
  131. while (det == 'W') //if incoming data is a W, turn ON back lights
  132. {
  133. //digitalWrite(pinbackLights, HIGH);
  134. det = check();
  135. }
  136. while (det == 'w') //if incoming data is a w, turn OFF back lights
  137. {
  138. //digitalWrite(pinbackLights, LOW);
  139. det = check();
  140. }
  141. while (det == 'V') //if incoming data is a V, turn ON Horn
  142. {
  143. //digitalWrite(pinHorn, HIGH);
  144. det = check();
  145. }
  146. while (det == 'v') //if incoming data is a v, turn OFF Horn
  147. {
  148. //digitalWrite(pinHorn, LOW);
  149. det = check();
  150. }
  151. while (det == 'X') //if incoming data is a V, turn ON Horn
  152. {
  153. //digitalWrite(pinHorn, HIGH);
  154. det = check();
  155. }
  156. while (det == 'x') //if incoming data is a v, turn OFF Horn
  157. {
  158. // digitalWrite(pinHorn, LOW);
  159. det = check();
  160. }
  161. }
  162.  
  163. int check()
  164. {
  165. if (Serial.available() > 0) //Check for data on the serial lines.
  166. {
  167. dataIn = Serial.read(); //Get the character sent by the phone and store it in 'dataIn'.
  168. if (dataIn == 'F')
  169. {
  170. determinant = 'F';
  171. }
  172. else if (dataIn == 'B')
  173. {
  174. determinant = 'B';
  175. }
  176. else if (dataIn == 'L')
  177. {
  178. determinant = 'L';
  179. }
  180. else if (dataIn == 'R')
  181. {
  182. determinant = 'R';
  183. }
  184. else if (dataIn == 'I')
  185. {
  186. determinant = 'I';
  187. }
  188. else if (dataIn == 'J')
  189. {
  190. determinant = 'J';
  191. }
  192. else if (dataIn == 'G')
  193. {
  194. determinant = 'G';
  195. }
  196. else if (dataIn == 'H')
  197. {
  198. determinant = 'H';
  199. }
  200. else if (dataIn == 'S')
  201. {
  202. determinant = 'S';
  203. }
  204.  
  205. else if (dataIn == 'U')
  206. {
  207. determinant = 'U';
  208. }
  209. else if (dataIn == 'u')
  210. {
  211. determinant = 'u';
  212. }
  213. else if (dataIn == 'W')
  214. {
  215. determinant = 'W';
  216. }
  217.  
  218. else if (dataIn == 'w')
  219. {
  220. determinant = 'w';
  221. }
  222. }
  223. return determinant;
  224. }
  225.