Servo Motor Control using bluetooth and ultra sonic sensor

I am doing a school project where I am making a Smart dustbin that can open its lid using a servo motor by an ultra-sonic sensor and Bluetooth module. but only the bluetooth part is working. kindly help me right my code.

code:

#include <SoftwareSerial.h>
#include <Servo.h>

Servo myServo;
int pos = 0;
const int trigPin = 5;
const int echoPin = 6;
const int led = 13;

long duration;
float distance;

int TxD = 11;
int RxD = 10;
int servoposition;
int servopos;
int new1;
SoftwareSerial bluetooth(TxD, RxD);

void setup() {
  Servo myservo;
  int pos = 0;
  myServo.attach(9);
  myServo.write(0);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  myservo.write(pos);
  Serial.begin(9600);       // start serial communication at 9600bps
  bluetooth.begin(9600);

}

void loop() {
  { Servo myservo;
    //Serial.begin(9600);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);
    distance = 0.034 * (duration / 2);
    //Serial.println(distance);
    if (distance < 27)
    {
      digitalWrite(led, HIGH);
      myservo.write(pos + 180);
      delay(1000);
    }
    else
    {
      digitalWrite(led, LOW);
      myservo.write(pos);
    }
    delay(300);
  }

  if (bluetooth.available())
  {
    String value = bluetooth.readString();
    servoposition = value.toInt();

    if (value.toInt() == 0)
    {
      Serial.println(servoposition);
      myServo.write(0);
    }

    if (value.toInt() == 45)
    {
      Serial.println(servoposition);
      myServo.write(45);
    }

    if (value.toInt() == 90)
    {
      Serial.println(servoposition);
      myServo.write(90);
    }

    if (value.toInt() == 135)
    {
      Serial.println(servoposition);
      myServo.write(135);
    }

    if (value.toInt() == 180)
    {
      Serial.println(servoposition);
      myServo.write(180);
    }

    while (value.toInt() == 1) {
      if (bluetooth.available())
      {
        value = bluetooth.readString();
        Serial.println(value);
        if (value.toInt() == 2)
        {
          Serial.println("YYY");
          break;
        }

      }

      servopos++;
      delay(30);
      Serial.println(servopos);
      myServo.write(servopos);



      if (servopos == 180 )
      {
        servopos = 0;
        break;
      }


    }

  }

}

"Right" is very old-fashioned; normally we'd say "correct".

Please remember to use code tags when posting code.

Maybe try using the IDE's auto-format tool to see what's wrong

Auto-format is showing no errors. thanks anyways

Maybe you could post the result anyway- it makes it easier to read your code - it's very badly formatted as it is now.

this is the code for servo motor control using Bluetooth module:

#include <SoftwareSerial.h>
#include <Servo.h>

Servo myServo;
int TxD = 11;
int RxD = 10;
int servoposition;
int servopos;
int new1;
SoftwareSerial bluetooth(TxD, RxD);

void setup() {
  int pos=0;
  myServo.attach(9);
  myServo.write(0);
  Serial.begin(9600);       // start serial communication at 9600bps
  bluetooth.begin(9600);
}

void loop() {
 if (bluetooth.available())
   {
    String value = bluetooth.readString();
    servoposition = value.toInt();

    if (value.toInt() == 0)
    {
      Serial.println(servoposition);
    myServo.write(0);
      }

      if (value.toInt() == 45)
    {
      Serial.println(servoposition);
    myServo.write(45);
      }

      if (value.toInt() == 90)
    {
      Serial.println(servoposition);
    myServo.write(90);
      }

      if (value.toInt() == 135)
    {
      Serial.println(servoposition);
    myServo.write(135);
      }

      if (value.toInt() == 180)
    {
      Serial.println(servoposition);
    myServo.write(180);
      }
      
   while(value.toInt()==1){
    if (bluetooth.available())
   {
    value = bluetooth.readString();
    Serial.println(value);
    if (value.toInt()==2)
    {Serial.println("YYY");  break; }
      
   }
   
    servopos++;
    delay(30);
    Serial.println(servopos);
    myServo.write(servopos);
  
 
    
    if (servopos ==180 )
    {servopos=0;break;}
 
    
  }

 }

}

and this is the code for servo motor control using ultra sonic sensor:

#include <Servo.h>
Servo myservo;  
int pos =0;  
const int trigPin = 5;
const int echoPin = 6;
const int led = 13;

long duration;
float distance;

void setup() 
{
  myservo.attach(9);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 
  pinMode(led, OUTPUT);
  myservo.write(pos);
}

void loop() 
{
  //Serial.begin(9600);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = 0.034*(duration/2);
  //Serial.println(distance);
  if (distance < 27)
  {
    digitalWrite(led,HIGH);
    myservo.write(pos+180);
    delay(1000);
  }
  else 
  {
    digitalWrite(led,LOW);
      myservo.write(pos);
  }
  delay(300);
}

both codes work perfectly on their own, I need to combine these 2 functions for my project.

The instantiation of myservo i.e. "Servo myservo;" should NOT be repeated in setup() and loop(). It should only be in one place, where it is in the working code.

There may be other errors but fix that first and post the new version of your combined code.

Steve

Hi
I tested your code here with minor modifications and the US part worked correctly.
It only has a very large delay when the distance is less than 27.
To test both BT and US, and since I don't have a BT module,
I changed the BT lines to use the serial.
And again everything worked correctly.

#include <SoftwareSerial.h>
#include <Servo.h>
Servo myServo;
int pos = 0;
const int trigPin = 5;
const int echoPin = 6;
const int led = 13;
long duration;
float distance;
int TxD = 11;
int RxD = 10;
int servoposition;
int servopos;
int new1;
SoftwareSerial bluetooth(TxD, RxD);
//-------------------------------------------------------------------------------------------
void setup() {
  Servo myservo;
  int pos = 0;
  myServo.attach(9);
  myServo.write(0);   
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  myservo.write(pos);  // ????
  Serial.begin(9600);       // start serial communication at 9600bps
  bluetooth.begin(9600);
}
//-------------------------------------------------------------------------------------------
void loop() {
  //  {                              //????
  Servo myservo;
  //Serial.begin(9600);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = 0.034 * (duration / 2);
  //Serial.println(distance);
  if (distance < 27)
  {
    digitalWrite(led, HIGH);
    //myservo.write(pos + 180);  // ????
    myservo.write(180);
    delay(1000);   // high delay is necessary ????
  Serial.println("Servo180");
  }
  else
  {
    digitalWrite(led, LOW);
    myservo.write(pos);
    Serial.println("Servo0");
  }
  delay(300);
  //  }                             //????
  if (bluetooth.available())
//   if(Serial.available())
  {
    String value = bluetooth.readString();
    //String value = Serial.readString();
    servoposition = value.toInt();
    if (value.toInt() == 0)
    {
      Serial.println(servoposition);
      myServo.write(0);
    }
    if (value.toInt() == 45)
    {
      Serial.println(servoposition);
      myServo.write(45);
    }
    if (value.toInt() == 90)
    {
      Serial.println(servoposition);
      myServo.write(90);
    }
    if (value.toInt() == 135)
    {
      Serial.println(servoposition);
      myServo.write(135);
    }
    if (value.toInt() == 180)
    {
      Serial.println(servoposition);
      myServo.write(180);
    }
    
    while (value.toInt() == 1) 
    {
      if (bluetooth.available())
      //if (Serial.available())
      {
        value = bluetooth.readString();
        //value = Serial.readString();
        Serial.println(value);
        if (value.toInt() == 2)
        {
          Serial.println("YYY");
          break;
        }
      }
      servopos++;
      delay(30);
      Serial.println(servopos);
      myServo.write(servopos);
      if (servopos == 180 )
      {
        servopos = 0;
        break;
      }
    }
  }
}

Serial monitor printout:

57
58
59
60
61
62
63
64
65
66
67
2

YYY
Servo0
Servo0
Servo0
Servo0
Servo0
Servo0
Servo0
Servo0
Servo0
Servo0
Servo0
Servo0
Servo0
Servo180
Servo180
Servo180
Servo180
Servo180
Servo180
Servo0
Servo0
Servo0
Servo0

the servo motor still doesn't operate when the code is uploaded

then the compiler says that myservo was not declared in this scope.
anyways this is the new code after removing my servo from the setup and loop

#include <SoftwareSerial.h>
#include <Servo.h>

Servo myServo;
int pos = 0;
const int trigPin = 5;
const int echoPin = 6;
const int led = 13;

long duration;
float distance;

int TxD = 11;
int RxD = 10;
int servoposition;
int servopos;
int new1;
SoftwareSerial bluetooth(TxD, RxD);

void setup() {
  
  int pos = 0;
  myServo.attach(9);
  myServo.write(0);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  myservo.write(pos);
  Serial.begin(9600);       // start serial communication at 9600bps
  bluetooth.begin(9600);

}

void loop() {
  { 
    //Serial.begin(9600);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);
    distance = 0.034 * (duration / 2);
    //Serial.println(distance);
    if (distance < 27)
    {
      digitalWrite(led, HIGH);
      myservo.write(pos + 180);
      delay(1000);
    }
    else
    {
      digitalWrite(led, LOW);
      myservo.write(pos);
    }
    delay(300);
  }

  if (bluetooth.available())
  {
    String value = bluetooth.readString();
    servoposition = value.toInt();

    if (value.toInt() == 0)
    {
      Serial.println(servoposition);
      myServo.write(0);
    }

    if (value.toInt() == 45)
    {
      Serial.println(servoposition);
      myServo.write(45);
    }

    if (value.toInt() == 90)
    {
      Serial.println(servoposition);
      myServo.write(90);
    }

    if (value.toInt() == 135)
    {
      Serial.println(servoposition);
      myServo.write(135);
    }

    if (value.toInt() == 180)
    {
      Serial.println(servoposition);
      myServo.write(180);
    }

    while (value.toInt() == 1) {
      if (bluetooth.available())
      {
        value = bluetooth.readString();
        Serial.println(value);
        if (value.toInt() == 2)
        {
          Serial.println("YYY");
          break;
        }

      }

      servopos++;
      delay(30);
      Serial.println(servopos);
      myServo.write(servopos);



      if (servopos == 180 )
      {
        servopos = 0;
        break;
      }


    }

  }

}

C++ is case-sensitive.

could it be the case that both modules are using pin 9 to send signal to servo moto

I'm sorry, I don't understand the question