Advertisement
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: **Robot Control**
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-12-02 15:20:20
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Arduino fire fighting robot */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Servo.h> // Include Servo library for controlling the servo motor
- #include <IRremote.h> // Include IRremote library for infrared sensor functionality
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Pin Definitions
- #define enA 5 // Enable1 L298 Pin enA
- #define in1 6 // Motor1 L298 Pin in1
- #define in2 7 // Motor1 L298 Pin in2
- #define in3 8 // Motor2 L298 Pin in3
- #define in4 9 // Motor2 L298 Pin in4
- #define enB 10 // Enable2 L298 Pin enB
- #define ir_R A0 // Right IR sensor pin
- #define ir_F A1 // Front IR sensor pin
- #define ir_L A2 // Left IR sensor pin
- #define servoPin A4 // Servo motor pin
- #define pump A5 // Pump control pin
- // Global Variables
- int Speed = 160; // Duty Cycle (to 255) for motor speed
- int s1, s2, s3; // Sensor readings
- // Instantiate Servo object
- Servo myServo;
- void setup(void) {
- // Begin serial communication
- Serial.begin(9600); // Set baud rate to 9600 bps
- // Pin Modes
- pinMode(ir_R, INPUT);
- pinMode(ir_F, INPUT);
- pinMode(ir_L, INPUT);
- pinMode(enA, OUTPUT);
- pinMode(in1, OUTPUT);
- pinMode(in2, OUTPUT);
- pinMode(in3, OUTPUT);
- pinMode(in4, OUTPUT);
- pinMode(enB, OUTPUT);
- pinMode(servoPin, OUTPUT);
- pinMode(pump, OUTPUT);
- // Attach the servo to the pin
- myServo.attach(servoPin);
- // Initialize Servo to sweep
- for (int angle = 90; angle <= 130; angle += 5) {
- myServo.write(angle);
- delay(15); // Allow time for the servo to reach the position
- }
- for (int angle = 130; angle >= 40; angle -= 5) {
- myServo.write(angle);
- delay(15); // Allow time for the servo to reach the position
- }
- for (int angle = 40; angle <= 95; angle += 5) {
- myServo.write(angle);
- delay(15); // Allow time for the servo to reach the position
- }
- // Set initial motor speeds
- analogWrite(enA, Speed);
- analogWrite(enB, Speed);
- delay(500); // Small delay for stability
- }
- void loop(void) {
- // READ SENSOR VALUES
- s1 = analogRead(ir_R);
- s2 = analogRead(ir_F);
- s3 = analogRead(ir_L);
- // Debugging: Print Sensor Values
- Serial.print("Right: "); Serial.print(s1);
- Serial.print("\tFront: "); Serial.print(s2);
- Serial.print("\tLeft: "); Serial.println(s3);
- delay(50);
- //-------------------------------------------------------------------
- // Fire Detection and Response
- //-------------------------------------------------------------------
- if (s1 < 250) { // Fire detected on the right
- Stop();
- digitalWrite(pump, HIGH); // Activate pump
- for (int angle = 90; angle >= 40; angle -= 3) {
- myServo.write(angle);
- delay(15); // Allow time for the servo to reach the position
- }
- for (int angle = 40; angle <= 90; angle += 3) {
- myServo.write(angle);
- delay(15); // Allow time for the servo to reach the position
- }
- } else if (s2 < 350) { // Fire detected at the front
- Stop();
- digitalWrite(pump, HIGH); // Activate pump
- for (int angle = 90; angle <= 130; angle += 3) {
- myServo.write(angle);
- delay(15); // Allow time for the servo to reach the position
- }
- for (int angle = 130; angle >= 40; angle -= 3) {
- myServo.write(angle);
- delay(15); // Allow time for the servo to reach the position
- }
- for (int angle = 40; angle <= 90; angle += 3) {
- myServo.write(angle);
- delay(15); // Allow time for the servo to reach the position
- }
- } else if (s3 < 350) { // Fire detected on the left
- Stop();
- digitalWrite(pump, HIGH); // Activate pump
- for (int angle = 90; angle <= 130; angle += 3) {
- myServo.write(angle);
- delay(15); // Allow time for the servo to reach the position
- }
- for (int angle = 130; angle >= 90; angle -= 3) {
- myServo.write(angle);
- delay(15); // Allow time for the servo to reach the position
- }
- } else { // No fire detected
- digitalWrite(pump, LOW); // Deactivate pump
- if (s1 >= 251 && s1 <= 700) {
- backward();
- delay(100);
- turnright();
- delay(200);
- } else if (s2 >= 251 && s2 <= 800) {
- forward();
- } else if (s3 >= 251 && s3 <= 700) {
- backward();
- delay(100);
- turnleft();
- delay(200);
- } else {
- Stop();
- }
- }
- delay(10);
- }
- // Robot Movement Functions
- void forward() {
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- digitalWrite(in3, LOW);
- digitalWrite(in4, HIGH);
- }
- void backward() {
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- }
- void turnright() {
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- digitalWrite(in3, LOW);
- digitalWrite(in4, HIGH);
- }
- void turnleft() {
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- }
- void Stop() {
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- digitalWrite(in3, LOW);
- digitalWrite(in4, LOW);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement