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: Obstacle Detection
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-02-05 05:51:54
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* "I want to implement ultrasonic object detection */
- /* using the HC-SR04 sensor for distance measurement */
- /* and integrate it with a line-following mechanism." */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Ultrasonic.h> // https://github.com/ErickSimoes/Ultrasonic
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Ultra_HC_SR04_Echo_PIN_D3 = 3;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t Ultra_HC_SR04_Trigger_PIN_D2 = 2;
- // ============================
- // Motor control pins
- // ============================
- const int motorLeft1 = 7;
- const int motorLeft2 = 8;
- const int motorRight1 = 9;
- const int motorRight2 = 10;
- const int motorSpeedLeft = 5;
- const int motorSpeedRight = 6;
- // ============================
- // Line-following sensor pins
- // ============================
- const int lineSensorLeft = A0;
- const int lineSensorRight = A2;
- // ============================
- // Front Ultrasonic sensor pins
- // ============================
- const int trigPin = 11;
- const int echoPin = 12;
- // ============================
- // Thresholds and global variables
- // ============================
- const int lineThreshold = 500; // Increased threshold to 30 cm (adjust as needed)
- const int obstacleThreshold = 30;
- // Create an instance of the Ultrasonic class for the front sensor
- Ultrasonic ultrasonic(trigPin, echoPin);
- // ============================
- // Setup
- // ============================
- void setup() {
- // put your setup code here, to run once:
- pinMode(Ultra_HC_SR04_Echo_PIN_D3, INPUT);
- pinMode(Ultra_HC_SR04_Trigger_PIN_D2, OUTPUT);
- // Initialize motor control pins as outputs
- pinMode(motorLeft1, OUTPUT);
- pinMode(motorLeft2, OUTPUT);
- pinMode(motorRight1, OUTPUT);
- pinMode(motorRight2, OUTPUT);
- pinMode(motorSpeedLeft, OUTPUT);
- pinMode(motorSpeedRight, OUTPUT);
- // Initialize line-following sensor pins as inputs
- pinMode(lineSensorLeft, INPUT);
- pinMode(lineSensorRight, INPUT);
- // Initialize serial communication for debugging
- Serial.begin(9600);
- randomSeed(analogRead(A3));
- }
- void loop(void) {
- // put your main code here, to run repeatedly:
- updateOutputs(); // Refresh output data
- // Read line-following sensors
- int leftSensorValue = analogRead(lineSensorLeft);
- int rightSensorValue = analogRead(lineSensorRight);
- // Read the front ultrasonic sensor
- int distance = ultrasonic.read(); // Use the Ultrasonic library to get distance
- Serial.print("Front distance: ");
- Serial.println(distance);
- // Check for obstacles in front using the front sensor
- if (distance > 0 && distance < obstacleThreshold) {
- Serial.println("Obstacle detected in front!");
- compareDistance();
- } else {
- // No front obstacle: follow the line
- if (leftSensorValue < lineThreshold && rightSensorValue < lineThreshold) {
- // Both sensors on the line, move forward
- moveForward();
- } else if (leftSensorValue < lineThreshold) {
- // Left sensor on the line, turn right to re-align
- turnRight();
- } else if (rightSensorValue < lineThreshold) {
- // Right sensor on the line, turn left to re-align
- turnLeft();
- } else {
- // Neither sensor on the line, stop
- stopMotors();
- }
- }
- }
- // ============================
- // New function: compareDistance()
- // Uses left/right ultrasonic sensors to decide which way to avoid an obstacle
- // ============================
- void compareDistance() {
- // Placeholder for left/right distance comparison logic
- // Uncomment and implement if using left/right ultrasonic sensors
- }
- // ============================
- // Motor control functions
- // ============================
- void moveForward() {
- digitalWrite(motorLeft1, HIGH);
- digitalWrite(motorLeft2, LOW);
- digitalWrite(motorRight1, HIGH);
- digitalWrite(motorRight2, LOW);
- analogWrite(motorSpeedLeft, 200);
- analogWrite(motorSpeedRight, 200);
- }
- void moveBackward() {
- digitalWrite(motorLeft1, LOW);
- digitalWrite(motorLeft2, HIGH);
- digitalWrite(motorRight1, LOW);
- digitalWrite(motorRight2, HIGH);
- analogWrite(motorSpeedLeft, 200);
- analogWrite(motorSpeedRight, 200);
- }
- void turnLeft() {
- // To turn left, reverse the left motor and forward the right motor
- digitalWrite(motorLeft1, LOW);
- digitalWrite(motorLeft2, HIGH);
- digitalWrite(motorRight1, HIGH);
- digitalWrite(motorRight2, LOW);
- analogWrite(motorSpeedLeft, 200);
- analogWrite(motorSpeedRight, 200);
- }
- void turnRight() {
- // To turn right, forward the left motor and reverse the right motor
- digitalWrite(motorLeft1, HIGH);
- digitalWrite(motorLeft2, LOW);
- digitalWrite(motorRight1, LOW);
- digitalWrite(motorRight2, HIGH);
- analogWrite(motorSpeedLeft, 200);
- analogWrite(motorSpeedRight, 200);
- }
- void stopMotors() {
- digitalWrite(motorLeft1, LOW);
- digitalWrite(motorLeft2, LOW);
- digitalWrite(motorRight1, LOW);
- digitalWrite(motorRight2, LOW);
- analogWrite(motorSpeedLeft, 0);
- analogWrite(motorSpeedRight, 0);
- }
- void updateOutputs() {
- // Placeholder for updating outputs logic
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement