pleasedontcode

Motor Control rev_01

Oct 29th, 2025
421
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: Motor Control
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-29 17:54:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a servo motor control system that */
  21.     /* adjusts its angle based on input from connected */
  22.     /* sensors, improving automation precision in the */
  23.     /* project. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. // System Requirements: Implement a servo motor control system that adjusts its angle based on input from connected sensors, improving automation precision in the project.
  30.  
  31. #include <Servo.h>
  32. #include <NewPing.h>
  33.  
  34. #define SERVO_PIN 3
  35. #define ULTRASONIC_SENSOR_TRIG 11
  36. #define ULTRASONIC_SENSOR_ECHO 12
  37. #define MAX_REGULAR_MOTOR_SPEED 80
  38. #define MAX_MOTOR_ADJUST_SPEED 170
  39. #define DISTANCE_TO_CHECK 40
  40.  
  41. //Right motor
  42. int enableRightMotor=5;
  43. int rightMotorPin1=7;
  44. int rightMotorPin2=8;
  45.  
  46. //Left motor
  47. int enableLeftMotor=6;
  48. int leftMotorPin1=9;
  49. int leftMotorPin2=10;
  50.  
  51. NewPing mySensor(ULTRASONIC_SENSOR_TRIG, ULTRASONIC_SENSOR_ECHO, 400);
  52. Servo myServo;
  53.  
  54. // Function to rotate motors at specified speeds
  55. void rotateMotor(int rightMotorSpeed, int leftMotorSpeed) {
  56.   if (rightMotorSpeed < 0) {
  57.     digitalWrite(rightMotorPin1,LOW);
  58.     digitalWrite(rightMotorPin2,HIGH);    
  59.   } else if (rightMotorSpeed >= 0) {
  60.     digitalWrite(rightMotorPin1,HIGH);
  61.     digitalWrite(rightMotorPin2,LOW);      
  62.   }
  63.   if (leftMotorSpeed < 0) {
  64.     digitalWrite(leftMotorPin1,LOW);
  65.     digitalWrite(leftMotorPin2,HIGH);    
  66.   } else if (leftMotorSpeed >= 0) {
  67.     digitalWrite(leftMotorPin1,HIGH);
  68.     digitalWrite(leftMotorPin2,LOW);      
  69.   }
  70.   analogWrite(enableRightMotor, abs(rightMotorSpeed));
  71.   analogWrite(enableLeftMotor, abs(leftMotorSpeed));    
  72. }
  73.  
  74. void setup() {
  75.   // Initialize motor control pins
  76.   pinMode(enableRightMotor,OUTPUT);
  77.   pinMode(rightMotorPin1,OUTPUT);
  78.   pinMode(rightMotorPin2,OUTPUT);
  79.  
  80.   pinMode(enableLeftMotor,OUTPUT);
  81.   pinMode(leftMotorPin1,OUTPUT);
  82.   pinMode(leftMotorPin2,OUTPUT);
  83.  
  84.   // Attach servo
  85.   myServo.attach(SERVO_PIN);
  86.   myServo.write(90); // Set servo to center position
  87. }
  88.  
  89. void loop() {
  90.   // Read distance using ultrasonic sensor
  91.   int distance = mySensor.ping_cm();
  92.  
  93.   // If object detected within threshold, perform avoidance maneuver
  94.   if (distance > 0 && distance < DISTANCE_TO_CHECK) {
  95.     // Stop motors
  96.     rotateMotor(0, 0);
  97.     delay(500);
  98.    
  99.     // Reverse motors
  100.     rotateMotor(-MAX_MOTOR_ADJUST_SPEED, -MAX_MOTOR_ADJUST_SPEED);
  101.     delay(200);
  102.    
  103.     // Stop motors
  104.     rotateMotor(0, 0);
  105.     delay(500);
  106.    
  107.     // Rotate servo to left
  108.     myServo.write(180);
  109.     delay(500);
  110.    
  111.     // Read left side distance
  112.     int distanceLeft = mySensor.ping_cm();
  113.  
  114.     // Rotate servo to right
  115.     myServo.write(0);
  116.     delay(500);
  117.  
  118.     // Read right side distance
  119.     int distanceRight = mySensor.ping_cm();
  120.  
  121.     // Return servo to center
  122.     myServo.write(90);
  123.     delay(500);
  124.  
  125.     // Decide turn direction based on distances
  126.     if (distanceLeft == 0) {
  127.       rotateMotor(MAX_MOTOR_ADJUST_SPEED, -MAX_MOTOR_ADJUST_SPEED);
  128.       delay(550);
  129.     } else if (distanceRight == 0) {
  130.       rotateMotor(-MAX_MOTOR_ADJUST_SPEED, MAX_MOTOR_ADJUST_SPEED);
  131.       delay(550);
  132.     } else if (distanceLeft >= distanceRight) {
  133.       rotateMotor(MAX_MOTOR_ADJUST_SPEED, -MAX_MOTOR_ADJUST_SPEED);
  134.       delay(550);
  135.     } else {
  136.       rotateMotor(-MAX_MOTOR_ADJUST_SPEED, MAX_MOTOR_ADJUST_SPEED);
  137.       delay(550);
  138.     }
  139.     // Stop motors after maneuver
  140.     rotateMotor(0, 0);
  141.     delay(550);
  142.   } else {
  143.     // Move forward
  144.     rotateMotor(MAX_REGULAR_MOTOR_SPEED, MAX_REGULAR_MOTOR_SPEED);
  145.   }
  146. }
  147.  
  148. /* END CODE */
  149.  
Advertisement
Add Comment
Please, Sign In to add comment