Control of two DC motors and two servos

Hello, I have a problem.

I want to control two DC motors and two servos with a bluetooth device conected to my arduino UNO. I have alraedy tested both motors and they work perfect with an L293D H-bridge and an android app I made with a youtube tutorial.

The problem I have is that I don’t know how to connect the servos to the same system or to modify my code to mi particular need.

Can anybody help me?

Thank you.

/* Carro Inalambrico Bluetooth

ARDUINO L293D(Puente H)
5 10
6 15
9 7
10 2
5V 1, 9, 16
GND 4, 5, 12, 13

El motor 1 se conecta a los pines 3 y 6 del Puente H
El motor 2 se conecta a los pines 11 y 14 del Puente H

La fuente de alimentacion de los Motores se conecta a tierra y
el positivo al pin 8 del puennte H.

Conexion del Modulo Bluetooth HC-06 y el Arduino
ARDUINO Bluetooth HC-06
0 (RX) TX
1 (TX) RX
5V VCC
GND GND
!!Cuidado!! Las conexiones de TX y RX al modulo Bluetooth deben estar desconectadas
en el momento que se realiza la carga del codigo (Sketch) al Arduino.

*/

int izqA = 5;
int izqB = 6;
int derA = 9;
int derB = 10;
int vel = 255; // Velocidad de los motores (0-255)
int estado = ‘g’; // inicia detenido

void setup() {
Serial.begin(9600); // inicia el puerto serial para comunicacion con el Bluetooth
pinMode(derA, OUTPUT);
pinMode(derB, OUTPUT);
pinMode(izqA, OUTPUT);
pinMode(izqB, OUTPUT);
}

void loop()
{

if(Serial.available()>0){ // lee el bluetooth y almacena en estado
estado = Serial.read();
}
if(estado==‘a’){ // Boton desplazar al Frente
analogWrite(derB, 0);
analogWrite(izqB, 0);
analogWrite(derA, vel);
analogWrite(izqA, vel);
}
if(estado==‘b’){ // Boton IZQ
analogWrite(derB, vel);
analogWrite(izqB, 0);
analogWrite(derA, 0);
analogWrite(izqA, vel);
}
if(estado==‘c’){ // Boton Parar
analogWrite(derB, 0);
analogWrite(izqB, 0);
analogWrite(derA, 0);
analogWrite(izqA, 0);
}
if(estado==‘d’){ // Boton DER
analogWrite(derB, 0);
analogWrite(izqB, vel);
analogWrite(izqA, 0);
analogWrite(derA, vel);
}

if(estado==‘e’){ // Boton Reversa
analogWrite(derA, 0);
analogWrite(izqA, 0);
analogWrite(derB, vel);
analogWrite(izqB, vel);
}
if (estado ==‘f’){ // Boton ON se mueve sensando distancia

}
if (estado==‘g’){ // Boton OFF, detiene los motores no hace nada
}
}

Remote_control.ino (2.22 KB)

Have you looked at any of the Servo examples? What part is confusing you? What do you want the servos to do?

Delta_G: Have you looked at any of the Servo examples? What part is confusing you? What do you want the servos to do?

I want to control them with the same Android app I'm controlling the motors. The motors are being controlled with foward, back, left, right, buttons. I want to control the servos with a slider for each one of them. As you can see in the code, I am aussing states "a", "b", "c" and "d" to send orders to the motors via TX an RX pins of my Arduino, but I dont know how to send integer signals through the same pins.

I have seen the knob example of the servo library, and I understand that I have to map the values of my signal from 0 to 180 toturn the servo to my desired angle.

davidl8a: I want to control them with the same Android app I'm controlling the motors.

This is an app you made?

It sounds like you want to control the servos proportionally but you don't know how to send integers from the Android, is this right?

How are you programming the app? I don't suppose you're using App Inventor 2?

Have you seen "Joystick BT Commander"? It's a free app with a joystick and several buttons. It sends integers over the Bluetooth connection. You might want to see if it would work for your application.

I used this tutorial to make the app to control the motors.

I just thought that adding two sliders programmed to send integers I could control the servos.

The same bluetooth con send both string and integer values to the TX and RX pins of my Arduinos? In that case, will it work if I use a line like this one?

if (realservo >= 1000 && realservo <1180){
int servo1 = realservo;
servo1 = map(servo1, 1000,1180,0,180);
myservo1.write(servo1);
delay(10);

It means that a value between 1000 and 1180 sent to servo1 (attached to any PWM pin) is mapped to 0-180. And fot servo 2 it should be 2000-2180.

Why not send a servo identifier and a value between 0 and 180 such as A120 or B90 ? Then you can easily write code on the Arduino to identify which servo (A or B) should be moved to which position.

I want to control two DC motors and two servos

You may want to read the first paragraph of the library reference page for the Servo library.

davidl8a:
I just thought that adding two sliders programmed to send integers I could control the servos.

The same bluetooth con send both string and integer values to the TX and RX pins of my Arduinos?

Yes, you can send both strings and integers (the integers will likely be sent as a string of ASCII characters). This is pretty easy to do with AI2.

I haven’t used AI2 apps to send data to an Arduino myself yet but I’ve used it with other microcontrollers. You can actually send either ASCII characters or the raw data values but it’s a lot easier on the Arduino side to deal with ASCII characters.

You can use UKHeliBob’s idea and add slider value to some identifying character. Use the “join” block to add your slider value to a string “A” (or whichever character you choose). This “join” block would be attached to the string variable you send with your Bluetooth send call.

Let us know if you have trouble. I’m not on a computer where I can do a screenshot of the AI2 environment but if you get stuck I can show you just what I’m talking about.