So in a project I'm doing I need to control 3 servos with a bluetooth controller in my cellphone (I'm using an app called Bluetooth Serial Controller by NEXT PROTOTYPES that I found on playstore).
I have 2 servos that rotate on 360º and one of 180º. The app basically has 3 buttons (FOWARD,REVERSE,CATCH), and each button sends a letter to the module (a,b,c), I do know that the module is working because in the serial monitor it reads "BT: a, BT: b, BT: c" but my servos still don't move.
Here is my code:
#include <Servo.h>
Servo patas1;
Servo patas2;
Servo cola;
int r = 8;
int g = 7;
int b = 6;
int ft = A5;
int valorft;
const int trigping = 5;
const int echoping = 4;
long duration, cm;
char RXchar;
void setup() {
pinMode(r, OUTPUT);
pinMode(g, OUTPUT);
pinMode(b, OUTPUT);
pinMode(ft, INPUT);
pinMode(trigping, OUTPUT);
pinMode(echoping, INPUT);
patas1.attach(9);
patas2.attach(10);
cola.attach(11);
Serial.begin(9600);
}
void loop() {
valorft = analogRead(ft); //LDR controla led azul
Serial.println(valorft);
delay(1000);
if (valorft <=200){
digitalWrite(b, HIGH);
}
else{
digitalWrite(b, LOW);
}
digitalWrite(trigping, LOW); //Sensor ultrasónico controla led roja
delayMicroseconds(2);
digitalWrite(trigping, HIGH);
delayMicroseconds(50);
digitalWrite(trigping, LOW);
duration = pulseIn(echoping, HIGH);
cm = (duration*0.034)/2;
Serial.print("Distance: ");
Serial.println(cm);
if (cm <= 10){
digitalWrite(r, HIGH);
}
else{
digitalWrite(r, LOW);
}
if (Serial.available()){ //Bluetooth que controla movimiento de patas y cola
RXchar = Serial.read();
Serial.print("BT: ");
Serial.println(RXchar);
}
if (RXchar == "a"){
patas1.write(0);
patas2.write(180);
}
else if (RXchar == "b"){
patas1.write(180);
patas2.write(0);
}
if (RXchar == "c"){
cola.write(180);
delay(500);
}
else{
cola.write(0);
delay(500);
}
}
Just ignore the other things, they are working for me so far, only the part that is at the end (bluetooth) is giving me problems right now...
And sorry if it's a really simple mistake, this is only my second arduino project so far and the first time I'm using bluetooth module, so I'm doing what I can haha....
if (RXchar == "a"){
patas1.write(0);
patas2.write(180);
}
else if (RXchar == "b"){
patas1.write(180);
patas2.write(0);
}
You write 0 to the patas1 servo then a few microseconds later write 180 to the patas1 servo. The servo has no time to move from where ever it was to 0 and no time to move from 0 to 180. You need some sort of delay between the write commands to give the servo time to move. The program does not wait for the servo to move to position. Same for the patas2 servo. A typical servo spec is around 0.2 seconds to move 60 degrees. Consult the data sheet for your servos to see what the spec is for them.
Let us operate your servos from Android Smart Phone via HC-5 Bluetooth using UNO Platform.
1. The Setup that I have made as test jig.
Figure-1: Connection among UNO, BT, and Servos
2. Upload the following sketch.
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3);
// DPin-2 of UNO is RX-pin; to receive data from TX-pin of HC-05
// DPin-3 of UNO is TX-pin; to transmit data to RX-pin of HC-05
#include <Servo.h>
Servo potas1;
Servo potas2;
Servo cola;
char x = ' ';
void setup()
{
Serial.begin(9600);
potas1.attach(9);
potas2.attach(10);
cola.attach(11);
Serial.println("Arduino is ready");
// HC-05 default serial speed for communication mode is 9600
BTserial.begin(9600);
Serial.println("BTserial started at 9600");
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTserial.available())
{
x = BTserial.read();
Serial.write(x);
if(x == 'a')
{
potas1.write(180);
}
if(x == 'b')
{
potas2.write(180);
}
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
x = Serial.read();
Serial.write(x);
BTserial.write(x);
}
}
3. Send a from Smart Phone. Check that SMA has rotated.
4. Send b from Smart Phone. Check that SMB has rotated.
5 Send a character from the InputBox of the Serial Monitor. Check that the character has appeared on the screen of the Smart Phone.
Wnen you make changes in your code, post the latest version so that we can keep up. A schematic drawing is much more usetul than a picture of a bunch of wires.