Hi there everyone,
First post here and I know it might be a bit much to ask, I'm trying to combine the following two programs below to make a single program. Separately the two programs work perfectly fine, first one just basic movement instructions and the second set is the program to control the claw/servo. I attempted to combine the two by just copying and pasting parts of the second program that I thought were needed. By doing this I was only able to have the servo motor running and the DC motors didn't move.
I am using an HC-05 bluetooth module, L293D motor driver and an arduino uno for this project.
First Code:
#include <AFMotor.h>
AF_DCMotor Motor1(1, MOTOR12_64KHZ);
AF_DCMotor Motor2(2, MOTOR12_64KHZ);
AF_DCMotor Motor3(3, MOTOR12_64KHZ);
AF_DCMotor Motor4(4, MOTOR12_64KHZ);
String readString;
void setup() {
Serial.begin(9600);
Motor1.setSpeed(250);
Motor2.setSpeed(250);
Motor3.setSpeed(250);
Motor4.setSpeed(250);
}
void loop() {
while(Serial.available()){
delay(50);
char c=Serial.read();
readString+=c;
}
//Forward Motion
if(readString.length()>0){
Serial.println(readString);
if (readString =="Forward"){
Motor1.run (BACKWARD); //In order to go in the forward posistion I want it must be set to backwards
Motor2.run (BACKWARD);
Motor3.run (BACKWARD);
Motor4.run (BACKWARD);
delay(400);
}
//Backward Motion
if (readString =="Reverse"){
Motor1.run (FORWARD); //Reverse motion to go backward
Motor2.run (FORWARD);
Motor3.run (FORWARD);
Motor4.run (FORWARD);
delay(400);
}
//Left Motion with only two motors running
if (readString =="Left"){
Motor3.run (BACKWARD);
Motor4.run (BACKWARD);
delay(400);
}
//Right Motion with only two motors running
if (readString =="Right"){
Motor1.run (BACKWARD);
Motor2.run (BACKWARD);
delay(400);
}
if (readString =="Stop"){
Motor1.run (RELEASE);
Motor2.run (RELEASE);
Motor3.run (RELEASE);
Motor4.run (RELEASE);
delay(400);
}
readString="";
}
}
Second Code:
#include <SoftwareSerial.h> // TX RX software library for bluetooth
#include <AFMotor.h>
#include <Servo.h> // servo library
Servo myservo; // servo name
int bluetoothTx = 11; // bluetooth tx to 11 pin
int bluetoothRx = 12; // bluetooth rx to 12 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
myservo.attach(10); // attach servo signal wire to pin 10
//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()> 0 ) // receive number from bluetooth
{
int servopos = bluetooth.read(); // save the received number to servopos
Serial.println(servopos); // serial print servopos current number received from bluetooth
myservo.write(servopos); // roate the servo the angle received from the android app
}
}