Hello
I'm building a firefighting robot with both manual and automatic modes. The automatic mode operates independently using sensors, while the manual mode allows control via a mobile phone using Bluetooth. However, I'm unable to use both modes in a combined code . Currently, I have to upload different codes whenever I want to switch between modes. How can I add a switch to toggle between the two modes ? the switch I have is vertical slide switch
This is the automatic mode code :
/*------ Arduino Fire Fighting Robot Code by hobby project---- */
#include <Servo.h> //include servo.h library
Servo myservo;
int pos = 0;
boolean fire = false;
#define Left 9 // left sensor
#define Right 10 // right sensor
#define Forward 8 //front sensor
#define LM1 2 // left motor
#define LM2 3 // left motor
#define RM1 4 // right motor
#define RM2 5 // right motor
#define pump 6
void setup()
{
pinMode(Left, INPUT);
pinMode(Right, INPUT);
pinMode(Forward, INPUT);
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
pinMode(pump, OUTPUT);
myservo.attach(11);
myservo.write(90);
digitalWrite(pump,HIGH);
}
void put_off_fire()
{
delay (500);
digitalWrite(LM1, HIGH);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, HIGH);
digitalWrite(pump, LOW);
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,HIGH);
myservo.write(90);
fire=false;
}
void loop(){
myservo.write(90); //Sweep_Servo();
if (digitalRead(Left) == 1 && digitalRead(Right)==1 && digitalRead(Forward) ==1)
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, HIGH);
}
else if (digitalRead(Forward) ==0)
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
fire = true;
}
else if (digitalRead(Left) ==0)
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
else if (digitalRead(Right) ==0)
{
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, HIGH);
}
delay(300);//change this value to increase the distance
while (fire == true)
{
put_off_fire();
}
}
````Use code tags to format code for the forum`
**This is the manual mode code :**
#include <Servo.h>
// Motor pins
#define LM1 2 // Left motor
#define LM2 3 // Left motor
#define RM1 4 // Right motor
#define RM2 5 // Right motor
// Water pump and servo pins
#define pumpPin 6
#define servoPin 11
#define buzzerPin 12 // Define the pin for the buzzer
// Servo and water pump objects
Servo pumpServo;
// Flag to indicate if the servo is running
bool servoRunning = false;
// Flag to indicate if the water pump is running
bool pumpRunning = false;
void setup() {
pinMode(LM1, OUTPUT);
pinMode(LM2, OUTPUT);
pinMode(RM1, OUTPUT);
pinMode(RM2, OUTPUT);
pinMode(pumpPin, OUTPUT);
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
// Attach the servo to its pin
pumpServo.attach(servoPin);
// Start with the water pump and servo off
digitalWrite(pumpPin, HIGH); // Water pump off
pumpServo.write(90); // Set the servo angle to the default position
Serial.begin(9600); // Start serial communication
}
void loop() {
// Check for commands from the app
if (Serial.available() > 0) {
char command = Serial.read();
if (command == 'W') { // Start the water pump
digitalWrite(pumpPin, LOW); // Water pump on
pumpRunning = true; // Set the flag to indicate pump is running
} else if (command == 'w') { // Stop the water pump
digitalWrite(pumpPin, HIGH); // Water pump off
pumpRunning = false; // Set the flag to indicate pump is stopped
} else if (command == 'V') { // Rotate servo left by 90 degrees
pumpServo.write(0); // Move the servo to 0 degrees (left)
servoRunning = true; // Set the flag to indicate servo is running
} else if (command == 'v') { // Rotate servo right by 90 degrees
pumpServo.write(180); // Move the servo to 180 degrees (right)
servoRunning = true; // Set the flag to indicate servo is running
} else if (command == 'N') { // Return servo to normal position
pumpServo.write(90); // Move the servo to 90 degrees (default position)
servoRunning = false; // Set the flag to indicate servo is stopped
} else if (command == 'F') { // Move forward
moveForward();
} else if (command == 'B') { // Move backward
moveBackward();
} else if (command == 'L') { // Turn left
turnLeft();
} else if (command == 'R') { // Turn right
turnRight();
} else if (command == 'S') { // Stop the robot
stopRobot();
} else if (command == 'P') { // Activate the buzzer
digitalWrite(buzzerPin, HIGH);
delay(500); // Adjust this delay as needed
digitalWrite(buzzerPin, LOW);
}
}
}
// Move forward function
void moveForward() {
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
// Move backward function
void moveBackward() {
digitalWrite(LM1, LOW);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, LOW);
digitalWrite(RM2, HIGH);
}
// Turn left function
void turnLeft() {
digitalWrite(LM1, HIGH);
digitalWrite(LM2, HIGH);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, LOW);
}
// Turn right function
void turnRight() {
digitalWrite(LM1, HIGH);
digitalWrite(LM2, LOW);
digitalWrite(RM1, HIGH);
digitalWrite(RM2, HIGH);
}
// Stop the robot function
void stopRobot() {
digitalWrite(LM1, LOW);
digitalWrite(LM2, LOW);
digitalWrite(RM1, LOW);
digitalWrite(RM2, LOW);
}

the switch image