Advertisement
pleasedontcode

**Robot Control** rev_01

Dec 2nd, 2024
50
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-02 15:20:20
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Arduino fire fighting robot */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Servo.h>  // Include Servo library for controlling the servo motor
  27. #include <IRremote.h> // Include IRremote library for infrared sensor functionality
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. // Pin Definitions  
  34. #define enA 5  // Enable1 L298 Pin enA  
  35. #define in1 6  // Motor1 L298 Pin in1  
  36. #define in2 7  // Motor1 L298 Pin in2  
  37. #define in3 8  // Motor2 L298 Pin in3  
  38. #define in4 9  // Motor2 L298 Pin in4  
  39. #define enB 10 // Enable2 L298 Pin enB  
  40. #define ir_R A0 // Right IR sensor pin  
  41. #define ir_F A1 // Front IR sensor pin  
  42. #define ir_L A2 // Left IR sensor pin  
  43. #define servoPin A4 // Servo motor pin  
  44. #define pump A5  // Pump control pin
  45.  
  46. // Global Variables  
  47. int Speed = 160; // Duty Cycle (to 255) for motor speed  
  48. int s1, s2, s3; // Sensor readings
  49.  
  50. // Instantiate Servo object
  51. Servo myServo;
  52.  
  53. void setup(void) {
  54.     // Begin serial communication  
  55.     Serial.begin(9600);  // Set baud rate to 9600 bps
  56.  
  57.     // Pin Modes  
  58.     pinMode(ir_R, INPUT);
  59.     pinMode(ir_F, INPUT);
  60.     pinMode(ir_L, INPUT);
  61.     pinMode(enA, OUTPUT);
  62.     pinMode(in1, OUTPUT);
  63.     pinMode(in2, OUTPUT);
  64.     pinMode(in3, OUTPUT);
  65.     pinMode(in4, OUTPUT);
  66.     pinMode(enB, OUTPUT);
  67.     pinMode(servoPin, OUTPUT);
  68.     pinMode(pump, OUTPUT);
  69.  
  70.     // Attach the servo to the pin
  71.     myServo.attach(servoPin);
  72.  
  73.     // Initialize Servo to sweep  
  74.     for (int angle = 90; angle <= 130; angle += 5) {
  75.         myServo.write(angle);
  76.         delay(15); // Allow time for the servo to reach the position
  77.     }
  78.     for (int angle = 130; angle >= 40; angle -= 5) {
  79.         myServo.write(angle);
  80.         delay(15); // Allow time for the servo to reach the position
  81.     }
  82.     for (int angle = 40; angle <= 95; angle += 5) {
  83.         myServo.write(angle);
  84.         delay(15); // Allow time for the servo to reach the position
  85.     }
  86.  
  87.     // Set initial motor speeds  
  88.     analogWrite(enA, Speed);
  89.     analogWrite(enB, Speed);
  90.    
  91.     delay(500); // Small delay for stability  
  92. }
  93.  
  94. void loop(void) {
  95.     // READ SENSOR VALUES  
  96.     s1 = analogRead(ir_R);
  97.     s2 = analogRead(ir_F);
  98.     s3 = analogRead(ir_L);
  99.  
  100.     // Debugging: Print Sensor Values  
  101.     Serial.print("Right: "); Serial.print(s1);
  102.     Serial.print("\tFront: "); Serial.print(s2);
  103.     Serial.print("\tLeft: "); Serial.println(s3);
  104.     delay(50);
  105.  
  106.     //-------------------------------------------------------------------
  107.     // Fire Detection and Response  
  108.     //-------------------------------------------------------------------
  109.  
  110.     if (s1 < 250) { // Fire detected on the right  
  111.         Stop();
  112.         digitalWrite(pump, HIGH); // Activate pump  
  113.         for (int angle = 90; angle >= 40; angle -= 3) {
  114.             myServo.write(angle);
  115.             delay(15); // Allow time for the servo to reach the position
  116.         }
  117.         for (int angle = 40; angle <= 90; angle += 3) {
  118.             myServo.write(angle);
  119.             delay(15); // Allow time for the servo to reach the position
  120.         }
  121.     } else if (s2 < 350) { // Fire detected at the front  
  122.         Stop();
  123.         digitalWrite(pump, HIGH); // Activate pump  
  124.         for (int angle = 90; angle <= 130; angle += 3) {
  125.             myServo.write(angle);
  126.             delay(15); // Allow time for the servo to reach the position
  127.         }
  128.         for (int angle = 130; angle >= 40; angle -= 3) {
  129.             myServo.write(angle);
  130.             delay(15); // Allow time for the servo to reach the position
  131.         }
  132.         for (int angle = 40; angle <= 90; angle += 3) {
  133.             myServo.write(angle);
  134.             delay(15); // Allow time for the servo to reach the position
  135.         }
  136.     } else if (s3 < 350) { // Fire detected on the left  
  137.         Stop();
  138.         digitalWrite(pump, HIGH); // Activate pump  
  139.         for (int angle = 90; angle <= 130; angle += 3) {
  140.             myServo.write(angle);
  141.             delay(15); // Allow time for the servo to reach the position
  142.         }
  143.         for (int angle = 130; angle >= 90; angle -= 3) {
  144.             myServo.write(angle);
  145.             delay(15); // Allow time for the servo to reach the position
  146.         }
  147.     } else { // No fire detected  
  148.         digitalWrite(pump, LOW); // Deactivate pump
  149.  
  150.         if (s1 >= 251 && s1 <= 700) {
  151.             backward();
  152.             delay(100);
  153.             turnright();
  154.             delay(200);
  155.         } else if (s2 >= 251 && s2 <= 800) {
  156.             forward();
  157.         } else if (s3 >= 251 && s3 <= 700) {
  158.             backward();
  159.             delay(100);
  160.             turnleft();
  161.             delay(200);
  162.         } else {
  163.             Stop();
  164.         }
  165.     }
  166.     delay(10);
  167. }
  168.  
  169. // Robot Movement Functions  
  170. void forward() {
  171.     digitalWrite(in1, HIGH);
  172.     digitalWrite(in2, LOW);
  173.     digitalWrite(in3, LOW);
  174.     digitalWrite(in4, HIGH);
  175. }
  176.  
  177. void backward() {
  178.     digitalWrite(in1, LOW);
  179.     digitalWrite(in2, HIGH);
  180.     digitalWrite(in3, HIGH);
  181.     digitalWrite(in4, LOW);
  182. }
  183.  
  184. void turnright() {
  185.     digitalWrite(in1, LOW);
  186.     digitalWrite(in2, HIGH);
  187.     digitalWrite(in3, LOW);
  188.     digitalWrite(in4, HIGH);
  189. }
  190.  
  191. void turnleft() {
  192.     digitalWrite(in1, HIGH);
  193.     digitalWrite(in2, LOW);
  194.     digitalWrite(in3, HIGH);
  195.     digitalWrite(in4, LOW);
  196. }
  197.  
  198. void Stop() {
  199.     digitalWrite(in1, LOW);
  200.     digitalWrite(in2, LOW);
  201.     digitalWrite(in3, LOW);
  202.     digitalWrite(in4, LOW);
  203. }
  204.  
  205. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement