Guest User

Untitled

a guest
Jan 11th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. ---------------------------------------------------------------------------
  2.  
  3. //APSC 101-Mariya Fedoroff
  4.  
  5. // Arduino- Servo, Sonor, and Serial Monitor
  6.  
  7. //
  8.  
  9. //This program reads values gathered by the sonar and shifts
  10.  
  11. //the servo motor accordingly
  12.  
  13. // ---------------------------------------------------------------------------
  14.  
  15.  
  16. #include <Servo.h>
  17.  
  18. #define SERVO_PIN 9
  19. #define groundPin 10
  20. #define echoPin 11
  21. #define trigPin 12
  22. #define VccPin 13
  23. #define CLOSE 60
  24. #define OPEN 150
  25.  
  26. Servo myservo ;
  27.  
  28.  
  29. void setup()
  30.  
  31. {
  32.  
  33. Serial.begin(9600);
  34.  
  35. pinMode(VccPin, OUTPUT) ; //pin 13 shall be used as output
  36. digitalWrite(VccPin, HIGH); //Tell pin 13 to output HIGH (+5V)
  37. pinMode(echoPin, INPUT);
  38. pinMode(trigPin, OUTPUT);
  39. pinMode(groundPin, OUTPUT);
  40. digitalWrite(groundPin, LOW);
  41.  
  42.  
  43. myservo.attach(9);
  44. myservo.write(CLOSE);
  45.  
  46. }
  47.  
  48.  
  49. void loop()
  50.  
  51. {
  52.  
  53. delay(200);
  54.  
  55. int duration, distance;
  56. digitalWrite(trigPin, HIGH);
  57. delayMicroseconds(1000);
  58. digitalWrite(trigPin, LOW);
  59.  
  60. duration = pulseIn(echoPin, HIGH);
  61. distance = (duration/2)/29.1;
  62.  
  63. if(distance>=200 || distance<= 0){
  64. Serial.println("Out of Range");
  65. }
  66. while(distance >= 10){
  67. Serial.println("Claw is closed: it will remain closed above 10cm or 5 seconds below 10cm");
  68.  
  69. myservo.write(CLOSE);
  70. Serial.print(distance);
  71. Serial.println(" cm");
  72. Serial.println("");
  73. delay(5000);
  74.  
  75. // duration = pulseIn(echoPin, HIGH); I think theres a problem here
  76. // distance = (duration/2)/29.1; the distance is always read to be 0 here
  77. // for some reason
  78. }
  79.  
  80. if(distance < 10){
  81. myservo.write(OPEN);
  82. Serial.println("");
  83. Serial.print(distance);
  84. Serial.println(" cm");
  85. Serial.println("");
  86. Serial.println("Claw is open");
  87. Serial.println("You have 5 seconds before claw closes!");
  88. delay(5000);
  89. myservo.write(CLOSE);
  90.  
  91. //duration = pulseIn(echoPin, HIGH);
  92. //distance = (duration/2)/29.1;
  93.  
  94. if(distance<10){
  95. delay(5000);
  96.  
  97. }
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment