CCCPaste

Obstcal_line rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: Obstcal_line
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-12-05 22:31:06
	- Source Code generated by: Khalid

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* iam making an a robot I'm using ardunio uno and */
	/* l293d motor shield I'm using 2 motors m1 and m2 in */
	/* the motor shield and I'm using 3 cannel ir sensor */
	/* to detect the black line and follow it ant it is */
	/* connected to the shield in pin(#define left A0 */
	/* #defi */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* iam making an a robot I'm using ardunio uno and */
/* l293d motor shield I'm using 2 motors m1 and m2 in */
/* the motor shield and I'm using 3 cannel ir sensor */
/* to detect the black line and follow it ant it is */
/* connected to the shield in pin(#define left A0 */
/* #defi */
/****** END SYSTEM REQUIREMENTS *****/

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF PWM OUTPUT PINS *****/
const int Servo_Servomotor_PWMSignal_PIN_D3 = 3;

/***** DEFINITION OF MOTOR SHIELD PINS *****/
const int Motor_M1_Enable_PIN_9 = 9;
const int Motor_M2_Enable_PIN_10 = 10;
const int Motor_M1_Input1_PIN_2 = 2;
const int Motor_M1_Input2_PIN_4 = 4;
const int Motor_M2_Input3_PIN_6 = 6;
const int Motor_M2_Input4_PIN_8 = 8;

/***** DEFINITION OF IR SENSOR PINS *****/
const int IR_Sensor_Left_PIN_A0 = A0;
const int IR_Sensor_Center_PIN_A1 = A1;
const int IR_Sensor_Right_PIN_A2 = A2;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Servo myservo; // Creating an instance of the Servo class

void setup(void) {
  // put your setup code here, to run once:
  pinMode(Servo_Servomotor_PWMSignal_PIN_D3, OUTPUT);
  myservo.attach(Servo_Servomotor_PWMSignal_PIN_D3); // Attaching the servo to the designated pin
  
  pinMode(Motor_M1_Enable_PIN_9, OUTPUT);
  pinMode(Motor_M2_Enable_PIN_10, OUTPUT);
  
  pinMode(Motor_M1_Input1_PIN_2, OUTPUT);
  pinMode(Motor_M1_Input2_PIN_4, OUTPUT);
  pinMode(Motor_M2_Input3_PIN_6, OUTPUT);
  pinMode(Motor_M2_Input4_PIN_8, OUTPUT);
  
  pinMode(IR_Sensor_Left_PIN_A0, INPUT);
  pinMode(IR_Sensor_Center_PIN_A1, INPUT);
  pinMode(IR_Sensor_Right_PIN_A2, INPUT);
}

void loop(void) {
  // put your main code here, to run repeatedly:
  
  // Read the values from the IR sensors
  int leftSensorValue = digitalRead(IR_Sensor_Left_PIN_A0);
  int centerSensorValue = digitalRead(IR_Sensor_Center_PIN_A1);
  int rightSensorValue = digitalRead(IR_Sensor_Right_PIN_A2);
  
  // Line following logic
  if (leftSensorValue == LOW && centerSensorValue == LOW && rightSensorValue == LOW) {
    // All sensors detect the black line, go straight
    digitalWrite(Motor_M1_Input1_PIN_2, HIGH);
    digitalWrite(Motor_M1_Input2_PIN_4, LOW);
    digitalWrite(Motor_M2_Input3_PIN_6, HIGH);
    digitalWrite(Motor_M2_Input4_PIN_8, LOW);
  }
  else if (leftSensorValue == HIGH && centerSensorValue == LOW && rightSensorValue == LOW) {
    // Left sensor detects the black line, turn left
    digitalWrite(Motor_M1_Input1_PIN_2, LOW);
    digitalWrite(Motor_M1_Input2_PIN_4, HIGH);
    digitalWrite(Motor_M2_Input3_PIN_6, HIGH);
    digitalWrite(Motor_M2_Input4_PIN_8, LOW);
  }
  else if (leftSensorValue == LOW && centerSensorValue == LOW && rightSensorValue == HIGH) {
    // Right sensor detects the black line, turn right
    digitalWrite(Motor_M1_Input1_PIN_2, HIGH);
    digitalWrite(Motor_M1_Input2_PIN_4, LOW);
    digitalWrite(Motor_M2_Input3_PIN_6, LOW);
    digitalWrite(Motor_M2_Input4_PIN_8, HIGH);
  }
  else {
    // No black line detected, stop the motors
    digitalWrite(Motor_M1_Input1_PIN_2, LOW);
    digitalWrite(Motor_M1_Input2_PIN_4, LOW);
    digitalWrite(Motor_M2_Input3_PIN_6, LOW);
    digitalWrite(Motor_M2_Input4_PIN_8, LOW);
  }
}