Created
November 7, 2025 04:31
-
-
Save Mackaber/aa42902c809068e31cfd59c954db5d07 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Include the Servo library | |
| #include <Servo.h> | |
| // --- Pin Definitions (as variables) --- | |
| // L298N Motor Driver Pins | |
| const int enA = 7; // Enable A (Left Motor Speed) | |
| const int leftMotorIn1 = 6; // IN1 (Left Motor) | |
| const int leftMotorIn2 = 5; // IN2 (Left Motor) | |
| const int rightMotorIn1 = 4; // IN3 (Right Motor) | |
| const int rightMotorIn2 = 3; // IN4 (Right Motor) | |
| const int enB = 2; // Enable B (Right Motor Speed) | |
| // Ultrasonic Sensor Pins | |
| const int trigPin = 9; | |
| const int echoPin = 8; | |
| // Servo Motor Pin | |
| const int servoPin = 10; | |
| // --- STUDENT CALIBRATION VARIABLES --- | |
| int motorSpeed = 180; // General speed for forward/turning (0-255) | |
| int stopDistance = 20; // How close to get to a wall before stopping (cm). | |
| int turnDelay = 450; // How long to run motors for a 90-degree turn (ms) | |
| int servoWaitDelay = 400; // How long to wait for servo to move (ms) | |
| int servoLeftAngle = 130; | |
| int servoCenterAngle = 90; | |
| int servoRightAngle = 50; | |
| // Create our servo object | |
| Servo headServo; | |
| void setup() { | |
| Serial.begin(9600); | |
| // Set all motor pins as OUTPUTs | |
| pinMode(enA, OUTPUT); | |
| pinMode(leftMotorIn1, OUTPUT); | |
| pinMode(leftMotorIn2, OUTPUT); | |
| pinMode(enB, OUTPUT); | |
| pinMode(rightMotorIn1, OUTPUT); | |
| pinMode(rightMotorIn2, OUTPUT); | |
| // Set sensor pins | |
| pinMode(trigPin, OUTPUT); | |
| pinMode(echoPin, INPUT); | |
| // Attach the servo | |
| headServo.attach(servoPin); | |
| headServo.write(servoCenterAngle); // Start facing center | |
| Serial.println("Procedural Reactive Avoider - Ready."); | |
| Serial.println("Ensure calibration variables are set!"); | |
| delay(1000); | |
| } | |
| void loop() { | |
| // --- "Get Distance" Snippet --- | |
| long duration, frontDistance; | |
| digitalWrite(trigPin, LOW); | |
| delayMicroseconds(2); | |
| digitalWrite(trigPin, HIGH); | |
| delayMicroseconds(10); | |
| digitalWrite(trigPin, LOW); | |
| duration = pulseIn(echoPin, HIGH); | |
| frontDistance = (duration * 0.0343) / 2; // Calculate distance in cm | |
| // IF: The path in front is clear | |
| if (frontDistance > stopDistance) { | |
| // --- "Move Forward" Snippet --- | |
| digitalWrite(leftMotorIn1, HIGH); | |
| digitalWrite(leftMotorIn2, LOW); | |
| analogWrite(enA, motorSpeed); | |
| digitalWrite(rightMotorIn1, HIGH); | |
| digitalWrite(rightMotorIn2, LOW); | |
| analogWrite(enB, motorSpeed); | |
| } | |
| else { | |
| // --- "Stop Motors" Snippet --- | |
| digitalWrite(leftMotorIn1, LOW); | |
| digitalWrite(leftMotorIn2, LOW); | |
| analogWrite(enA, 0); | |
| digitalWrite(rightMotorIn1, LOW); | |
| digitalWrite(rightMotorIn2, LOW); | |
| analogWrite(enB, 0); | |
| // 2a. Look Right | |
| headServo.write(servoRightAngle); | |
| delay(servoWaitDelay); | |
| // --- "Get Distance" Snippet --- | |
| long rightDuration, rightDistance; | |
| digitalWrite(trigPin, LOW); | |
| delayMicroseconds(2); | |
| digitalWrite(trigPin, HIGH); | |
| delayMicroseconds(10); | |
| digitalWrite(trigPin, LOW); | |
| rightDuration = pulseIn(echoPin, HIGH); | |
| rightDistance = (rightDuration * 0.0343) / 2; | |
| // --- "Get Distance" Snippet --- | |
| long leftDuration, leftDistance; | |
| digitalWrite(trigPin, LOW); | |
| delayMicroseconds(2); | |
| digitalWrite(trigPin, HIGH); | |
| delayMicroseconds(10); | |
| digitalWrite(trigPin, LOW); | |
| leftDuration = pulseIn(echoPin, HIGH); | |
| leftDistance = (leftDuration * 0.0343) / 2; | |
| // 2c. Make the decision | |
| if (leftDistance > rightDistance) { | |
| // --- "Turn Left" Snippet --- | |
| digitalWrite(leftMotorIn1, LOW); | |
| digitalWrite(leftMotorIn2, HIGH); | |
| analogWrite(enA, motorSpeed); | |
| digitalWrite(rightMotorIn1, HIGH); | |
| digitalWrite(rightMotorIn2, LOW); | |
| analogWrite(enB, motorSpeed); | |
| } else { | |
| // --- "Turn Right" Snippet --- | |
| digitalWrite(leftMotorIn1, HIGH); | |
| digitalWrite(leftMotorIn2, LOW); | |
| analogWrite(enA, motorSpeed); | |
| digitalWrite(rightMotorIn1, LOW); | |
| digitalWrite(rightMotorIn2, HIGH); | |
| analogWrite(enB, motorSpeed); | |
| } | |
| // --- "Stop Motors" Snippet --- | |
| digitalWrite(leftMotorIn1, LOW); | |
| digitalWrite(leftMotorIn2, LOW); | |
| analogWrite(enA, 0); | |
| digitalWrite(rightMotorIn1, LOW); | |
| digitalWrite(rightMotorIn2, LOW); | |
| analogWrite(enB, 0); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment