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;
}
}
}
}
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.
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;
}
}
}
}