I created a robot arm and placed them on wheels and want to control the motor and servos. I took code from 2 different projects.
Code to control motors with bluetooth:
// Starting of Program
#include <SoftwareSerial.h>
int Input_Pin_4 = 4;
int Input_Pin_3 = 5;
int Input_Pin_2 = 6;
int Input_Pin_1 = 7;
int enA = 3;
int enB = 2;
char Value;
int bluetoothTx = 10;
int bluetoothRx = 11;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
pinMode(Input_Pin_4, OUTPUT); // Digital pin 9
pinMode(Input_Pin_3, OUTPUT); // Digital pin 10
pinMode(Input_Pin_2, OUTPUT); // Digital pin 11
pinMode(Input_Pin_1, OUTPUT); // Digital pin 12
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop() {
while (bluetooth.available() > 0) {
Value = bluetooth.read();
Serial.println(Value);
}
if ( Value == 'F') {
// Robo Pe_t Run Forward
digitalWrite(enA, 255);
digitalWrite(enB, 255);
digitalWrite(Input_Pin_1, HIGH);
digitalWrite(Input_Pin_2, LOW);
digitalWrite(Input_Pin_3, HIGH);
digitalWrite(Input_Pin_4, LOW);
} else if (Value == 'B') {
//Robo Pet Run Backward
digitalWrite(enA, 255);
digitalWrite(enB, 255);
digitalWrite(Input_Pin_1, LOW);
digitalWrite(Input_Pin_2, HIGH);
digitalWrite(Input_Pin_3, LOW);
digitalWrite(Input_Pin_4, HIGH);
} else if (Value == 'L') {
//Robo Pet Turn Left
digitalWrite(enA, 255);
digitalWrite(Input_Pin_1, LOW);
digitalWrite(Input_Pin_2, LOW);
digitalWrite(Input_Pin_3, HIGH);
digitalWrite(Input_Pin_4, LOW);
} else if (Value == 'R') {
//Robo Pet Turn Right
digitalWrite(enB, 255);
digitalWrite(Input_Pin_1, HIGH);
digitalWrite(Input_Pin_2, LOW);
digitalWrite(Input_Pin_3, LOW);
digitalWrite(Input_Pin_4, LOW);
} else if (Value == 'S') {
//Robo Pet Stop
digitalWrite(Input_Pin_1, LOW);
digitalWrite(Input_Pin_2, LOW);
digitalWrite(Input_Pin_3, LOW);
digitalWrite(Input_Pin_4, LOW);
}
}
Code to control servo with bluetooth:
#include <Servo.h> // servo library
Servo myservo1, myservo2, myservo3; // servo name
int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
myservo1.attach(9); // attach servo signal wire to pin 9
myservo2.attach(12);
myservo3.attach(13);
//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(50);
}
if (realservo >= 2000 && realservo <2180) {
int servo2 = realservo;
servo2 = map(servo2, 2000, 2180, 0, 180);
myservo2.write(servo2);
Serial.println("Servo 2 ON");
delay(50);
}
if (realservo >= 3000 && realservo <3180) {
int servo3 = realservo;
servo3 = map(servo3, 3000, 3180, 0, 180);
myservo3.write(servo3);
Serial.println("Servo 3 ON");
delay(50);
}
}
}
I compiled them into one file:
#include <Servo.h> // servo library
Servo myservo1, myservo2, myservo3; // servo name
int motorOne = 3;
int motorOne2 = 4;
int motorTwo = 5;
int motorTwo2 = 6;
char Value;
int en1 = 2;
int en2 = 7;
int bluetoothTx = 10;
int bluetoothRx = 11;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
//initial motors pin
char command;
void setup()
{
myservo1.attach(9); // attach servo signal wire to pin 9
myservo2.attach(12);
myservo3.attach(13);
//Setup usb serial connection to computer
Serial.begin(9600);
bluetooth.begin(9600);
//Setup Bluetooth serial connection to android
}
void loop()
{
while (bluetooth.available() > 0) {
Value = bluetooth.read();
Serial.println(Value);
}
if ( Value == 'F') {
// Robo Pet Run Forward
digitalWrite(en1, HIGH);
digitalWrite(en2, HIGH);
digitalWrite(motorOne, HIGH);
digitalWrite(motorOne2, LOW);
digitalWrite(motorTwo, HIGH);
digitalWrite(motorTwo2, LOW);
} else if (Value == 'B') {
//Robo Pet Run Backward
digitalWrite(en1, HIGH);
digitalWrite(en2, HIGH);
digitalWrite(motorOne, LOW);
digitalWrite(motorOne2, HIGH);
digitalWrite(motorTwo, LOW);
digitalWrite(motorTwo2, HIGH);
} else if (Value == 'L') {
//Robo Pet Turn Left
digitalWrite(en1, HIGH);
digitalWrite(motorOne, HIGH);
digitalWrite(motorOne2, LOW);
digitalWrite(motorTwo, LOW);
digitalWrite(motorTwo2, LOW);
} else if (Value == 'R') {
//Robo Pet Turn Right
digitalWrite(en1, HIGH);
digitalWrite(motorTwo, HIGH);
digitalWrite(motorTwo2, LOW);
digitalWrite(motorOne, LOW);
digitalWrite(motorOne2, LOW);
} else if (Value == 'S') {
//Robo Pet Stop
digitalWrite(motorOne, LOW);
digitalWrite(motorOne2, LOW);
digitalWrite(motorTwo, LOW);
digitalWrite(motorTwo2, LOW);
}
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(50);
}
if (realservo >= 2000 && realservo <2180) {
int servo2 = realservo;
servo2 = map(servo2, 2000, 2180, 0, 180);
myservo2.write(servo2);
Serial.println("Servo 2 ON");
delay(50);
}
if (realservo >= 3000 && realservo <3180) {
int servo3 = realservo;
servo3 = map(servo3, 3000, 3180, 0, 180);
myservo3.write(servo3);
Serial.println("Servo 3 ON");
delay(50);
}
}
}
The motors respond and the servos don’t in the combined file. I tested the code for motors and servos in their separate files and they function without a problem. I found out the functions that read input for the motors and servos are mixing up their signals causing the problem.
Input reading function for car:
while (bluetooth.available() > 0) {
Value = bluetooth.read();
Serial.println(Value);
}
Input reading function for servos:
{
unsigned int servopos = bluetooth.read();
unsigned int servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 *256) + servopos;
Serial.println(realservo);
}
I’ve experimented by erasing one function and keeping the other, using code from another project. All other projects about bluetooth control read inputs the same way the servos and motors do. I’m a beginner coder and don’t know how to troubleshoot this properly.
Any tips & help would be much appreciated.
Thank you!