After waiting a long time to be able to make one obstacle avoiding robot myself, with as little expenses as possible, after all to make one successful project it should be cost-effective, but again not too professional, i.e easy to develop and deploy, I finallly build this obstacle avoiding vehicle project with Arduino.

 

 

 

Arduino Code:
  1. // My First Obstacle Avoiding Bot With IR Led-Photodiode Sensor
  2. //Created somewhere around 2011
  3. #include "LiquidCrystal.h"
  4. //This library contains the functions for the LCD Module Interface
  5.  
  6. //Now let's define the IR based Digital Obstacle Sensors
  7. int aFS = A0; int aLS = A1; int aRS = A2;
  8. //Analog IR Sensor and Photodiode Modules Connection
  9. int FSv = 0, LSv = 0, RSv = 0;
  10. //variables to store sensor value states.
  11. //Set, if respective sensor detects an obstacle
  12.  
  13. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
  14. //Define the pins to which the LCD Module is connected
  15.  
  16. void setup()
  17. {
  18.  
  19. lcd.begin(16, 2);
  20. //Initialize the 16X2 LCD Module
  21. pinMode(8, OUTPUT);
  22. pinMode(9, OUTPUT);
  23. pinMode(10, OUTPUT);
  24. pinMode(12, OUTPUT);
  25.  
  26. }
  27.  
  28. void move_forward()
  29. {
  30. digitalWrite(8, LOW);
  31. digitalWrite(9, HIGH);
  32.  
  33. digitalWrite(12, LOW);
  34. digitalWrite(10, HIGH);
  35.  
  36. lcd.setCursor(0, 0);
  37. lcd.print("No Obstruction ");
  38. //Print Moving Forward in The LCD
  39. lcd.setCursor(0, 1);
  40. lcd.print(" Moving Forward");
  41. }
  42.  
  43. void move_reverse()
  44.  
  45. {
  46. digitalWrite(9, LOW);
  47. digitalWrite(8, HIGH);
  48.  
  49. digitalWrite(10, LOW);
  50. digitalWrite(12, HIGH);
  51.  
  52. lcd.setCursor(0, 0);
  53. lcd.print("Obstacle Detected");
  54. lcd.setCursor(0, 1);
  55. lcd.print(" Taking Reverse ");
  56.  
  57. }
  58.  
  59. void turn_left()
  60. {
  61. digitalWrite(9, LOW);
  62. digitalWrite(8, LOW);
  63.  
  64. digitalWrite(12, LOW);
  65. digitalWrite(10, HIGH);
  66.  
  67. lcd.setCursor(0, 0);
  68. lcd.print("Avoiding Obstacle");
  69. lcd.setCursor(0, 1);
  70. lcd.print(" Turning Right ");
  71. }
  72.  
  73. void turn_right()
  74. {
  75. digitalWrite(9, HIGH);
  76. digitalWrite(8, LOW);
  77.  
  78. digitalWrite(12, LOW);
  79. digitalWrite(10, LOW);
  80.  
  81. lcd.setCursor(0, 0);
  82. lcd.print("Avoiding Obstacle");
  83. lcd.setCursor(0, 1);
  84. lcd.print(" Turning Left ");
  85. }
  86.  
  87. void loop() {
  88.  
  89. int FS, LS, RS;
  90. FSv = analogRead(aFS);
  91. LSv = analogRead(aLS);
  92. RSv = analogRead(aRS);
  93.  
  94. if (FSv < 950) //Set Condition of Front Sensor,
  95. //i.e Front sensor is HIGH when an obstacle is detected in Front of the Bot
  96. FS = HIGH;
  97. else
  98. FS = LOW;
  99.  
  100. if (LSv < 950) //Set condition for Left Sensor
  101. LS = HIGH;
  102. else
  103. LS = LOW;
  104.  
  105. if (RSv < 950) // Set Condition for Right Sensor
  106. RS = HIGH;
  107. else
  108. RS = LOW;
  109.  
  110. //Move the bot according to the status of the sensors (Condition Check)
  111.  
  112. if (FS == LOW)//If No obstcacles are detected in the front , move the bot forward.
  113. {
  114. move_forward();
  115. }
  116. else
  117. {
  118.  
  119. if (RS == LOW && LS == HIGH) //If Front obstacle detected, and Right Side of the vehicle is free, i.e RS=LOW then move Right.
  120. {
  121. turn_right();
  122. }
  123.  
  124. if (LS == LOW && RS == HIGH) //TURN LEFT, if left obstacle is not present and right obstacle is present
  125. {
  126. turn_left();
  127. }
  128.  
  129. if (LS == LOW && RS == LOW) //TURN RHT
  130. {
  131. turn_right();
  132. }
  133.  
  134. if (LS == HIGH && RS == HIGH) //move BACK a few distance and then move right
  135. {
  136. //Straight Back
  137. move_reverse();
  138. delay(2500);
  139.  
  140. //Move Right
  141. turn_right();
  142. delay(2500);
  143. }
  144.  
  145. }// Close of conditional movement , if front sensor detects an obstacle
  146. }// Cloase of Infinite Loop