Switching between automatic and manual mode

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);
}



![image|500x500](upload://xp06E7P1LwZjkzKmOHlVhboswJh.jpeg)

the switch image

Then you need to design a third code that will allow the switching. That logic is not just something you can throw together over night.

yes i know I have to add a third code but how

Conceptually ?
Every so often the program has to look at a flag, say, to see if it's supposed to be in Manual or Auto.
How often will that be done, polled?

Welcome to the forum

What action will switch between auto and manual mode ?

And how will you change any switch or variable settings when a switch if needed? LOTS of logic needs to be considered when a switch is toggled.

The polling is done frequently many times per second ensuring that the program can quickly respond to changes in mode

I should
mention I dont have that much experience I'm still learning

toggling the mode switch will switch between auto and manual modes physically changing the position of the mode switch

also guys the vertical slide switch have 3 legs how it should be attached on the breadboard like is there a GND leg and leg for the digtial pin

Are you telling us you did not write the current programs.

You may be in for an interesting ride, as often control software must be 'tweaked' extensively for handling the transitions between manual and auto. Much detail missing here, including the purpose of the whole project, which matters very much, and the type/quality of the code being managed.

I did it with my friend but he did the most work

Does your friend not want to help any more?

bro we did it together ofc if he know he would told me

what details you want
its fire fighting robot

In automatic mode the robot finds fires using flame sensors and puts them out using a servo and water pump In manual mode Im using bluetooth module to control the robot with mobile phone using Bluetooth

A global variable mode flag and a conditional mode state in the loop will make manual and automatic modes available in the same sketch.

int mode = 0;

void setup() {
     // <insert setup code here>
}

void loop() {
     checkStateSwitch();
     if(mode == 0) {
          manualModeLoop();
     }
    else {
          automaticModeLoop();
     }
}

void manualModeLoop() {
     // The code for manual mode goes here 
}

void automaticModeLoop() {
     // The code for automatic mode goes here 
}

void checkStateSwitch() {
     // The code for checking the state of the mode switch goes here 
     // The code must assign mode = 0 for manual 
     // Anything other than 0 (1 is good) will “else” into the automatic mode, so assign mode = 1 for automatic mode 
}

This is how I combine multiple modes into my Arduino sketches and is a simple example of a state machine program.

If it's a toggle switch then it should be easy. I figured the mode was supposed to come in on the remote.

yes it vertical slide switch

Thanks guys it worked

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.