Advertisement
pleasedontcode

"Robot Control" rev_01

Dec 20th, 2024
30
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: "Robot Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-20 08:24:13
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop an Arduino-based system that integrates */
  21.     /* various sensors and actuators, allowing users to */
  22.     /* monitor environmental conditions and automate */
  23.     /* responses based on predefined thresholds. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Servo.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. // Servo object
  36. Servo myservo;
  37.  
  38. // Variables
  39. int pos = 0;
  40. boolean fire = false;
  41.  
  42. // Pin definitions
  43. #define Left A0
  44. #define Forward A1
  45. #define Right A2
  46. #define Back A3
  47.  
  48. #define trigPinL 3
  49. #define echoPinL 9
  50. #define trigPinR 11
  51. #define echoPinR 12
  52.  
  53. #define pump 13
  54.  
  55. #define in1 2
  56. #define in2 4
  57. #define in3 7
  58. #define in4 8
  59. #define ena 5
  60. #define enb 6
  61.  
  62. long duration, distance, UltraSensorL, UltraSensorR;
  63. int Cdistance, Rdistance, Ldistance, distanceR, distanceRight;
  64.  
  65. void setup(void)
  66. {
  67.     // Initialize serial communication
  68.     Serial.begin(9600);
  69.  
  70.     // Set motor control pins as outputs
  71.     pinMode(in1, OUTPUT);
  72.     pinMode(in2, OUTPUT);
  73.     pinMode(in3, OUTPUT);
  74.     pinMode(in4, OUTPUT);
  75.     pinMode(ena, OUTPUT);
  76.     pinMode(enb, OUTPUT);
  77.  
  78.     // Set sensor input pins
  79.     pinMode(Left, INPUT);
  80.     pinMode(Forward, INPUT);
  81.     pinMode(Right, INPUT);
  82.     pinMode(Back, INPUT);
  83.  
  84.     // Attach the servo to pin 10 and set initial position
  85.     myservo.attach(10);
  86.     myservo.write(90);
  87.  
  88.     // Set ultrasonic sensor pins
  89.     pinMode(trigPinL, OUTPUT);
  90.     pinMode(echoPinL, INPUT);
  91.     pinMode(trigPinR, OUTPUT);
  92.     pinMode(echoPinR, INPUT);
  93. }
  94.  
  95. void SonarSensor(int trigPinSensor, int echoPinSensor) {
  96.     digitalWrite(trigPinSensor, LOW);
  97.     delayMicroseconds(2);
  98.     digitalWrite(trigPinSensor, HIGH);
  99.     delayMicroseconds(10);
  100.     digitalWrite(trigPinSensor, LOW);
  101.     duration = pulseIn(echoPinSensor, HIGH);
  102.     distance = duration / 57; // Calculate distance in cm
  103. }
  104.  
  105. void FORWARD() {
  106.     analogWrite(ena, 80);
  107.     analogWrite(enb, 80);
  108.     digitalWrite(in1, LOW);
  109.     digitalWrite(in2, HIGH);
  110.     digitalWrite(in3, LOW);
  111.     digitalWrite(in4, HIGH);
  112. }
  113.  
  114. void Stop() {
  115.     digitalWrite(in1, LOW);
  116.     digitalWrite(in2, LOW);
  117.     digitalWrite(in3, LOW);
  118.     digitalWrite(in4, LOW);
  119. }
  120.  
  121. void TurnLeft() {
  122.     analogWrite(ena, 100);
  123.     analogWrite(enb, 100);
  124.     digitalWrite(in1, LOW);
  125.     digitalWrite(in2, HIGH);
  126.     digitalWrite(in3, HIGH);
  127.     digitalWrite(in4, LOW);
  128. }
  129.  
  130. void Tleft90() {
  131.     TurnLeft();
  132.     delay(650);
  133. }
  134.  
  135. void Tleft180() {
  136.     TurnLeft();
  137.     delay(1500);
  138. }
  139.  
  140. void TurnRight() {
  141.     analogWrite(ena, 100);
  142.     analogWrite(enb, 100);
  143.     digitalWrite(in1, HIGH);
  144.     digitalWrite(in2, LOW);
  145.     digitalWrite(in3, LOW);
  146.     digitalWrite(in4, HIGH);
  147. }
  148.  
  149. void Tright90() {
  150.     TurnRight();
  151.     delay(750);
  152. }
  153.  
  154. void put_off_fire() {
  155.     delay(500);
  156.     Stop();
  157.     digitalWrite(pump, HIGH);
  158.     delay(500);
  159.  
  160.     for (pos = 50; pos <= 130; pos += 1) {
  161.         myservo.write(pos);
  162.         delay(10);
  163.     }
  164.     for (pos = 130; pos >= 50; pos -= 1) {
  165.         myservo.write(pos);
  166.         delay(10);
  167.     }
  168.  
  169.     digitalWrite(pump, LOW);
  170.     myservo.write(90);
  171.     if (digitalRead(Forward) == 0) {
  172.         fire = false;
  173.     }
  174. }
  175.  
  176. void comparison(int r, int l) {
  177.     if (r > 15 || l > 15) {
  178.         if (r > l || r == l) {
  179.             Tleft90();
  180.         } else if (l > r) {
  181.             Tright90();
  182.         }
  183.     } else if (r < 25 && l < 25) {
  184.         Tleft180();
  185.     }
  186. }
  187.  
  188. void loop(void)
  189. {
  190.     myservo.write(90);
  191.     SonarSensor(trigPinL, echoPinL);
  192.     UltraSensorL = distance;
  193.     SonarSensor(trigPinR, echoPinR);
  194.     UltraSensorR = distance;
  195.  
  196.     if (UltraSensorR < 10) {
  197.         Tleft90();
  198.     } else if (UltraSensorL < 10) {
  199.         Tright90();
  200.     } else {
  201.         if (UltraSensorL > 30 || UltraSensorR > 30) {
  202.             FORWARD();
  203.         } else {
  204.             SonarSensor(trigPinL, echoPinL);
  205.             Rdistance = distance;
  206.             SonarSensor(trigPinR, echoPinR);
  207.             Ldistance = distance;
  208.             comparison(Rdistance, Ldistance);
  209.             Stop();
  210.             delay(150);
  211.         }
  212.     }
  213.  
  214.     if (digitalRead(Left) == 0 && digitalRead(Right) == 0 && digitalRead(Forward) == 0 && digitalRead(Back) == 1) {
  215.         Stop();
  216.     } else if (digitalRead(Left) == 1) {
  217.         Tleft90();
  218.     } else if (digitalRead(Right) == 1) {
  219.         Tright90();
  220.     } else if (digitalRead(Back) == 0) {
  221.         Tleft180();
  222.     } else if (digitalRead(Forward) == 1 && analogRead(Forward) < 750) {
  223.         FORWARD();
  224.     } else {
  225.         fire = true;
  226.     }
  227.  
  228.     delay(300);
  229.     while (fire == true) {
  230.         put_off_fire();
  231.     }
  232. }
  233.  
  234. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement