how do i switch between these 2 sketches to function one at a time?

Basically right now i'm working on a project where i need to build a robot crane with a claw attached to it that has 2 function. The first one is, the car have a line tracking function which i use IR sensors. The second function is a manual control function in which it will be controlled via bluetooth. For the claw/arm i have used servo motors. i have already done with the sketches for both function. is there a way where i can combine the two sketches and switch between them to function one at a time.

this is the first sketch ,the line tracking

int IRsensor1 = A0; //LEFT SENSOR
int IRsensor2 = A1; // CENTER SENSOR
int IRsensor3 = A2; // RIGHT SENSOR
int Sensorvalue1 = 0;
int Sensorvalue2 = 0;
int Sensorvalue3 = 0;
int MotorA = 12;
int EA = 10;
int MotorB = 13;
int EB = 11;

void setup() {
  pinMode(IRsensor1,INPUT);
  pinMode(IRsensor2,INPUT);
  pinMode(IRsensor3,INPUT);
   pinMode(MotorA, OUTPUT);
    pinMode(MotorB, OUTPUT);
  Serial.begin(9600);
  Serial.println("Program Starting"); 
  delay(100);
}

void loop() {
Sensorvalue1= analogRead(IRsensor1);
Sensorvalue2= analogRead(IRsensor2);
Sensorvalue3= analogRead(IRsensor3);
Serial.print("Sensorvalue1:");
Serial.println(Sensorvalue1);
Serial.print("Sensorvalue2:");
Serial.println(Sensorvalue2);
Serial.print("Sensorvalue3:");
Serial.println(Sensorvalue3);
delay(100);
 if ((Sensorvalue3 <= 250) & (Sensorvalue2 >= 250) & (Sensorvalue1 <= 250))
  {
    Serial.println("FORWARD");
    digitalWrite(MotorA, HIGH);
    analogWrite(EA, 130);
    digitalWrite(MotorB, HIGH);
    analogWrite(EB, 130);
  }
  else if ((Sensorvalue1 >= 250) & (Sensorvalue2 >= 250) & (Sensorvalue3 >= 250))
  {
    Serial.println("STOP");
    digitalWrite(MotorA, LOW);
    analogWrite(EA, 0);
    digitalWrite(MotorB, LOW);
    analogWrite(EB, 0);
    
  }
  else if ((Sensorvalue3 >= 250 ) & (Sensorvalue2 >= 250) & (Sensorvalue1 <= 250))
  {
    Serial.println("SHARP LEFT");
    digitalWrite(MotorA, HIGH);
    analogWrite(EA, 220);
    digitalWrite(MotorB, LOW);
    analogWrite(EB, 175);
  }
     else if ((Sensorvalue3 >= 250 ) & (Sensorvalue2 <= 250) & (Sensorvalue1 <= 250))
  {
    Serial.println("LEFT");
    digitalWrite(MotorA, HIGH);
    analogWrite(EA, 160);
    digitalWrite(MotorB, LOW);
    analogWrite(EB, 150);
  }
  else if ((Sensorvalue3 <= 250) & (Sensorvalue2 >= 250) & (Sensorvalue1 >= 250))
  {
    Serial.println("SHARP RIGHT");
    digitalWrite(MotorA, LOW);
    analogWrite(EA, 175);
    digitalWrite(MotorB, HIGH);
    analogWrite(EB, 220);
  }
   else if ((Sensorvalue3 <= 250) & (Sensorvalue2 <= 250) & (Sensorvalue1 >= 250))
   {
    Serial.println("RIGHT");
    digitalWrite(MotorA, LOW);
    analogWrite(EA, 150);
    digitalWrite(MotorB, HIGH);
    analogWrite(EB, 160);
}}

the second codes, manual control

#include <Servo.h>
Servo gripper;
Servo arm;
Servo arm2;
int MotorA = 12;
int EA = 10;
int MotorB = 13;
int EB = 11;
char incoming_byte = 0;
int value = 0;
int pos = 0;

void setup() {
  // put your setup code here, to run once:
gripper.attach(A3);
  arm.attach(A4);
  arm2.attach(A5);
   Serial.begin(9600);
}

void loop() {
  

{
  if (Serial.available() > 0)
  {
    incoming_byte = Serial.read();
    
if (incoming_byte == '9')
    {
      arm.write(80);
      arm2.write(10);
    }
    if (incoming_byte == '7')
    {
      arm.write(130);
      arm2.write(130);
    }
    if (incoming_byte == 'E')
    {
      gripper.write(150);
    }

    else if (incoming_byte == 'R')
    {
      gripper.write(0);
    }
    if (incoming_byte == 'F')
    {
      arm.write(5);
    }

    else if (incoming_byte == 'G')
    {
      arm.write(12);
    }
    else if (incoming_byte == 'H')
    {
      arm.write(17);
    }
    else if (incoming_byte == 'J')
    {
      arm.write(25);
    }
    else if (incoming_byte == 'K')
    {
      arm.write(30);
    }
    if (incoming_byte == 'C')
    {
      arm2.write(0);
    }
    else if (incoming_byte == 'V')
    {
      arm2.write(10);
    }
    else if (incoming_byte == 'B')
    {
      arm2.write(20);
    }
    else if (incoming_byte == 'N')
    {
      arm2.write(30);
    }
    else if (incoming_byte == 'M')
    {
      arm2.write(40);
    }
     else if (incoming_byte == '1')
    {
      arm2.write(90);
    }
     else if (incoming_byte == '8')
    {
      arm2.write(120);
    }
    ////////////Forward////////////
    if (incoming_byte == 'W')
    {
      digitalWrite(MotorA, HIGH);
      digitalWrite(MotorB, HIGH);
      analogWrite(EA, 255);
      analogWrite(EB, 255);
    }
    ////////////Slow Forward////////////
    if (incoming_byte == 'Q')
    {
      digitalWrite(MotorA, HIGH);
      digitalWrite(MotorB, HIGH);
      analogWrite(EA, 100);
      analogWrite(EB, 100);
    }
    ////////////Reverse////////////
    if (incoming_byte == 'S')
    {
      digitalWrite(MotorA, LOW);
      digitalWrite(MotorB, LOW);
      analogWrite(EA, 255);
      analogWrite(EB, 255);
    }
    ////////////Left////////////
    if (incoming_byte == 'A')
    {
      digitalWrite(MotorA, HIGH);
      digitalWrite(MotorB, LOW);
      analogWrite(EA, 255);
      analogWrite(EB, 255);
    }
    ////////////Right////////////
    if (incoming_byte == 'D')
    {
      digitalWrite(MotorA, LOW);
      digitalWrite(MotorB, HIGH);
      analogWrite(EA, 255);
      analogWrite(EB, 200);
    }
    ////////////STOP////////////
    if (incoming_byte == 'X')
    {
      digitalWrite(MotorA, LOW);
      digitalWrite(MotorB, LOW);
      analogWrite(EA, 0);
      analogWrite(EB, 0);
    }}}}

Switch between how? Using what to trigger the switch?

A very simplistic approach is to combine the code in setup functions and combine code in loop functions. Resolve any pin or variable name conflicts between both sketches.

Then in loop function...

if (Running sketch a) {
  //code from sketch a
} else if (Running sketch b) {
  //code from sketch b
}

Up to you to add the logic for how to decide if you are “running sketch x”.

A more nuanced approach would be to actually get both sketches to function simultaneously. But that requires that you first gain an understanding of how each works.

I've tried this method but the manual part doesn't seem to work.

here is the combined sketch:

int IRsensor1 = A0; //LEFT SENSOR
int IRsensor2 = A1; // CENTER SENSOR
int IRsensor3 = A2; // RIGHT SENSOR
int Sensorvalue1 = 0;
int Sensorvalue2 = 0;
int Sensorvalue3 = 0;
int MotorA = 12;
int EA = 10;
int MotorB = 13;
int EB = 11;
#include <Servo.h>
Servo gripper;
Servo arm;
Servo arm2;
char incoming_byte = 0;
int value = 0;
int pos = 0;


void setup() {
  gripper.attach(A3);
  arm.attach(A4);
  arm2.attach(A5);
  pinMode(IRsensor1,INPUT);
  pinMode(IRsensor2,INPUT);
 pinMode(IRsensor3,INPUT);
 pinMode(MotorA, OUTPUT);
 pinMode(MotorB, OUTPUT);
 Serial.begin(9600);
  
}

void loop()
{
  if (Serial.available() > 0)
  {
    incoming_byte = Serial.read();

    
  if (incoming_byte == 'Z') 
  {
 Sensorvalue1= analogRead(IRsensor1);
Sensorvalue2= analogRead(IRsensor2);
Sensorvalue3= analogRead(IRsensor3);
Serial.print("Sensorvalue1:");
Serial.println(Sensorvalue1);
Serial.print("Sensorvalue2:");
Serial.println(Sensorvalue2);
Serial.print("Sensorvalue3:");
Serial.println(Sensorvalue3);
delay(100);
    if ((Sensorvalue3 <= 250) & (Sensorvalue2 >= 250) & (Sensorvalue1 <= 250))
  {
    Serial.println("FORWARD");
    digitalWrite(MotorA, HIGH);
    analogWrite(EA, 130);
    digitalWrite(MotorB, HIGH);
    analogWrite(EB, 130);
  }
  else if ((Sensorvalue1 >= 250) & (Sensorvalue2 >= 250) & (Sensorvalue3 >= 250))
  {
    Serial.println("STOP");
    digitalWrite(MotorA, LOW);
    analogWrite(EA, 0);
    digitalWrite(MotorB, LOW);
    analogWrite(EB, 0);
    
  }
  else if ((Sensorvalue3 >= 250 ) & (Sensorvalue2 >= 250) & (Sensorvalue1 <= 250))
  {
    Serial.println("SHARP LEFT");
    digitalWrite(MotorA, HIGH);
    analogWrite(EA, 220);
    digitalWrite(MotorB, LOW);
    analogWrite(EB, 175);
  }
     else if ((Sensorvalue3 >= 250 ) & (Sensorvalue2 <= 250) & (Sensorvalue1 <= 250))
  {
    Serial.println("LEFT");
    digitalWrite(MotorA, HIGH);
    analogWrite(EA, 160);
    digitalWrite(MotorB, LOW);
    analogWrite(EB, 150);
  }
  else if ((Sensorvalue3 <= 250) & (Sensorvalue2 >= 250) & (Sensorvalue1 >= 250))
  {
    Serial.println("SHARP RIGHT");
    digitalWrite(MotorA, LOW);
    analogWrite(EA, 175);
    digitalWrite(MotorB, HIGH);
    analogWrite(EB, 220);
  }
   else if ((Sensorvalue3 <= 250) & (Sensorvalue2 <= 250) & (Sensorvalue1 >= 250))
   {
    Serial.println("RIGHT");
    digitalWrite(MotorA, LOW);
    analogWrite(EA, 150);
    digitalWrite(MotorB, HIGH);
    analogWrite(EB, 160);

  }
  else if (incoming_byte =='L')
 {
    
if (incoming_byte == '9')
    {
      arm.write(80);
      arm2.write(10);
    }
    if (incoming_byte == '7')
    {
      arm.write(130);
      arm2.write(130);
    }
    if (incoming_byte == 'E')
    {
      gripper.write(150);
    }

    else if (incoming_byte == 'R')
    {
      gripper.write(0);
    }
    if (incoming_byte == 'F')
    {
      arm.write(5);
    }

    else if (incoming_byte == 'G')
    {
      arm.write(12);
    }
    else if (incoming_byte == 'H')
    {
      arm.write(17);
    }
    else if (incoming_byte == 'J')
    {
      arm.write(25);
    }
    else if (incoming_byte == 'K')
    {
      arm.write(30);
    }
    if (incoming_byte == 'C')
    {
      arm2.write(0);
    }
    else if (incoming_byte == 'V')
    {
      arm2.write(10);
    }
    else if (incoming_byte == 'B')
    {
      arm2.write(20);
    }
    else if (incoming_byte == 'N')
    {
      arm2.write(30);
    }
    else if (incoming_byte == 'M')
    {
      arm2.write(40);
    }
     else if (incoming_byte == '1')
    {
      arm2.write(90);
    }
     else if (incoming_byte == '8')
    {
      arm2.write(120);
    }
    ////////////Forward////////////
    if (incoming_byte == 'W')
    {
      digitalWrite(MotorA, HIGH);
      digitalWrite(MotorB, HIGH);
      analogWrite(EA, 255);
      analogWrite(EB, 255);
    }
    ////////////Slow Forward////////////
    if (incoming_byte == 'Q')
    {
      digitalWrite(MotorA, HIGH);
      digitalWrite(MotorB, HIGH);
      analogWrite(EA, 100);
      analogWrite(EB, 100);
    }
    ////////////Reverse////////////
    if (incoming_byte == 'S')
    {
      digitalWrite(MotorA, LOW);
      digitalWrite(MotorB, LOW);
      analogWrite(EA, 255);
      analogWrite(EB, 255);
    }
    ////////////Left////////////
    if (incoming_byte == 'A')
    {
      digitalWrite(MotorA, HIGH);
      digitalWrite(MotorB, LOW);
      analogWrite(EA, 255);
      analogWrite(EB, 255);
    }
    ////////////Right////////////
    if (incoming_byte == 'D')
    {
      digitalWrite(MotorA, LOW);
      digitalWrite(MotorB, HIGH);
      analogWrite(EA, 255);
      analogWrite(EB, 200);
    }
    ////////////STOP////////////
    if (incoming_byte == 'X')
    {
      digitalWrite(MotorA, LOW);
      digitalWrite(MotorB, LOW);
      analogWrite(EA, 0);
      analogWrite(EB, 0);
    }    }}}}

zezai:
I've tried this method but the manual part doesn't seem to work.

here is the combined sketch:

Rather put all your code into loop() you should divide it up into short single-purpose functions. Then the code in loop() might be as short as this

void loop() {
   readSerial();
   if (mode == 'A') {
       automaticControl();
   }
   else {
      manualControl();
  }
}

Have a look at Planning and Implementing a Program

Also, don't do this

    }    }}}}

put every } on its own line and use the AutoFormat tool to indent the code consistently. It will be much easier to read.

...R

Dear zezai,

it is very good that you posted your latest sketch. There is some additional work you should do to make it easier for others to help you.

It is a peace of cake for you to describe the behaviour of your code what you can watch wat the code does.
It is tsill pretty easy for you to write what the code should do.

programming needs to obey the syntax-rules 100% otherwise you get errormessages.
There is a second thing: to make it easy to read for humans the code needs to be formatted in the established style.
This style will make it easier for you to read the code too.
Therefore there are some formatting rules. Fortunately standard-formatting can be done by just pressing Ctrl-T in the IDE.

The automated Ctrl-T-formatting will indent the code in the right way. Whenever code is indented the commands are only executed if the if-condition above is true.

That your code does not work yet is a problem of when certain-if-conditions are executed.

I'm not gonna point you directly on the line where the problem lays. If I would do so in combination with your attitude of "can somebody help me quick" this would prevent you from making optimal progress in your own programming knowledge. You may find this tedious. Yes it is. It_s my way of contribute to this forum.

Another way of getting instant feedback is to add debug-output through to the serial-monitor using

Serial.print();
Serial.println();

best regards Stefan

StefanL38:
Whenever code is indented the commands are only executed if the if-condition above is true.

The indenting of code is only for the convenience of the programmer. The compiler doesn't care. As far as the compiler is concerned the entire program could be on a single line.

That said I am strongly in favour of indented code and the use of the Auto Format tool.

...R

Hi Robin2,

this is a great community. There are a lot of attentive, watchful I eyes fidning almost any error.
I was thinking the right thing but writing something wrong.
Of course the compiler does not care at all about indenting. After using the automatic formatting which does indent
code that is placed inside a inf-condintion, this code inside will be more indentend than the if-condition and that is an easy way to discover wrong placed code through the indenting.

Thanks for correcting me.
best regards Stefan

zezai:
thank you guys for all the advice and help. I very much appreciate it. @robin2 by the means of you saying "divided the codes in short single function", does it means that i need to divide the 2 sketch into different void under the void loop?

I would consider 2 functions (each called from loop() ) to be the minimum. But there may be code that is common to the automatic and the manual modes and there would be no need to repeat it in both if it was put into its own function.

Have you studied the link I gave you. You should see from that that I prefer to have as many small functions as is necessary to make the code easy to maintain.

By the way the units are "functions" not "voids". The word "void" just indicates to the compiler that a particular function does not return a value.

...R

I sended a PM to zezai to explain how to use the forum. He replied to me by PM.

@Zezai: one purpose of this userforum is to learn from each other. Private messages make sense some time. Most of the communication should take place in the forum. I'm transferring the conversation back to the forum through quoting
the last message

Hi stefan, thank you for helping me with this project. yes indeed my knowledge about programming is still improvable, i have so much to learn. i'll be more detailed next time when asking for help or describing an error. thanks again for helping me, i appreciate it.

@i zezai:
if a compiler-error occurs the minimum what you can do is to copy and paste the error-message into a new post and ask for help. You should add a guessing what the compiler-error-message might mean.
Even this "start thinking about the meaning" will deliver new ideas to you what the error might be. Not in all cases but in a part.

So your "homework" is exactly that:
Find the compiler-error-message and post it here. Just posting your code gives the other additional work to do.
They have to copy your code maybe have to install additional libraries and then compile the code to see the error-message.

best regards Stefan