Switch into two program

hlw everyone . i'm trying to make a android control and a line follower robot. but i dont know how to switch into 2 program. can some one help me please :slight_smile:

code:

//Android Controll
//Line Follower

int en1 = 6; //motor 1  forward
int en2 = 9; // motor 1  backward
int en3 = 10; // motor 2  forward
int en4 = 11; // motor 2  backward
int ls = 3; //ir sensor L
int rs = 5; //ir sersor R

void setup() {
 pinMode(ls, INPUT);
 pinMode(rs, INPUT);
 pinMode(en1, OUTPUT);
 pinMode(en2, OUTPUT);
 pinMode(en3, OUTPUT);
 pinMode(en4, OUTPUT);
 Serial.begin(9600);
 while (! Serial);
 Serial.println("Enter direction:");
}

void loop() {
 if (Serial.available())
 {
   char ch = Serial.read();
   if (ch == 's')
   {
     digitalWrite(en1, HIGH);
     digitalWrite(en2, LOW);
     digitalWrite(en3, HIGH);
     digitalWrite(en4, LOW);

     Serial.print("back ");
   }
   if (ch == 'w')
   {
     digitalWrite(en1, LOW);
     digitalWrite(en2, HIGH);
     digitalWrite(en3, LOW);
     digitalWrite(en4, HIGH);

     Serial.print("forward ");
   }
   if (ch == 'a')
   {
     digitalWrite(en1, LOW);
     digitalWrite(en2, HIGH);
     digitalWrite(en3, HIGH);
     digitalWrite(en4, LOW);

     Serial.print("left ");
   }
   if (ch == 'd')
   {
     digitalWrite(en1, HIGH);
     digitalWrite(en2, LOW);
     digitalWrite(en3, HIGH);
     digitalWrite(en4, LOW);

     Serial.print("right ");
   }
   if (ch == 'z')
   {
     digitalWrite(en1, LOW);
     digitalWrite(en2, LOW);
     digitalWrite(en3, LOW);
     digitalWrite(en4, LOW);

     Serial.print("STOP ");
   }
   while(ch == 'x'){
     Serial.print("LINE FOLLOWER MODE ACTIVATE ");
 if(digitalRead(ls) && digitalRead(rs))     // Forward
 {
   digitalWrite(en1, LOW);
   digitalWrite(en2, HIGH);
   digitalWrite(en3, LOW);
   digitalWrite(en4, HIGH);
 }
 
 if(!(digitalRead(ls)) && digitalRead(rs))     // right
 {
   digitalWrite(en1, LOW);
   digitalWrite(en2, LOW);
   digitalWrite(en3, LOW);
   digitalWrite(en4, HIGH);
 }
 
 if(digitalRead(ls) && !(digitalRead(rs)))     //left
 {
   digitalWrite(en1, LOW);
   digitalWrite(en2, HIGH);
   digitalWrite(en3, LOW);
   digitalWrite(en4, LOW);
 }
 
 if(!(digitalRead(ls)) && !(digitalRead(rs)))     // stop
 {
   digitalWrite(en1, LOW);
   digitalWrite(en2, LOW);
   digitalWrite(en3, LOW);
   digitalWrite(en4, LOW);
 }
 
}
   }
         
 
}

ooo. Difficult read. Perhaps if your variables had descriptive names (like motor2ForwardPin perhaps) instead of en3 it would be more readable. Also, please read the 'how to use this forum' post at the top of the list. It will teach you how to put your code into a box for easy manipulation, among other useful things.

sorry for that. :slight_smile:
i'm new here...

darkboy:
sorry for that. :slight_smile:
i'm new here...

We are all new at something, so that's fine. If you edit your first post and at least use the code tags (from the how to ...) your code will get read and thought about by some of the major brainiacs here.

done sir :slight_smile:

i'm trying to make a android control and a line follower robot. but i dont know how to switch into 2 program.

I don't really understand this. Can you try explaining again?

MarkT:
I don't really understand this. Can you try explaining again?

i can switch from android control to line follower but i cant switch from line follower to android controller..

**sorry for my bad english

darkboy:
i can switch from android control to line follower but i cant switch from line follower to android controller..

**sorry for my bad english

Do you have two different programs or two different parts of your program? If you have two different programs, merge them together into one code. If that's already done, do something like this-

int pin = 3;
boolean switchState = true;

void setup() {

  pinMode(pin, INPUT_PULLUP);
}

void loop() {

  in pinState = digitalRead(pin);

  if (pin == LOW) {
    delay(10);
    switchState != switchState;
  }
  
  if (switchState == true) {

    androidContoller();

  }

  else {

    lineFollower();

  }

}

void androidController() {

}

void lineFollower() {

}

chummer1010:
Do you have two different programs or two different parts of your program? If you have two different programs, merge them together into one code. If that's already done...

sir i want to take value from serial monitor for program switching