0% found this document useful (0 votes)
5 views

Chat GPT

Uploaded by

Jaya Sai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chat GPT

Uploaded by

Jaya Sai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ChatGPT

chat.openai.com/c/f414f079-9da0-4485-bb96-9a2ab03203a7

ChatGPT 3.5

Arduino Code:

1/7
arduino

// Pin definitions
#define LEFT_MOTOR_EN 5
#define LEFT_MOTOR_IN1 6
#define LEFT_MOTOR_IN2 7
#define RIGHT_MOTOR_EN 9
#define RIGHT_MOTOR_IN1 10
#define RIGHT_MOTOR_IN2 11
#define LEFT_SENSOR A0
#define RIGHT_SENSOR A1

// Threshold for detecting the line


#define THRESHOLD 500

void setup() {
// Motor pins setup
pinMode(LEFT_MOTOR_EN, OUTPUT);
pinMode(LEFT_MOTOR_IN1, OUTPUT);
pinMode(LEFT_MOTOR_IN2, OUTPUT);
pinMode(RIGHT_MOTOR_EN, OUTPUT);
pinMode(RIGHT_MOTOR_IN1, OUTPUT);
pinMode(RIGHT_MOTOR_IN2, OUTPUT);
// Sensor pins setup
pinMode(LEFT_SENSOR, INPUT);
pinMode(RIGHT_SENSOR, INPUT);
}

void loop() {
int leftSensorValue = analogRead(LEFT_SENSOR);
int rightSensorValue = analogRead(RIGHT_SENSOR);

// Line following logic


if (leftSensorValue < THRESHOLD && rightSensorValue < THRESHOLD) {
moveForward();
} else if (leftSensorValue < THRESHOLD && rightSensorValue >=
THRESHOLD) {
turnRight();
} else if (leftSensorValue >= THRESHOLD && rightSensorValue <
THRESHOLD) {
turnLeft();
} else {
stopMotors();
}
}

void moveForward() {
digitalWrite(LEFT_MOTOR_IN1, HIGH);
digitalWrite(LEFT_MOTOR_IN2, LOW);
digitalWrite(RIGHT_MOTOR_IN1, HIGH);
digitalWrite(RIGHT_MOTOR_IN2, LOW);
analogWrite(LEFT_MOTOR_EN, 200);

2/7
analogWrite(RIGHT_MOTOR_EN, 200);
}

void turnLeft() {
digitalWrite(LEFT_MOTOR_IN1, LOW);
digitalWrite(LEFT_MOTOR_IN2, HIGH);
digitalWrite(RIGHT_MOTOR_IN1, HIGH);
digitalWrite(RIGHT_MOTOR_IN2, LOW);
analogWrite(LEFT_MOTOR_EN, 150);
analogWrite(RIGHT_MOTOR_EN, 150);
}

void turnRight() {
digitalWrite(LEFT_MOTOR_IN1, HIGH);
digitalWrite(LEFT_MOTOR_IN2, LOW);
digitalWrite(RIGHT_MOTOR_IN1, LOW);
digitalWrite(RIGHT_MOTOR_IN2, HIGH);
analogWrite(LEFT_MOTOR_EN, 150);
analogWrite(RIGHT_MOTOR_EN, 150);
}

void stopMotors() {
digitalWrite(LEFT_MOTOR_IN1, LOW);
digitalWrite(LEFT_MOTOR_IN2, LOW);
digitalWrite(RIGHT_MOTOR_IN1, LOW);
digitalWrite(RIGHT_MOTOR_IN2, LOW);
}

You

ChatGPT

Sure, let's go through the code line by line:

3/7
cpp

// Pin definitions
#define LEFT_MOTOR_EN 5
#define LEFT_MOTOR_IN1 6
#define LEFT_MOTOR_IN2 7
#define RIGHT_MOTOR_EN 9
#define RIGHT_MOTOR_IN1 10
#define RIGHT_MOTOR_IN2 11
#define LEFT_SENSOR A0
#define RIGHT_SENSOR A1

These lines define symbolic names for the pin numbers used in the Arduino
sketch. It makes the code easier to read and modify. For example,
LEFT_MOTOR_EN represents the enable pin for the left motor.

cpp

// Threshold for detecting the line


#define THRESHOLD 500

This sets a threshold value for the sensor readings. If the sensor readings fall
below this threshold, the robot considers itself to be on the line.

4/7
cpp

void setup() {
// Motor pins setup
pinMode(LEFT_MOTOR_EN, OUTPUT);
pinMode(LEFT_MOTOR_IN1, OUTPUT);
pinMode(LEFT_MOTOR_IN2, OUTPUT);
pinMode(RIGHT_MOTOR_EN, OUTPUT);
pinMode(RIGHT_MOTOR_IN1, OUTPUT);
pinMode(RIGHT_MOTOR_IN2, OUTPUT);
// Sensor pins setup
pinMode(LEFT_SENSOR, INPUT);
pinMode(RIGHT_SENSOR, INPUT);
}

In the setup() function, the pins used for motor control and sensor reading
are configured. The motor pins are set as outputs, while the sensor pins are
set as inputs.

cpp

void loop() {
int leftSensorValue = analogRead(LEFT_SENSOR);
int rightSensorValue = analogRead(RIGHT_SENSOR);

// Line following logic


if (leftSensorValue < THRESHOLD && rightSensorValue < THRESHOLD) {
moveForward();
} else if (leftSensorValue < THRESHOLD && rightSensorValue >=
THRESHOLD) {
turnRight();
} else if (leftSensorValue >= THRESHOLD && rightSensorValue <
THRESHOLD) {
turnLeft();
} else {
stopMotors();
}
}

5/7
The loop() function runs continuously once the Arduino is powered on. It
reads the sensor values and decides what action the robot should take
based on the line-following logic.

cpp

void moveForward() {
digitalWrite(LEFT_MOTOR_IN1, HIGH);
digitalWrite(LEFT_MOTOR_IN2, LOW);
digitalWrite(RIGHT_MOTOR_IN1, HIGH);
digitalWrite(RIGHT_MOTOR_IN2, LOW);
analogWrite(LEFT_MOTOR_EN, 200);
analogWrite(RIGHT_MOTOR_EN, 200);
}

This function is called when both sensors detect the line. It sets the motor
directions to move the robot forward and applies a certain PWM (Pulse Width
Modulation) value to the motor enable pins to control the speed.

cpp

void turnLeft() {
digitalWrite(LEFT_MOTOR_IN1, LOW);
digitalWrite(LEFT_MOTOR_IN2, HIGH);
digitalWrite(RIGHT_MOTOR_IN1, HIGH);
digitalWrite(RIGHT_MOTOR_IN2, LOW);
analogWrite(LEFT_MOTOR_EN, 150);
analogWrite(RIGHT_MOTOR_EN, 150);
}

This function is called when only the left sensor detects the line. It makes the
robot turn left by reversing the left motor's direction and setting the right
motor to move forward.

6/7
cpp

void turnRight() {
digitalWrite(LEFT_MOTOR_IN1, HIGH);
digitalWrite(LEFT_MOTOR_IN2, LOW);
digitalWrite(RIGHT_MOTOR_IN1, LOW);
digitalWrite(RIGHT_MOTOR_IN2, HIGH);
analogWrite(LEFT_MOTOR_EN, 150);
analogWrite(RIGHT_MOTOR_EN, 150);
}

This function is called when only the right sensor detects the line. It makes
the robot turn right by reversing the right motor's direction and setting the left
motor to move forward.

cpp

void stopMotors() {
digitalWrite(LEFT_MOTOR_IN1, LOW);
digitalWrite(LEFT_MOTOR_IN2, LOW);
digitalWrite(RIGHT_MOTOR_IN1, LOW);
digitalWrite(RIGHT_MOTOR_IN2, LOW);
}

This function is called when neither sensor detects the line. It stops both
motors by setting their control pins low.

ChatGPT can make mistakes. Consider checking important information.

7/7

You might also like