16 Dec 2011

"Wally" -- IR Detection Robot V1

I will update the functionality of this robot, and upload some videos.

16/12/2011

Object following:

bad points:
1. head needs to be rebuild with better material for more smooth performance.
2. coding needs improvement.




update: 

18/12/2011 
I have improved this robot and details are here: 
http://arduin0.blogspot.com/2011/12/ir-detection-robot-enhanced-version-v2.html

today I simply assembled all the parts I built previously. the Main thing I did was on the coding.

the Parts can be found here:


Before I put them together, I first made a base with a recycled CD.









So this is the final result:




Here is my code:

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