Wally – IR Detection Robot using Arduino

My First ever Robot. It tracks objects using Infra-red sensors, and has basic AI that follows and avoid objects.

I will describe the how I made this robot, and upload some videos.

Version 1 (16 Dec 2011):

—————–The components used ———————-

I actually planned this robot before making
it. I didn’t use all the parts expected. here http://blog.oscarliang.net/ir-…

I only used these compenents:

Arduino

2 x Servo

IR detector

Motor Driver

2 x Motors

4 x AA battery

2 x Wheels

some capacitors/resistors

———————- How did you implement the code ———————-

I divided the robot into 3 functional parts,
write the code and tested each part separately, and finally put them together
and integrate them to work together. For each part see:

———————- Explaining to me the whole idea of this robot ———————-

You should see what this robot can do from the videos. It’s
very simple, and it’s very responsive to interactions, therefore making it very
fun to play with (my friends love it!

=============================================================
update:
18/12/2011
Before I put them together, I first made a base with a recycled CD.

CD

Here is my code:
  1. #include
  2. // Pins
  3. byte leftIRPin = A0;
  4. byte rightIRPin = A1;
  5. byte upIRPin = A2;
  6. byte downIRPin = A3;
  7. byte lrServoPin = 2;
  8. byte udServoPin = 3;
  9. byte motorPin1 = 4;
  10. byte motorPin2 = 5;
  11. byte motorPin3 = 6;
  12. byte motorPin4 = 7;
  13. byte irLEDPin = 8;
  14. // Servos
  15. Servo lrServo;
  16. Servo udServo;
  17. int lrServoPos = 1500;
  18. int udServoPos = 1500;
  19. //IR values
  20. int leftValue_amb;
  21. int rightValue_amb;
  22. int upValue_amb;
  23. int downValue_amb;
  24. int leftValue;
  25. int rightValue;
  26. int upValue;
  27. int downValue;
  28. int distance;   // from 0 – 1000, the larger the closer
  29. int minDistance = 165;
  30. int servoAdjust = 25;
  31. int minDif = 30;
  32. void ReadIR(){
  33.   digitalWrite(irLEDPin, HIGH);
  34.   delay(15);
  35.   // total values
  36.   leftValue = analogRead(leftIRPin);
  37.   rightValue = analogRead(rightIRPin);
  38.   upValue = analogRead(upIRPin);
  39.   downValue = analogRead(downIRPin);
  40.   digitalWrite(irLEDPin, LOW);
  41.   delay(15);
  42.   leftValue_amb = analogRead(leftIRPin);
  43.   rightValue_amb = analogRead(rightIRPin);
  44.   upValue_amb = analogRead(upIRPin);
  45.   downValue_amb = analogRead(downIRPin);
  46.   leftValue = leftValue – leftValue_amb;
  47.   rightValue = rightValue – rightValue_amb;
  48.   upValue = upValue – upValue_amb;
  49.   downValue = downValue – downValue_amb;
  50.   distance = (leftValue + rightValue + upValue +downValue)/4;
  51. }
  52. void setup()
  53. {
  54.   pinMode(irLEDPin, OUTPUT);
  55.   pinMode(motorPin1, OUTPUT);
  56.   pinMode(motorPin2, OUTPUT);
  57.   pinMode(motorPin3, OUTPUT);
  58.   pinMode(motorPin4, OUTPUT);
  59.   lrServo.attach(lrServoPin);
  60.   udServo.attach(udServoPin);
  61.   lrServo.writeMicroseconds(lrServoPos);
  62.   udServo.writeMicroseconds(udServoPos);
  63. }
  64. void loop()
  65. {
  66.   ReadIR();
  67.   // =============== Track Object =================
  68.   if (distance > minDistance){
  69.     if ((rightValue – leftValue) > minDif)
  70.       lrServoPos = lrServoPos – servoAdjust ;
  71.     else if ((leftValue – rightValue) > minDif)
  72.       lrServoPos = lrServoPos  + servoAdjust ;
  73.     if ((upValue – downValue) > minDif)
  74.       udServoPos = udServoPos + servoAdjust ;
  75.     else if ((downValue – upValue) > minDif)
  76.       udServoPos =  udServoPos – servoAdjust ;
  77.   }
  78.   /*
  79.   // =============== Track Object =================
  80.   int temp = leftValue – rightValue;
  81.   if (distance > 70){
  82.     if (rightValue > leftValue)
  83.       lrServoPos = lrServoPos – (rightValue – leftValue)/5 ;
  84.     else
  85.       lrServoPos = lrServoPos  + (leftValue – rightValue)/5 ;
  86.     if (upValue > downValue)
  87.       udServoPos = udServoPos + (upValue – downValue)/5 ;
  88.     else
  89.       udServoPos =  udServoPos – (downValue – upValue)/5 ;
  90.   }
  91. */
  92.   lrServoPos = constrain(lrServoPos,600,2300);
  93.   udServoPos = constrain(udServoPos,600,2300);
  94.   lrServo.writeMicroseconds(lrServoPos);
  95.   udServo.writeMicroseconds(udServoPos);
  96.   // =============== turn body =================
  97.   if (lrServoPos < 1100){
  98.     digitalWrite(motorPin1, LOW);
  99.     digitalWrite(motorPin2, HIGH);
  100.     digitalWrite(motorPin3, HIGH);
  101.     digitalWrite(motorPin4, LOW);
  102.   }
  103.   else if (lrServoPos > 1900){
  104.     digitalWrite(motorPin1, HIGH);
  105.     digitalWrite(motorPin2, LOW);
  106.     digitalWrite(motorPin3, LOW);
  107.     digitalWrite(motorPin4, HIGH);
  108.   }
  109.   else{
  110.     digitalWrite(motorPin1, LOW);
  111.     digitalWrite(motorPin2, LOW);
  112.     digitalWrite(motorPin3, LOW);
  113.     digitalWrite(motorPin4, LOW);
  114.   }
  115. delay(10);
  116. }

Read more: Wally – IR Detection Robot using Arduino

Leave a Comment

Your email address will not be published. Required fields are marked *