Advertisement
pleasedontcode

Obstcal_line rev_01

Dec 5th, 2023
94
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: Obstcal_line
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-05 22:31:06
  15.     - Source Code generated by: Khalid
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** SYSTEM REQUIREMENTS *****/
  20. /****** SYSTEM REQUIREMENT 1 *****/
  21.     /* iam making an a robot I'm using ardunio uno and */
  22.     /* l293d motor shield I'm using 2 motors m1 and m2 in */
  23.     /* the motor shield and I'm using 3 cannel ir sensor */
  24.     /* to detect the black line and follow it ant it is */
  25.     /* connected to the shield in pin(#define left A0 */
  26.     /* #defi */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Arduino.h>
  31. #include <Servo.h>
  32.  
  33. /****** SYSTEM REQUIREMENTS *****/
  34. /****** SYSTEM REQUIREMENT 1 *****/
  35. /* iam making an a robot I'm using ardunio uno and */
  36. /* l293d motor shield I'm using 2 motors m1 and m2 in */
  37. /* the motor shield and I'm using 3 cannel ir sensor */
  38. /* to detect the black line and follow it ant it is */
  39. /* connected to the shield in pin(#define left A0 */
  40. /* #defi */
  41. /****** END SYSTEM REQUIREMENTS *****/
  42.  
  43. /****** FUNCTION PROTOTYPES *****/
  44. void setup(void);
  45. void loop(void);
  46.  
  47. /***** DEFINITION OF PWM OUTPUT PINS *****/
  48. const int Servo_Servomotor_PWMSignal_PIN_D3 = 3;
  49.  
  50. /***** DEFINITION OF MOTOR SHIELD PINS *****/
  51. const int Motor_M1_Enable_PIN_9 = 9;
  52. const int Motor_M2_Enable_PIN_10 = 10;
  53. const int Motor_M1_Input1_PIN_2 = 2;
  54. const int Motor_M1_Input2_PIN_4 = 4;
  55. const int Motor_M2_Input3_PIN_6 = 6;
  56. const int Motor_M2_Input4_PIN_8 = 8;
  57.  
  58. /***** DEFINITION OF IR SENSOR PINS *****/
  59. const int IR_Sensor_Left_PIN_A0 = A0;
  60. const int IR_Sensor_Center_PIN_A1 = A1;
  61. const int IR_Sensor_Right_PIN_A2 = A2;
  62.  
  63. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  64. Servo myservo; // Creating an instance of the Servo class
  65.  
  66. void setup(void) {
  67.   // put your setup code here, to run once:
  68.   pinMode(Servo_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  69.   myservo.attach(Servo_Servomotor_PWMSignal_PIN_D3); // Attaching the servo to the designated pin
  70.  
  71.   pinMode(Motor_M1_Enable_PIN_9, OUTPUT);
  72.   pinMode(Motor_M2_Enable_PIN_10, OUTPUT);
  73.  
  74.   pinMode(Motor_M1_Input1_PIN_2, OUTPUT);
  75.   pinMode(Motor_M1_Input2_PIN_4, OUTPUT);
  76.   pinMode(Motor_M2_Input3_PIN_6, OUTPUT);
  77.   pinMode(Motor_M2_Input4_PIN_8, OUTPUT);
  78.  
  79.   pinMode(IR_Sensor_Left_PIN_A0, INPUT);
  80.   pinMode(IR_Sensor_Center_PIN_A1, INPUT);
  81.   pinMode(IR_Sensor_Right_PIN_A2, INPUT);
  82. }
  83.  
  84. void loop(void) {
  85.   // put your main code here, to run repeatedly:
  86.  
  87.   // Read the values from the IR sensors
  88.   int leftSensorValue = digitalRead(IR_Sensor_Left_PIN_A0);
  89.   int centerSensorValue = digitalRead(IR_Sensor_Center_PIN_A1);
  90.   int rightSensorValue = digitalRead(IR_Sensor_Right_PIN_A2);
  91.  
  92.   // Line following logic
  93.   if (leftSensorValue == LOW && centerSensorValue == LOW && rightSensorValue == LOW) {
  94.     // All sensors detect the black line, go straight
  95.     digitalWrite(Motor_M1_Input1_PIN_2, HIGH);
  96.     digitalWrite(Motor_M1_Input2_PIN_4, LOW);
  97.     digitalWrite(Motor_M2_Input3_PIN_6, HIGH);
  98.     digitalWrite(Motor_M2_Input4_PIN_8, LOW);
  99.   }
  100.   else if (leftSensorValue == HIGH && centerSensorValue == LOW && rightSensorValue == LOW) {
  101.     // Left sensor detects the black line, turn left
  102.     digitalWrite(Motor_M1_Input1_PIN_2, LOW);
  103.     digitalWrite(Motor_M1_Input2_PIN_4, HIGH);
  104.     digitalWrite(Motor_M2_Input3_PIN_6, HIGH);
  105.     digitalWrite(Motor_M2_Input4_PIN_8, LOW);
  106.   }
  107.   else if (leftSensorValue == LOW && centerSensorValue == LOW && rightSensorValue == HIGH) {
  108.     // Right sensor detects the black line, turn right
  109.     digitalWrite(Motor_M1_Input1_PIN_2, HIGH);
  110.     digitalWrite(Motor_M1_Input2_PIN_4, LOW);
  111.     digitalWrite(Motor_M2_Input3_PIN_6, LOW);
  112.     digitalWrite(Motor_M2_Input4_PIN_8, HIGH);
  113.   }
  114.   else {
  115.     // No black line detected, stop the motors
  116.     digitalWrite(Motor_M1_Input1_PIN_2, LOW);
  117.     digitalWrite(Motor_M1_Input2_PIN_4, LOW);
  118.     digitalWrite(Motor_M2_Input3_PIN_6, LOW);
  119.     digitalWrite(Motor_M2_Input4_PIN_8, LOW);
  120.   }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement