Advertisement
Guest User

Untitled

a guest
May 29th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <Servo.h>
  2. Servo Motor[2];
  3.  
  4. int sensorPins[]={A0,A1};
  5. int sensorValues[] = {0, 0};
  6. int motorTurn[] = {0, 0};
  7. int echoPin= 12;
  8. int trigPin = 11;
  9. long duration ;
  10. int i;
  11. int values[10];
  12. int sum;
  13.  
  14. long distance;
  15.  
  16.  
  17. void setup() {
  18. // put your setup code here, to run once:
  19. Motor[0].attach(9);
  20.  
  21.  
  22. Motor[1].attach(13);
  23. Serial.begin(9600);
  24. pinMode(trigPin, OUTPUT);
  25. pinMode(echoPin, INPUT);
  26. }
  27.  
  28. void loop() {
  29.  
  30. motorTurn[0] = map(distance, 0, 200, 0, 150);
  31. Motor[0].write(motorTurn[0]);
  32.  
  33. motorTurn[1] = map(distance, 0, 200, 0, 30);
  34. Motor[1].write(motorTurn[1]);
  35.  
  36.  
  37. for(i=0; i < 10; i++){
  38. digitalWrite(trigPin,LOW);
  39. delayMicroseconds(2);
  40. digitalWrite(trigPin,HIGH);
  41. delayMicroseconds(10);
  42. digitalWrite(trigPin,LOW);
  43. duration= pulseIn(echoPin ,HIGH);
  44. distance=duration/29.1;
  45. Serial.println(distance);
  46. sum += distance;
  47. }
  48. sum /= 10;
  49.  
  50. if( sum <= 30 ){
  51.  
  52. motorTurn[0] = map(distance, 0, 200, 0, 150);
  53. Motor[0].write(motorTurn[0]);
  54. motorTurn[1] = 0 ;
  55. Motor[1].write(motorTurn[1]);
  56. delay(2000);
  57.  
  58.  
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement