Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: "Line Following"
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-12-18 03:08:36
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* make a line follower and obstacle avoiding code */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- long getDistance(); // Added function prototype for distance measurement
- void lineFollow(); // Added function prototype for line following
- void moveForward(); // Added function prototype for moving forward
- void stopMotors(); // Added function prototype for stopping motors
- void turnRight(); // Added function prototype for turning right
- void turnLeft(); // Added function prototype for turning left
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t more_HC-SR04_Echo_PIN_D3 = 3;
- const int irLeftPin = 2; // IR sensor left
- const int irRightPin = 3; // IR sensor right
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t more_HC-SR04_Trigger_PIN_D2 = 2;
- const int trigPin = 4; // Ultrasonic Trigger Pin
- const int echoPin = 5; // Ultrasonic Echo Pin
- const int motorLeftPin1 = 6; // Motor driver left motor control 1
- const int motorLeftPin2 = 7; // Motor driver left motor control 2
- const int motorRightPin1 = 8; // Motor driver right motor control 1
- const int motorRightPin2 = 9; // Motor driver right motor control 2
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool more_HC-SR04_Trigger_PIN_D2_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float more_HC-SR04_Trigger_PIN_D2_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Create an instance of the Ultrasonic class
- Ultrasonic ultrasonic(trigPin, echoPin);
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(more_HC-SR04_Echo_PIN_D3, INPUT);
- pinMode(more_HC-SR04_Trigger_PIN_D2, OUTPUT);
- // Initialize the pins for the IR sensors and motors
- pinMode(irLeftPin, INPUT);
- pinMode(irRightPin, INPUT);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(motorLeftPin1, OUTPUT);
- pinMode(motorLeftPin2, OUTPUT);
- pinMode(motorRightPin1, OUTPUT);
- pinMode(motorRightPin2, OUTPUT);
- Serial.begin(9600); // For debugging
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- updateOutputs(); // Refresh output data
- // Get distance from ultrasonic sensor
- long distance = getDistance();
- // Check for obstacles using ultrasonic sensor
- if (distance < 10) {
- // Obstacle detected, stop and turn
- stopMotors();
- delay(500); // Wait for a moment
- turnRight(); // Turn to avoid obstacle
- } else {
- // No obstacle detected, follow the line
- lineFollow();
- }
- }
- void updateOutputs()
- {
- digitalWrite(more_HC-SR04_Trigger_PIN_D2, more_HC-SR04_Trigger_PIN_D2_rawData);
- }
- // Function to measure the distance using ultrasonic sensor
- long getDistance() {
- digitalWrite(trigPin, LOW); // Clear the Trigger pin
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH); // Send trigger pulse
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- long duration = pulseIn(echoPin, HIGH); // Measure the duration of echo pulse
- long distance = (duration / 2) / 29.1; // Calculate the distance in cm
- return distance;
- }
- // Function for line following
- void lineFollow() {
- int leftSensor = digitalRead(irLeftPin);
- int rightSensor = digitalRead(irRightPin);
- if (leftSensor == LOW && rightSensor == LOW) {
- // Both sensors on the line, move forward
- moveForward();
- } else if (leftSensor == LOW && rightSensor == HIGH) {
- // Left sensor on the line, turn left
- turnLeft();
- } else if (leftSensor == HIGH && rightSensor == LOW) {
- // Right sensor on the line, turn right
- turnRight();
- } else {
- // Both sensors off the line, turn right or left to re-align
- turnRight();
- }
- }
- // Move forward
- void moveForward() {
- digitalWrite(motorLeftPin1, HIGH);
- digitalWrite(motorLeftPin2, LOW);
- digitalWrite(motorRightPin1, HIGH);
- digitalWrite(motorRightPin2, LOW);
- }
- // Stop motors
- void stopMotors() {
- digitalWrite(motorLeftPin1, LOW);
- digitalWrite(motorLeftPin2, LOW);
- digitalWrite(motorRightPin1, LOW);
- digitalWrite(motorRightPin2, LOW);
- }
- // Turn right
- void turnRight() {
- digitalWrite(motorLeftPin1, HIGH);
- digitalWrite(motorLeftPin2, LOW);
- digitalWrite(motorRightPin1, LOW);
- digitalWrite(motorRightPin2, LOW);
- delay(300); // Turn for 300ms
- moveForward(); // Resume moving forward
- }
- // Turn left
- void turnLeft() {
- digitalWrite(motorLeftPin1, LOW);
- digitalWrite(motorLeftPin2, LOW);
- digitalWrite(motorRightPin1, HIGH);
- digitalWrite(motorRightPin2, LOW);
- delay(300); // Turn for 300ms
- moveForward(); // Resume moving forward
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement