pleasedontcode

Autonomous Grabber rev_01

Nov 24th, 2025
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Autonomous Grabber
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-11-24 15:25:17
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Include a feature to calibrate the servo's range, */
  21.     /* enabling adaptive operation for different attached */
  22.     /* mechanisms, utilizing the existing Servo library */
  23.     /* and Arduino Uno, with a simple start/stop */
  24.     /* calibration interface. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. #include <Servo.h>
  31.  
  32. // ------------------- PIN CONFIGURATION -------------------
  33. const int LEFT_IN1  = 2;
  34. const int LEFT_IN2  = 3;
  35. const int LEFT_EN   = 5;
  36. const int RIGHT_IN1 = 4;
  37. const int RIGHT_IN2 = 7;
  38. const int RIGHT_EN  = 6;
  39. const int SERVO_CLAW_PIN = 9;
  40. const int SERVO_ARM_PIN  = 10;
  41. const int SERVO_BASE_PIN = 11;
  42. const int TRIG_PIN = A0;
  43. const int ECHO_PIN = A1;
  44.  
  45. // ------------------- SERVO POSITIONS (adjust/calibrate) -------------------
  46. const int CLAW_OPEN_ANGLE   = 10;
  47. const int CLAW_CLOSED_ANGLE = 70;
  48. const int ARM_UP_ANGLE   = 25;
  49. const int ARM_DOWN_ANGLE = 100;
  50. const int BASE_HOME_ANGLE   = 90;
  51. const int BASE_PICKUP_ANGLE = 90;
  52.  
  53. // ------------------- MOTION & SENSOR PARAMETERS -------------------
  54. const int SPIN_SPEED     = 160;
  55. const int APPROACH_SPEED = 200;
  56. const int BACK_SPEED     = 200;
  57.  
  58. const float DETECTION_DISTANCE = 40.0;
  59. const float PICKUP_DISTANCE    = 12.0;
  60.  
  61. // ------------------- GLOBAL OBJECTS -------------------
  62. Servo servoClaw;
  63. Servo servoArm;
  64. Servo servoBase;
  65.  
  66. // ------------------- HELPER FUNCTIONS -------------------
  67.  
  68. long readUltrasonicCM() {
  69.   digitalWrite(TRIG_PIN, LOW);
  70.   delayMicroseconds(2);
  71.   digitalWrite(TRIG_PIN, HIGH);
  72.   delayMicroseconds(10);
  73.   digitalWrite(TRIG_PIN, LOW);
  74.   long duration = pulseIn(ECHO_PIN, HIGH, 30000);
  75.   if (duration == 0) return -1;
  76.   long cm = duration / 58;
  77.   return cm;
  78. }
  79.  
  80. // Set left motors speed: positive -> forward, negative -> backward, 0 -> stop
  81. void leftMotor(int speed) {
  82.   if (speed > 0) {
  83.     digitalWrite(LEFT_IN1, HIGH);
  84.     digitalWrite(LEFT_IN2, LOW);
  85.     analogWrite(LEFT_EN, constrain(speed, 0, 255));
  86.   } else if (speed < 0) {
  87.     digitalWrite(LEFT_IN1, LOW);
  88.     digitalWrite(LEFT_IN2, HIGH);
  89.     analogWrite(LEFT_EN, constrain(-speed, 0, 255));
  90.   } else {
  91.     digitalWrite(LEFT_IN1, LOW);
  92.     digitalWrite(LEFT_IN2, LOW);
  93.     analogWrite(LEFT_EN, 0);
  94.   }
  95. }
  96.  
  97. // Set right motors speed: positive -> forward, negative -> backward, 0 -> stop
  98. void rightMotor(int speed) {
  99.   if (speed > 0) {
  100.     digitalWrite(RIGHT_IN1, HIGH);
  101.     digitalWrite(RIGHT_IN2, LOW);
  102.     analogWrite(RIGHT_EN, constrain(speed, 0, 255));
  103.   } else if (speed < 0) {
  104.     digitalWrite(RIGHT_IN1, LOW);
  105.     digitalWrite(RIGHT_IN2, HIGH);
  106.     analogWrite(RIGHT_EN, constrain(-speed, 0, 255));
  107.   } else {
  108.     digitalWrite(RIGHT_IN1, LOW);
  109.     digitalWrite(RIGHT_IN2, LOW);
  110.     analogWrite(RIGHT_EN, 0);
  111.   }
  112. }
  113.  
  114. // High-level movement helpers:
  115. void moveForward(int speed) {
  116.   leftMotor(speed);
  117.   rightMotor(speed);
  118. }
  119.  
  120. void moveBackward(int speed) {
  121.   leftMotor(-speed);
  122.   rightMotor(-speed);
  123. }
  124.  
  125. // Spin in place. If clockwise==true, spins clockwise, else counter-clockwise.
  126. void spinInPlace(int speed, bool clockwise) {
  127.   if (clockwise) {
  128.     leftMotor(speed);
  129.     rightMotor(-speed);
  130.   } else {
  131.     leftMotor(-speed);
  132.     rightMotor(speed);
  133.   }
  134. }
  135.  
  136. void stopMotors() {
  137.   leftMotor(0);
  138.   rightMotor(0);
  139. }
  140.  
  141. // Pickup sequence performed after robot is stopped close to object
  142. void performPickupSequence() {
  143.   servoBase.write(BASE_PICKUP_ANGLE);
  144.   delay(600);
  145.   servoArm.write(ARM_DOWN_ANGLE);
  146.   delay(700);
  147.   servoClaw.write(CLAW_CLOSED_ANGLE);
  148.   delay(700);
  149.   servoArm.write(ARM_UP_ANGLE);
  150.   delay(700);
  151. }
  152.  
  153. // ------------------- SETUP -------------------
  154. void setup() {
  155.   pinMode(LEFT_IN1, OUTPUT);
  156.   pinMode(LEFT_IN2, OUTPUT);
  157.   pinMode(LEFT_EN, OUTPUT);
  158.   pinMode(RIGHT_IN1, OUTPUT);
  159.   pinMode(RIGHT_IN2, OUTPUT);
  160.   pinMode(RIGHT_EN, OUTPUT);
  161.   pinMode(TRIG_PIN, OUTPUT);
  162.   pinMode(ECHO_PIN, INPUT);
  163.   Serial.begin(9600);
  164.   servoClaw.attach(SERVO_CLAW_PIN);
  165.   servoArm.attach(SERVO_ARM_PIN);
  166.   servoBase.attach(SERVO_BASE_PIN);
  167.   servoBase.write(BASE_HOME_ANGLE);
  168.   servoArm.write(ARM_UP_ANGLE);
  169.   servoClaw.write(CLAW_OPEN_ANGLE);
  170.   delay(500);}
  171.  
  172. // ------------------- MAIN LOOP -------------------
  173. void loop() {
  174.   Serial.println("Spinning and scanning for object...");
  175.   while (true) {
  176.     spinInPlace(SPIN_SPEED, true);
  177.     delay(120);
  178.     long dist = readUltrasonicCM();
  179.     if (dist > 0) {
  180.       Serial.print("Distance: ");
  181.       Serial.print(dist);
  182.       Serial.println(" cm");
  183.       if (dist <= DETECTION_DISTANCE) {
  184.         Serial.println("Object detected within detection range. Stopping spin.");
  185.         stopMotors();
  186.         delay(200);
  187.         break;}
  188.     } else {
  189.       Serial.println("No echo");}
  190.   }
  191.   Serial.println("Approaching object...");
  192.   while (true) {
  193.     moveForward(APPROACH_SPEED);
  194.     delay(150);
  195.     long dist = readUltrasonicCM();
  196.     if (dist > 0) {
  197.       Serial.print("Approach distance: ");
  198.       Serial.print(dist);
  199.       Serial.println(" cm");
  200.       if (dist <= PICKUP_DISTANCE) {
  201.         Serial.println("Close enough for pickup. Stopping.");
  202.         stopMotors();
  203.         delay(250);
  204.         break;}
  205.     } else {
  206.       Serial.println("No echo while approaching (continuing).");}
  207.   }
  208.   Serial.println("Performing pickup sequence...");
  209.   performPickupSequence();
  210.   Serial.println("Backing away...");
  211.   moveBackward(BACK_SPEED);
  212.   delay(800);
  213.   stopMotors();
  214.   Serial.println("Pickup complete. Entering idle state.");
  215.   while (true) {
  216.     delay(1000);}
  217. }
  218.  
  219. /* END CODE */
  220.  
Advertisement
Add Comment
Please, Sign In to add comment