Advertisement
pleasedontcode

Object Tracking Robot

Jan 20th, 2024
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.56 KB | Source Code | 0 0
  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: Object_Tracking_Robot
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-07 21:01:40
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* start my moving myServo to 180 then back to 0 then */
  21.     /* leave at 90 degrees.  go to main loop. */
  22. /****** SYSTEM REQUIREMENT 2 *****/
  23.     /* if distance between 5 and 15, move forward. if */
  24.     /* rightSensor is high and leftSensor is low, turn */
  25.     /* right. if rightSensor is low and leftSensor is */
  26.     /* high, turn left. if distance is greater than 15, */
  27.     /* stop. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Arduino.h>
  32. #include <Ultrasonic.h>
  33. #include <Servo.h>
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t ultraSonic_HC_SR04_Echo_PIN = 3;
  41. const uint8_t rightSensor_PIN = 4;
  42. const uint8_t leftSensor_PIN = 5;
  43.  
  44. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  45. const uint8_t ultraSonic_HC_SR04_Trigger_PIN = 2;
  46. const uint8_t motor_L293xQuadrupleHalf_HDriver_1A_PIN = 7;
  47. const uint8_t motor_L293xQuadrupleHalf_HDriver_2A_PIN = 8;
  48. const uint8_t motor_L293xQuadrupleHalf_HDriver_3A_PIN = 11;
  49. const uint8_t motor_L293xQuadrupleHalf_HDriver_4A_PIN = 12;
  50.  
  51. /***** DEFINITION OF PWM OUTPUT PINS *****/
  52. const uint8_t myServo_Servomotor_PWMSignal_PIN = 6;
  53.  
  54. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  55. Ultrasonic ultrasonic(ultraSonic_HC_SR04_Trigger_PIN, ultraSonic_HC_SR04_Echo_PIN);
  56. Servo myServo;
  57.  
  58. void setup(void)
  59. {
  60.   // put your setup code here, to run once:
  61.  
  62.   pinMode(ultraSonic_HC_SR04_Echo_PIN, INPUT);
  63.   pinMode(rightSensor_PIN, INPUT);
  64.   pinMode(leftSensor_PIN, INPUT);
  65.  
  66.   pinMode(ultraSonic_HC_SR04_Trigger_PIN, OUTPUT);
  67.   pinMode(motor_L293xQuadrupleHalf_HDriver_1A_PIN, OUTPUT);
  68.   pinMode(motor_L293xQuadrupleHalf_HDriver_2A_PIN, OUTPUT);
  69.   pinMode(motor_L293xQuadrupleHalf_HDriver_3A_PIN, OUTPUT);
  70.   pinMode(motor_L293xQuadrupleHalf_HDriver_4A_PIN, OUTPUT);
  71.   pinMode(myServo_Servomotor_PWMSignal_PIN, OUTPUT);
  72.  
  73.   myServo.attach(myServo_Servomotor_PWMSignal_PIN);
  74.  
  75.   // SYSTEM REQUIREMENT 1: move myServo to 180 degrees, then back to 0 degrees, and leave at 90 degrees
  76.   myServo.write(180);
  77.   delay(1000);
  78.   myServo.write(0);
  79.   delay(1000);
  80.   myServo.write(90);
  81. }
  82.  
  83. void loop(void)
  84. {
  85.   // put your main code here, to run repeatedly:
  86.  
  87.   // SYSTEM REQUIREMENT 2: logic for moving forward, turning right, turning left, and stopping based on distance and sensor readings
  88.  
  89.   // Read the distance from the ultrasonic sensor
  90.   unsigned int distance = ultrasonic.read();
  91.  
  92.   // Read the sensor values
  93.   bool rightSensor = digitalRead(rightSensor_PIN);
  94.   bool leftSensor = digitalRead(leftSensor_PIN);
  95.  
  96.   // Check if the distance is between 5 and 15
  97.   if (distance >= 5 && distance <= 15)
  98.   {
  99.     // Move forward
  100.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_1A_PIN, HIGH);
  101.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_2A_PIN, LOW);
  102.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_3A_PIN, HIGH);
  103.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_4A_PIN, LOW);
  104.   }
  105.   else if (rightSensor && !leftSensor)
  106.   {
  107.     // Turn right
  108.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_1A_PIN, LOW);
  109.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_2A_PIN, HIGH);
  110.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_3A_PIN, HIGH);
  111.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_4A_PIN, LOW);
  112.   }
  113.   else if (!rightSensor && leftSensor)
  114.   {
  115.     // Turn left
  116.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_1A_PIN, HIGH);
  117.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_2A_PIN, LOW);
  118.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_3A_PIN, LOW);
  119.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_4A_PIN, HIGH);
  120.   }
  121.   else
  122.   {
  123.     // Stop
  124.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_1A_PIN, LOW);
  125.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_2A_PIN, LOW);
  126.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_3A_PIN, LOW);
  127.     digitalWrite(motor_L293xQuadrupleHalf_HDriver_4A_PIN, LOW);
  128.   }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement