BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Problem merging to independently working programs into 1 to switch modes at the press of a button

Wemos d1 mini clone
Android
• legacy app and local server
• Blynk Library version= 1.1.0
Hi m tying to merge to independent Arduino projects that work independently on their own. The first one is a simple line following car.

const int rightMotor = 14; // D5
const int leftMotor = 5; //D


const int rightSensor = 12; //D7
const int leftSensor = 13; // D8


void setup() {
  pinMode(rightMotor, OUTPUT);
  pinMode(leftMotor, OUTPUT);

  pinMode(rightSensor , INPUT);
  pinMode(leftSensor, INPUT);

  Serial.begin(115200);
}

void loop() {

 digitalWrite(rightMotor, !(digitalRead(rightSensor)));
 digitalWrite(!(leftMotor), !(digitalRead(leftSensor)));

}

The second one is a joystick-controlled car using blynk.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_AUTH_TOKEN ""
#define ipconfig ""
#define port 8080
#define leftMotor 5 // D3
#define rightMotor 14 //D4

int x=0;
int y=0;


BlynkTimer timer;
char auth[] =  BLYNK_AUTH_TOKEN; //Enter your Blynk auth token
char ssid[] = ""; 
char pass[] = ""; 



void setup() {
   pinMode(leftMotor, OUTPUT);
   pinMode(rightMotor, OUTPUT);
   digitalWrite(leftMotor, HIGH);
   digitalWrite(rightMotor, HIGH);
   Blynk.begin(auth, ssid, pass, ipconfig, 8080);
   timer.setInterval(500L, car);
   Serial.begin(115200);
  }

BLYNK_WRITE(V0) {
  x = param[0].asInt();
  y = param[1].asInt();
}
  

//Get the slider values

void car() {
  if (x==0 && y==0){
     stopda();
    }else
  if (y > 1) {
    forward();
    Serial.println("carForward");
  } else
  if (y < -1) {
    stopda();
    Serial.println("stop!");
  } else
  if (x < -1) {
    left();
    Serial.println("carLeft");
  } else
  if (x > 1) {
    right();
    Serial.println("carRight");
  } else
  if(y<=1 && y >=-1 && x>=-1 && x <=1) {
    stopda();
    Serial.println("carstop");
  } else{}
}
void loop() {
  Blynk.run();// Run the blynk function
  timer.run();
}



void forward(){
    digitalWrite(leftMotor,LOW);
    digitalWrite(rightMotor,LOW);
   }


 void left(){
   digitalWrite(leftMotor,HIGH);
   digitalWrite(rightMotor,LOW);
   }

 void right(){
   digitalWrite(leftMotor,LOW);
   digitalWrite(rightMotor,HIGH);
   }

  void stopda(){
    digitalWrite(leftMotor,HIGH);
    digitalWrite(rightMotor,HIGH);
   }

Both work perfectly independently.
My problem arises when i try to merge them into 1 function
Ive added a button to whose state is read and if pressed the linefollow() is executed and if its not pressed car() for joystick mode is executed

Now the joystick mode in this implemented version works fine but when the mode is switched to linefollow. it doesnt do anything

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_AUTH_TOKEN ""
#define ipconfig ""
#define port 8080
#define leftMotor 5 // D3
#define rightMotor 14 //D4
const int rightSensor = 12; //D7
const int leftSensor = 13;

int x=0;
int y=0;
int button1;



BlynkTimer timer;
char auth[] =  BLYNK_AUTH_TOKEN; //Enter your Blynk auth token
char ssid[] = ""; //Enter your WIFI name
char pass[] = ""; //Enter your WIFI passowrd



void setup() {
   pinMode(leftMotor, OUTPUT);
   pinMode(rightMotor, OUTPUT);
   
   pinMode(rightSensor , INPUT);
   pinMode(leftSensor, INPUT);
   Serial.begin(115200);
   Blynk.begin(auth, ssid, pass, ipconfig, 8080);
   timer.setInterval(500L, check);
  }

BLYNK_WRITE(V0) {
  x = param[0].asInt();
  y = param[1].asInt();
}
  

//Get the slider values
BLYNK_WRITE(V2) {
  button1 = param.asInt();
}  
BLYNK_WRITE(V1) {
  button2 = param.asInt();
}  
void car() {
  if (x==0 && y==0){
     stopda();
    }else
  if (y > 1) {
    forward();
    Serial.println("carForward");
  } else
  if (y < -1) {
    stopda();
    Serial.println("stop!");
  } else
  if (x < -1) {
    left();
    Serial.println("carLeft");
  } else
  if (x > 1) {
    right();
    Serial.println("carRight");
  } else
  if(y<=1 && y >=-1 && x>=-1 && x <=1) {
    stopda();
    Serial.println("carstop");
  } else{}
}
void loop() {
  
   
  Blynk.run();// Run the blynk function

  timer.run();
}



void forward(){
    digitalWrite(leftMotor,LOW);
    digitalWrite(rightMotor,LOW);
   }


 void left(){
   digitalWrite(leftMotor,HIGH);
   digitalWrite(rightMotor,LOW);
   }

 void right(){
   digitalWrite(leftMotor,LOW);
   digitalWrite(rightMotor,HIGH);
   }

  void stopda(){
    digitalWrite(leftMotor,HIGH);
    digitalWrite(rightMotor,HIGH);
   }

  void linefollow()
  {
     
     digitalWrite(rightMotor, !(digitalRead(rightSensor)));
     Serial.print(digitalRead(rightSensor));
     Serial.print( " ");
     digitalWrite(!(leftMotor), !(digitalRead(leftSensor)));
     Serial.println(digitalRead(leftSensor));
     

  }

  void check(){

    
    if(button1==0){
      
      car();
    }
     else if (button1==1){
     linefollow();
    }
   
 }

Should be 0.6.1

Physical buttons or app widgets?

If the latter, are they connected to virtual or digital pins, and which ones?

Pete.