Advertisement
ossipee

obsticle avoider

Mar 22nd, 2015
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. //Inputs/outputs
  4. #define L1 3
  5. #define L2 4
  6. #define LPWM 5
  7. #define RPWM 6
  8. #define R1 7
  9. #define R2 8
  10. //#define ping 10
  11. #define LedPin  13
  12.  
  13.  
  14. byte Lspeed1=255;
  15. byte Rspeed2=255;
  16. int danger;
  17. int range;
  18. int rotatePos;
  19. Servo rotateServo;  //180= look right   0=look left
  20.  
  21.  
  22.  
  23. //--------------------------------------------------------------------------
  24. void setup() {  
  25.    
  26.   // set motor pins as output and LOW so the motors are braked
  27.   Serial.begin(38400);
  28.   pinMode(L1,OUTPUT);
  29.   pinMode(L2, OUTPUT);
  30.   pinMode(LPWM, OUTPUT);
  31.   pinMode(RPWM, OUTPUT);
  32.   pinMode(R1, OUTPUT);
  33.   pinMode(R2, OUTPUT);  
  34.   danger=250;
  35.   rotateServo.attach(11);
  36.   rotateServo.write(90); //90 degrees, looking straight ahead
  37.   pinMode(LedPin, OUTPUT);      
  38.   digitalWrite(LedPin, LOW);
  39.   motorSpeed(255,255);  
  40. }
  41.  
  42. void loop()
  43. {
  44.   autonomous();
  45.   //forward();
  46.  // rightSlide();
  47.   //fireSonar();
  48.   //delay(1000);
  49. }
  50.  
  51.  
  52. void autonomous()
  53. {
  54.   for (rotatePos=20;rotatePos<160;rotatePos+=10)
  55.   {
  56.     rotateServo.write(rotatePos);
  57.     delay(50);
  58.     fireSonar();
  59.     checkRange();
  60.   }
  61.   for (rotatePos=160;rotatePos>20;rotatePos-=10)
  62.   {
  63.     rotateServo.write(rotatePos);
  64.     delay(50);
  65.     fireSonar();    
  66.     checkRange();
  67.   }  
  68. }
  69.  
  70.  
  71.  
  72. void checkRange()
  73. {
  74.   if (range>danger)
  75.   {
  76.     if (rotatePos<45)
  77.     {
  78.       leftSlide();
  79.     }
  80.     if (rotatePos>44 && rotatePos<91)
  81.     {
  82.       leftSpin();
  83.     }
  84.     if (rotatePos>90 && rotatePos<135)
  85.     {
  86.       rightSpin();
  87.     }
  88.     if (rotatePos>134)
  89.     {
  90.       rightSlide();
  91.     }  
  92.   }else{
  93.     forward();
  94.   }
  95.   fireSonar();  
  96.   if (range>danger)
  97.   {
  98.   checkRange();  
  99.   }
  100. }
  101.  
  102.  
  103. void motorSpeed(int thisLeft,int thisRight)
  104. {
  105.   analogWrite(LPWM,thisLeft);
  106.   analogWrite(RPWM,thisRight);
  107. }
  108.  
  109. void fireSonar()
  110. {
  111.   range=analogRead(A7);
  112.   //Serial.println(range);
  113. }
  114.  
  115. void forward()
  116. {
  117.   digitalWrite(L1,HIGH);
  118.   digitalWrite(L2,LOW);
  119.   digitalWrite(R1,HIGH);
  120.   digitalWrite(R2,LOW);
  121. }
  122.  
  123. void reverse()
  124. {
  125.   digitalWrite(L1,LOW);
  126.   digitalWrite(L2,HIGH);
  127.   digitalWrite(R1,LOW);
  128.   digitalWrite(R2,HIGH);  
  129. }
  130.  
  131. void leftSlide()
  132. {
  133.   digitalWrite(L1,LOW);
  134.   digitalWrite(L2,LOW);
  135.   digitalWrite(R1,HIGH);
  136.   digitalWrite(R2,LOW);  
  137. }
  138.  
  139. void rightSlide()
  140. {
  141.   digitalWrite(L1,HIGH);
  142.   digitalWrite(L2,LOW);
  143.   digitalWrite(R1,LOW);
  144.   digitalWrite(R2,LOW);  
  145. }
  146.  
  147. void leftSpin()
  148. {
  149.   digitalWrite(L1,LOW);
  150.   digitalWrite(L2,HIGH);
  151.   digitalWrite(R1,HIGH);
  152.   digitalWrite(R2,LOW);    
  153. }
  154.  
  155. void rightSpin()
  156. {
  157.   digitalWrite(L1,HIGH);
  158.   digitalWrite(L2,LOW);
  159.   digitalWrite(R1,LOW);
  160.   digitalWrite(R2,HIGH);    
  161. }
  162.  
  163. void allStop()
  164. {
  165.   digitalWrite(L1,LOW);
  166.   digitalWrite(L2,LOW);
  167.   digitalWrite(R1,LOW);
  168.   digitalWrite(R2,LOW);  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement