pleasedontcode

Autonomous Arduino rev_01

Jan 7th, 2026
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Autonomous Arduino
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2026-01-07 09:29:09
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* suivre une ligne dessinée au sol tout en adaptant */
  21.     /* sa vites en gardant une distance de sécurité avec */
  22.     /* la voiture de devant a l'aides des capteur */
  23.     /* ultrason pour adapté sa vitesse */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. // Include necessary libraries
  30. #include <Ultrasonic.h>
  31. #include <LineFollower.h>
  32.  
  33. // Define ultrasonic sensor pin
  34. const int ultrasonPin = 7; // Pin for ultrasonic sensor
  35. // Define Line Follower sensor pins
  36. const int leftFollowSensorPin = 2;
  37. const int rightFollowSensorPin = 3;
  38.  
  39. // Instantiate ultrasonic sensor object
  40. Ultrasonic ultrasonicSensor(ultrasonPin);
  41.  
  42. // Variables for distance
  43. unsigned int distance;
  44.  
  45. // Variables for line following
  46. int leftSensorValue, rightSensorValue;
  47.  
  48. // Threshold for line detection (adjust based on calibration)
  49. const int lineThreshold = 500;
  50.  
  51. // Motor control pins
  52. const int motorLeftForward = 9;
  53. const int motorLeftBackward = 10;
  54. const int motorRightForward = 11;
  55. const int motorRightBackward = 12;
  56.  
  57. // Initialization
  58. void setup(void) {
  59.   // Initialize serial communication
  60.   Serial.begin(9600);
  61.   // Setup sensor pins
  62.   pinMode(ultrasonPin, INPUT);
  63.   pinMode(leftFollowSensorPin, INPUT);
  64.   pinMode(rightFollowSensorPin, INPUT);
  65.   // Setup motor pins
  66.   pinMode(motorLeftForward, OUTPUT);
  67.   pinMode(motorLeftBackward, OUTPUT);
  68.   pinMode(motorRightForward, OUTPUT);
  69.   pinMode(motorRightBackward, OUTPUT);
  70. }
  71.  
  72. // Main loop
  73. void loop(void) {
  74.   // Read distance from ultrasonic sensor
  75.   distance = ultrasonicSensor.read();
  76.  
  77.   // Read line follower sensors
  78.   leftSensorValue = analogRead(leftFollowSensorPin);
  79.   rightSensorValue = analogRead(rightFollowSensorPin);
  80.  
  81.   // Debugging info
  82.   Serial.print("Distance: "); Serial.print(distance); Serial.println(" units");
  83.   Serial.print("Left Sensor: "); Serial.print(leftSensorValue);
  84.   Serial.print(" Right Sensor: "); Serial.println(rightSensorValue);
  85.  
  86.   // Check safety distance
  87.   if (distance > 0 && distance < 30) { // If object is within 30 units
  88.     // Slow down or stop
  89.     stopMotors();
  90.     Serial.println("Obstacle detected! Stopping.");
  91.   } else {
  92.     // Follow line logic
  93.     followLine();
  94.   }
  95.   delay(100); // Loop delay
  96. }
  97.  
  98. // Function to follow line based on sensors
  99. void followLine() {
  100.   bool leftOnLine = leftSensorValue > lineThreshold;
  101.   bool rightOnLine = rightSensorValue > lineThreshold;
  102.  
  103.   if (leftOnLine && rightOnLine) {
  104.     // Move forward
  105.     moveForward();
  106.   } else if (leftOnLine && !rightOnLine) {
  107.     // Turn right
  108.     turnRight();
  109.   } else if (!leftOnLine && rightOnLine) {
  110.     // Turn left
  111.     turnLeft();
  112.   } else {
  113.     // Search for line (e.g., rotate)
  114.     turnRight();
  115.   }
  116. }
  117.  
  118. // Motor control functions
  119. void moveForward() {
  120.   digitalWrite(motorLeftForward, HIGH);
  121.   digitalWrite(motorLeftBackward, LOW);
  122.   digitalWrite(motorRightForward, HIGH);
  123.   digitalWrite(motorRightBackward, LOW);
  124. }
  125.  
  126. void turnLeft() {
  127.   digitalWrite(motorLeftForward, LOW);
  128.   digitalWrite(motorLeftBackward, HIGH);
  129.   digitalWrite(motorRightForward, HIGH);
  130.   digitalWrite(motorRightBackward, LOW);
  131. }
  132.  
  133. void turnRight() {
  134.   digitalWrite(motorLeftForward, HIGH);
  135.   digitalWrite(motorLeftBackward, LOW);
  136.   digitalWrite(motorRightForward, LOW);
  137.   digitalWrite(motorRightBackward, HIGH);
  138. }
  139.  
  140. void stopMotors() {
  141.   digitalWrite(motorLeftForward, LOW);
  142.   digitalWrite(motorLeftBackward, LOW);
  143.   digitalWrite(motorRightForward, LOW);
  144.   digitalWrite(motorRightBackward, LOW);
  145. }
  146.  
  147. /* END CODE */
  148.  
Advertisement
Add Comment
Please, Sign In to add comment