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: Autonomous Arduino
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2026-01-07 09:29:09
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* suivre une ligne dessinée au sol tout en adaptant */
- /* sa vites en gardant une distance de sécurité avec */
- /* la voiture de devant a l'aides des capteur */
- /* ultrason pour adapté sa vitesse */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- // Include necessary libraries
- #include <Ultrasonic.h>
- #include <LineFollower.h>
- // Define ultrasonic sensor pin
- const int ultrasonPin = 7; // Pin for ultrasonic sensor
- // Define Line Follower sensor pins
- const int leftFollowSensorPin = 2;
- const int rightFollowSensorPin = 3;
- // Instantiate ultrasonic sensor object
- Ultrasonic ultrasonicSensor(ultrasonPin);
- // Variables for distance
- unsigned int distance;
- // Variables for line following
- int leftSensorValue, rightSensorValue;
- // Threshold for line detection (adjust based on calibration)
- const int lineThreshold = 500;
- // Motor control pins
- const int motorLeftForward = 9;
- const int motorLeftBackward = 10;
- const int motorRightForward = 11;
- const int motorRightBackward = 12;
- // Initialization
- void setup(void) {
- // Initialize serial communication
- Serial.begin(9600);
- // Setup sensor pins
- pinMode(ultrasonPin, INPUT);
- pinMode(leftFollowSensorPin, INPUT);
- pinMode(rightFollowSensorPin, INPUT);
- // Setup motor pins
- pinMode(motorLeftForward, OUTPUT);
- pinMode(motorLeftBackward, OUTPUT);
- pinMode(motorRightForward, OUTPUT);
- pinMode(motorRightBackward, OUTPUT);
- }
- // Main loop
- void loop(void) {
- // Read distance from ultrasonic sensor
- distance = ultrasonicSensor.read();
- // Read line follower sensors
- leftSensorValue = analogRead(leftFollowSensorPin);
- rightSensorValue = analogRead(rightFollowSensorPin);
- // Debugging info
- Serial.print("Distance: "); Serial.print(distance); Serial.println(" units");
- Serial.print("Left Sensor: "); Serial.print(leftSensorValue);
- Serial.print(" Right Sensor: "); Serial.println(rightSensorValue);
- // Check safety distance
- if (distance > 0 && distance < 30) { // If object is within 30 units
- // Slow down or stop
- stopMotors();
- Serial.println("Obstacle detected! Stopping.");
- } else {
- // Follow line logic
- followLine();
- }
- delay(100); // Loop delay
- }
- // Function to follow line based on sensors
- void followLine() {
- bool leftOnLine = leftSensorValue > lineThreshold;
- bool rightOnLine = rightSensorValue > lineThreshold;
- if (leftOnLine && rightOnLine) {
- // Move forward
- moveForward();
- } else if (leftOnLine && !rightOnLine) {
- // Turn right
- turnRight();
- } else if (!leftOnLine && rightOnLine) {
- // Turn left
- turnLeft();
- } else {
- // Search for line (e.g., rotate)
- turnRight();
- }
- }
- // Motor control functions
- void moveForward() {
- digitalWrite(motorLeftForward, HIGH);
- digitalWrite(motorLeftBackward, LOW);
- digitalWrite(motorRightForward, HIGH);
- digitalWrite(motorRightBackward, LOW);
- }
- void turnLeft() {
- digitalWrite(motorLeftForward, LOW);
- digitalWrite(motorLeftBackward, HIGH);
- digitalWrite(motorRightForward, HIGH);
- digitalWrite(motorRightBackward, LOW);
- }
- void turnRight() {
- digitalWrite(motorLeftForward, HIGH);
- digitalWrite(motorLeftBackward, LOW);
- digitalWrite(motorRightForward, LOW);
- digitalWrite(motorRightBackward, HIGH);
- }
- void stopMotors() {
- digitalWrite(motorLeftForward, LOW);
- digitalWrite(motorLeftBackward, LOW);
- digitalWrite(motorRightForward, LOW);
- digitalWrite(motorRightBackward, LOW);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment