Hola estoy haciendo una excavadora con 4 servos(para el brazo) y 2 motores DC que los controlo con un L298n(oruga o ruedas), controlado por el modulo bluetooth HC-05 y un arduino uno. Tengo los dos códigos y cuando cargo el del brazo o el de la oruga individualmente funcionan.El problema viene cuando los he intentado unir a uno solo no funcionan y si alguien me puede guiar a como unirlos seria genial. Les agradezco su ayuda. Gracias
Acá dejo los códigos individuales.
Brazo excavadora
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo1, myservo2, myservo3, myservo4;
int bluetoothTx = 12;
int bluetoothRx = 13;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
myservo1.attach(2);
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(7);
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
}
void loop()
{
//Read from bluetooth and write to usb serial
if(bluetooth.available()>= 2 )
{
unsigned int servopos = bluetooth.read();
unsigned int servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 *256) + servopos;
Serial.println(realservo);
if (realservo >= 1000 && realservo <1180){
int servo1 = realservo;
servo1 = map(servo1, 1000,1180,0,180);
myservo1.write(servo1);
Serial.println("servo 1 ON");
delay(10);
}
if (realservo >=2000 && realservo <2180){
int servo2 = realservo;
servo2 = map(servo2,2000,2180,0,180);
myservo2.write(servo2);
Serial.println("servo 2 On");
delay(10);
}
if (realservo >=3000 && realservo < 3180){
int servo3 = realservo;
servo3 = map(servo3, 3000, 3180,0,180);
myservo3.write(servo3);
Serial.println("servo 3 On");
delay(10);
}
if (realservo >=4000 && realservo < 4180){
int servo4 = realservo;
servo4 = map(servo4, 4000, 4180,0,180);
myservo4.write(servo4);
Serial.println("servo 4 On");
delay(10);
}
}
}
Oruga o ruedas excavadora
#include <SoftwareSerial.h>
int motorA1 = 5; // Pin 2 L293
int motorA2 = 6; // Pin 7 L293
int motorB1 = 9; // Pin 10 L293
int motorB2 = 10; // Pin 14 L293
int vel = 170; // Velocidad Motores (0-255)
int estado = '0'; // Iniciar Motores
int bluetoothTx = 12;
int bluetoothRx = 13;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
pinMode(motorA1, OUTPUT);
pinMode(motorA2, OUTPUT);
pinMode(motorB1, OUTPUT);
pinMode(motorB2, OUTPUT);
}
void loop() {
if(bluetooth.available()>= 0 )
{
estado = bluetooth.read();
}
if(estado=='G'){ // Adelante
Serial.println(estado);
analogWrite(motorA1, vel);
analogWrite(motorA2, 0);
analogWrite(motorB1, vel);
analogWrite(motorB2, 0);
}
if(estado=='K'){ // Reversa
Serial.println(estado);
analogWrite(motorA1, 0);
analogWrite(motorA2, vel);
analogWrite(motorB1, 0);
analogWrite(motorB2, vel);
}
if(estado=='J'){ // Derecha
Serial.println(estado);
analogWrite(motorA1, vel);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, vel);
}
if(estado=='H'){ // Izquierda
Serial.println(estado);
analogWrite(motorA1, 0);
analogWrite(motorA2, vel);
analogWrite(motorB1, vel);
analogWrite(motorB2, 0);
}
if(estado=='I'){ // Detener
Serial.println(estado);
analogWrite(motorA1, 0);
analogWrite(motorA2, 0);
analogWrite(motorB1, 0);
analogWrite(motorB2, 0);
}
}