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-20 08:24:13
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Develop an Arduino-based system that integrates */
- /* various sensors and actuators, allowing users to */
- /* monitor environmental conditions and automate */
- /* responses based on predefined thresholds. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Servo.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Servo object
- Servo myservo;
- // Variables
- int pos = 0;
- boolean fire = false;
- // Pin definitions
- #define Left A0
- #define Forward A1
- #define Right A2
- #define Back A3
- #define trigPinL 3
- #define echoPinL 9
- #define trigPinR 11
- #define echoPinR 12
- #define pump 13
- #define in1 2
- #define in2 4
- #define in3 7
- #define in4 8
- #define ena 5
- #define enb 6
- long duration, distance, UltraSensorL, UltraSensorR;
- int Cdistance, Rdistance, Ldistance, distanceR, distanceRight;
- void setup(void)
- {
- // Initialize serial communication
- Serial.begin(9600);
- // Set motor control pins as outputs
- pinMode(in1, OUTPUT);
- pinMode(in2, OUTPUT);
- pinMode(in3, OUTPUT);
- pinMode(in4, OUTPUT);
- pinMode(ena, OUTPUT);
- pinMode(enb, OUTPUT);
- // Set sensor input pins
- pinMode(Left, INPUT);
- pinMode(Forward, INPUT);
- pinMode(Right, INPUT);
- pinMode(Back, INPUT);
- // Attach the servo to pin 10 and set initial position
- myservo.attach(10);
- myservo.write(90);
- // Set ultrasonic sensor pins
- pinMode(trigPinL, OUTPUT);
- pinMode(echoPinL, INPUT);
- pinMode(trigPinR, OUTPUT);
- pinMode(echoPinR, INPUT);
- }
- void SonarSensor(int trigPinSensor, int echoPinSensor) {
- digitalWrite(trigPinSensor, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPinSensor, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPinSensor, LOW);
- duration = pulseIn(echoPinSensor, HIGH);
- distance = duration / 57; // Calculate distance in cm
- }
- void FORWARD() {
- analogWrite(ena, 80);
- analogWrite(enb, 80);
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- digitalWrite(in3, LOW);
- digitalWrite(in4, HIGH);
- }
- void Stop() {
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- digitalWrite(in3, LOW);
- digitalWrite(in4, LOW);
- }
- void TurnLeft() {
- analogWrite(ena, 100);
- analogWrite(enb, 100);
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- }
- void Tleft90() {
- TurnLeft();
- delay(650);
- }
- void Tleft180() {
- TurnLeft();
- delay(1500);
- }
- void TurnRight() {
- analogWrite(ena, 100);
- analogWrite(enb, 100);
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- digitalWrite(in3, LOW);
- digitalWrite(in4, HIGH);
- }
- void Tright90() {
- TurnRight();
- delay(750);
- }
- void put_off_fire() {
- delay(500);
- Stop();
- digitalWrite(pump, HIGH);
- delay(500);
- for (pos = 50; pos <= 130; pos += 1) {
- myservo.write(pos);
- delay(10);
- }
- for (pos = 130; pos >= 50; pos -= 1) {
- myservo.write(pos);
- delay(10);
- }
- digitalWrite(pump, LOW);
- myservo.write(90);
- if (digitalRead(Forward) == 0) {
- fire = false;
- }
- }
- void comparison(int r, int l) {
- if (r > 15 || l > 15) {
- if (r > l || r == l) {
- Tleft90();
- } else if (l > r) {
- Tright90();
- }
- } else if (r < 25 && l < 25) {
- Tleft180();
- }
- }
- void loop(void)
- {
- myservo.write(90);
- SonarSensor(trigPinL, echoPinL);
- UltraSensorL = distance;
- SonarSensor(trigPinR, echoPinR);
- UltraSensorR = distance;
- if (UltraSensorR < 10) {
- Tleft90();
- } else if (UltraSensorL < 10) {
- Tright90();
- } else {
- if (UltraSensorL > 30 || UltraSensorR > 30) {
- FORWARD();
- } else {
- SonarSensor(trigPinL, echoPinL);
- Rdistance = distance;
- SonarSensor(trigPinR, echoPinR);
- Ldistance = distance;
- comparison(Rdistance, Ldistance);
- Stop();
- delay(150);
- }
- }
- if (digitalRead(Left) == 0 && digitalRead(Right) == 0 && digitalRead(Forward) == 0 && digitalRead(Back) == 1) {
- Stop();
- } else if (digitalRead(Left) == 1) {
- Tleft90();
- } else if (digitalRead(Right) == 1) {
- Tright90();
- } else if (digitalRead(Back) == 0) {
- Tleft180();
- } else if (digitalRead(Forward) == 1 && analogRead(Forward) < 750) {
- FORWARD();
- } else {
- fire = true;
- }
- delay(300);
- while (fire == true) {
- put_off_fire();
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement