Bluetooth button to start function with only button pressed.

Hi everyone, I am a new beginner with Arduino coding. Currently my project is to make a trash skimmer boat. I would like to control 4 DC motors by using bluetooth remote controller apps from phone. The Arduino UNO is connected to two motor drivers and each motor driver control two motors.

I have done the coding but now I would like to make the motors only run when button is pressed and stop running when button is without pressed from the apps. The bluetooth remote controller apps I use is with button (UP DOWN LEFT RIGHT CIRCLE CROSS TRIANGLE SQUARE). So any idea to solve my problem? :slight_smile:

Thank you.

#include<SoftwareSerial.h>
SoftwareSerial bt(2,4);//RX,TX

//LR Motor A
const int A1A = 6;
const int A1B = 9;
//FB Motor B
const int B1A = 3;
const int B1B = 5;
//Handling Motor C
const int C1A = 10;
const int C1B = 11;
//Handling Motor D
const int D1A = 12;
const int D1B = 13;


int state = 0;

void setup() 
{
  pinMode(B1A,OUTPUT);// define pin as output
  pinMode(B1B,OUTPUT);
  pinMode(A1A,OUTPUT);
  pinMode(A1B,OUTPUT);
  pinMode(C1A,OUTPUT);  
  pinMode(C1B,OUTPUT);  
  pinMode(D1A,OUTPUT);  
  pinMode(D1B,OUTPUT);      
  delay(3000);
  bt.begin(9600); // Default connection rate for my BT module
}

void loop() 
{
  if(bt.available() > 0)
    {
      state = (bt.read()); 
    }
//-------------------------------------    
  if(state =='F')//Forward
  {
    digitalWrite(A1A,LOW);
    digitalWrite(A1B,HIGH); 
  }
  else if (state =='B')//Backward
  {
    digitalWrite(A1A,HIGH);
    digitalWrite(A1B,LOW); 
  }
  else if(state =='R')//Right
    {
      digitalWrite(A1A,LOW);
      digitalWrite(A1B,HIGH); 
      digitalWrite(B1A,LOW);
      digitalWrite(B1B,HIGH); 
    }
  else if(state =='L')//Left
    {
      digitalWrite(A1A,LOW);
      digitalWrite(A1B,HIGH); 
      digitalWrite(B1A,HIGH);
      digitalWrite(B1B,LOW); 
    }
  else if(state =='U')// LIFT UP ARM
    {
      digitalWrite(C1A,LOW);
      digitalWrite(C1B,HIGH); 
      digitalWrite(D1A,LOW);
      digitalWrite(D1B,HIGH); 
    }
  else if(state =='D')// LIFT DOWN ARM
    {
      digitalWrite(C1A,HIGH);
      digitalWrite(C1B,LOW); 
      digitalWrite(D1A,HIGH);
      digitalWrite(D1B,LOW); 
    }
  else
  {
    digitalWrite(A1A,LOW);
    digitalWrite(A1B,LOW);
    digitalWrite(B1A,LOW);
    digitalWrite(B1B,LOW);    
  } 
}

motor_driver.ino (1.76 KB)

The OPs code posted in the preferred way

#include<SoftwareSerial.h>
SoftwareSerial bt(2, 4); //RX,TX

//LR Motor A
const int A1A = 6;
const int A1B = 9;
//FB Motor B
const int B1A = 3;
const int B1B = 5;
//Handling Motor C
const int C1A = 10;
const int C1B = 11;
//Handling Motor D
const int D1A = 12;
const int D1B = 13;


int state = 0;

void setup()
{
  pinMode(B1A, OUTPUT); // define pin as output
  pinMode(B1B, OUTPUT);
  pinMode(A1A, OUTPUT);
  pinMode(A1B, OUTPUT);
  pinMode(C1A, OUTPUT);
  pinMode(C1B, OUTPUT);
  pinMode(D1A, OUTPUT);
  pinMode(D1B, OUTPUT);
  delay(3000);
  bt.begin(9600); // Default connection rate for my BT module
}

void loop()
{
  if (bt.available() > 0)
  {
    state = (bt.read());
  }
  //-------------------------------------
  if (state == 'F') //Forward
  {
    digitalWrite(A1A, LOW);
    digitalWrite(A1B, HIGH);
  }
  else if (state == 'B') //Backward
  {
    digitalWrite(A1A, HIGH);
    digitalWrite(A1B, LOW);
  }
  else if (state == 'R') //Right
  {
    digitalWrite(A1A, LOW);
    digitalWrite(A1B, HIGH);
    digitalWrite(B1A, LOW);
    digitalWrite(B1B, HIGH);
  }
  else if (state == 'L') //Left
  {
    digitalWrite(A1A, LOW);
    digitalWrite(A1B, HIGH);
    digitalWrite(B1A, HIGH);
    digitalWrite(B1B, LOW);
  }
  else if (state == 'U') // LIFT UP ARM
  {
    digitalWrite(C1A, LOW);
    digitalWrite(C1B, HIGH);
    digitalWrite(D1A, LOW);
    digitalWrite(D1B, HIGH);
  }
  else if (state == 'D') // LIFT DOWN ARM
  {
    digitalWrite(C1A, HIGH);
    digitalWrite(C1B, LOW);
    digitalWrite(D1A, HIGH);
    digitalWrite(D1B, LOW);
  }
  else
  {
    digitalWrite(A1A, LOW);
    digitalWrite(A1B, LOW);
    digitalWrite(B1A, LOW);
    digitalWrite(B1B, LOW);
  }
}

@zeroyiq - not that the code has been Auto formatted in the IDE and posted using code tags

See Read this before posting a programming question