Advertisement
Guest User

claw text

a guest
Jan 23rd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <Servo.h>
  2. #define GroundPin 10
  3. #define echoPin 11
  4. #define trigPin 12
  5. #define VccPin 13
  6. #define lowerThreshold 7
  7. #define upperThreshold 10
  8. #define lowDegree 12
  9. #define highDegree 100
  10.  
  11. /* Servo ports:
  12. * Orange: 9
  13. * Red: 5V
  14. * Brown: GND
  15. *
  16. * Sonar ports:
  17. * Vcc: 13
  18. * Trig: 12
  19. * Echo: 11
  20. * GND: 10
  21. */
  22.  
  23. Servo myservo;
  24. int distance, duration;
  25. int pos = 0;
  26. int toggle = 0;
  27. int spd = 5; //sets the amount of time that it takes for the servo to turn 180 degrees
  28.  
  29. void setup() {
  30. myservo.attach(9);
  31. Serial.begin(9600);
  32. pinMode(VccPin, OUTPUT) ; //tell pin 13 it is going to be an output
  33. digitalWrite(VccPin, HIGH) ; //tell pin 13 to output HIGH (+5V)
  34. pinMode(echoPin, INPUT); //tell pin 11 it is going to be an input
  35. pinMode(trigPin, OUTPUT); //tell pin 12 it is going to be an output
  36. pinMode(GroundPin, OUTPUT) ; //tell pin 10 it is going to be an output
  37. digitalWrite(GroundPin,LOW) ;//tell pin 10 to output LOW (0V, or ground)
  38. }
  39.  
  40. void loop() {
  41. digitalWrite(trigPin, LOW);
  42. delayMicroseconds(1000);
  43. digitalWrite(trigPin, HIGH); //set trigger pin to HIGH
  44. delayMicroseconds(1000); //wait 1000 microseconds
  45. digitalWrite(trigPin, LOW); //set trigger pin to LOW
  46. duration = pulseIn(echoPin, HIGH); //read echo pin
  47. distance = (duration/2) / 29.1; //compute distance from duration of echo Pin
  48.  
  49. if (distance <= lowerThreshold && distance >=0 && toggle == 0) {
  50. //picks up object
  51. delay(1000);
  52. for (pos = lowDegree; pos <= highDegree; pos++) {
  53. myservo.write(pos);
  54. delay(spd);
  55. }
  56. toggle = 1;
  57. }
  58. else if (toggle == 1 && distance >= upperThreshold && distance <= 200){
  59. //holding object above 20cm
  60. toggle = 2;
  61. }
  62. else if (toggle == 2 && distance <= lowerThreshold && distance >= 0) {
  63. //once claw is lowered below 10cm, drops object
  64. delay(1000);
  65. for (pos = highDegree; pos >= lowDegree; pos--) {
  66. myservo.write(pos);
  67. delay(spd);
  68. }
  69. toggle = 3;
  70. }
  71. else if (toggle == 3 && distance >= upperThreshold && distance <= 200){
  72. //once claw is back above 20cm, restarts sequence
  73. toggle = 0;
  74. }
  75. delay(500);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement