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 Navigation"
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-12-18 03:09:01
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Make me the code of an Arduno Uno line followerand */
- /* obstacle-avoiding robot with ultrasonic sensor and */
- /* ir sensor */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Ultrasonic.h> //https://github.com/ErickSimoes/Ultrasonic
- // Pin Definitions
- #define IR_LEFT A0 // Left IR sensor
- #define IR_RIGHT A1 // Right IR sensor
- #define TRIG 8 // Ultrasonic trigger pin
- #define ECHO 7 // Ultrasonic echo pin
- #define MOTOR_LEFT_1 2 // Left motor control pin 1
- #define MOTOR_LEFT_2 3 // Left motor control pin 2
- #define MOTOR_RIGHT_1 4 // Right motor control pin 1
- #define MOTOR_RIGHT_2 5 // Right motor control pin 2
- #define ENA 6 // Left motor speed (PWM)
- #define ENB 9 // Right motor speed (PWM)
- // Constants
- #define DISTANCE_THRESHOLD 15 // Minimum distance for obstacle avoidance (cm)
- #define DEFAULT_SPEED 120 // Default motor speed (0-255)
- #define TURN_DELAY 700 // Delay for turning (ms)
- #define STOP_DELAY 500 // Delay for stopping (ms)
- #define SENSOR_DEBOUNCE 5 // Debounce readings to filter noise
- // Movement States
- enum Movement {
- FORWARD,
- LEFT,
- RIGHT,
- STOP
- };
- // Function to calculate distance using ultrasonic sensor
- int getDistance() {
- long total = 0;
- for (int i = 0; i < 5; i++) { // Average readings for better accuracy
- digitalWrite(TRIG, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIG, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIG, LOW);
- total += pulseIn(ECHO, HIGH, 30000); // Timeout after 30ms
- }
- long avgDuration = total / 5;
- return avgDuration * 0.034 / 2; // Convert duration to distance in cm
- }
- // Function to set motor state
- void setMotorState(int left1, int left2, int right1, int right2) {
- digitalWrite(MOTOR_LEFT_1, left1);
- digitalWrite(MOTOR_LEFT_2, left2);
- digitalWrite(MOTOR_RIGHT_1, right1);
- digitalWrite(MOTOR_RIGHT_2, right2);
- }
- // Function to handle robot movement
- void setMovement(Movement state) {
- switch (state) {
- case FORWARD:
- Serial.println("Moving Forward");
- setMotorState(HIGH, LOW, HIGH, LOW);
- break;
- case LEFT:
- Serial.println("Turning Left");
- setMotorState(LOW, LOW, HIGH, LOW);
- break;
- case RIGHT:
- Serial.println("Turning Right");
- setMotorState(HIGH, LOW, LOW, LOW);
- break;
- case STOP:
- Serial.println("Stopping");
- setMotorState(LOW, LOW, LOW, LOW);
- break;
- }
- }
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // ***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Sensor_HC_SR04_Echo_PIN = ECHO; // Echo pin for ultrasonic sensor
- // ***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t Sensor_HC_SR04_Trigger_PIN = TRIG; // Trigger pin for ultrasonic sensor
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Create an instance of the Ultrasonic class
- Ultrasonic ultrasonic(TRIG, ECHO);
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(Sensor_HC_SR04_Echo_PIN, INPUT);
- pinMode(Sensor_HC_SR04_Trigger_PIN, OUTPUT);
- // Initialize pins for USER CODE
- pinMode(IR_LEFT, INPUT);
- pinMode(IR_RIGHT, INPUT);
- pinMode(TRIG, OUTPUT);
- pinMode(ECHO, INPUT);
- pinMode(MOTOR_LEFT_1, OUTPUT);
- pinMode(MOTOR_LEFT_2, OUTPUT);
- pinMode(MOTOR_RIGHT_1, OUTPUT);
- pinMode(MOTOR_RIGHT_2, OUTPUT);
- pinMode(ENA, OUTPUT);
- pinMode(ENB, OUTPUT);
- // Set default motor speed
- analogWrite(ENA, DEFAULT_SPEED);
- analogWrite(ENB, DEFAULT_SPEED);
- // Start serial communication
- Serial.begin(9600);
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- // Read and debounce IR sensors
- int leftIR = digitalRead(IR_LEFT);
- int rightIR = digitalRead(IR_RIGHT);
- for (int i = 0; i < SENSOR_DEBOUNCE; i++) {
- leftIR &= digitalRead(IR_LEFT);
- rightIR &= digitalRead(IR_RIGHT);
- }
- // Measure distance using ultrasonic sensor
- int distance = getDistance();
- Serial.print("Distance: ");
- Serial.println(distance);
- // Obstacle Avoidance Logic
- if (distance < DISTANCE_THRESHOLD) { // Obstacle detected
- setMovement(STOP); // Stop
- delay(STOP_DELAY); // Pause
- setMovement(RIGHT); // Turn right
- delay(TURN_DELAY); // Delay for turning
- setMovement(FORWARD); // Resume forward movement
- return; // Skip further line-following logic
- }
- // Line Following Logic
- if (leftIR == LOW && rightIR == LOW) { // Both sensors on the line
- setMovement(FORWARD);
- } else if (leftIR == LOW) { // Only left sensor on the line
- setMovement(LEFT);
- } else if (rightIR == LOW) { // Only right sensor on the line
- setMovement(RIGHT);
- } else { // Neither sensor on the line
- setMovement(STOP);
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement