Hi, I'm trying to build an app in MITAppInventor to control 6DOF Robotic Arm based on 4WD Car Platform with L298N motor drive controled by HC-05 module with Arduino Mega 2560 R3
I have successfully build app to control the 4WD car platform and second separate app to control one servo. Platform is working same like the app with one servo but I have 6 servos to control.
I read about SoftwareSerial and Servo conflict.
Do I need separate servo control board for this project? Is it possible to use only Arduino Mega 2560 R3 to control 6 servos and L298N via HC-05 and Android app?
I don't know the structure of Arduino sketch code for this project and how to program it. I've done app in MIT for this project attached below.
Please see and let me know.
The simplest code that I manage to work and build app for it was like below for car platform L298N control:
//no speed control
char t;
void setup() {
pinMode(12,OUTPUT); //left motors forward
pinMode(11,OUTPUT); //left motors reverse
pinMode(10,OUTPUT); //right motors forward
pinMode(9,OUTPUT); //right motors reverse
Serial.begin(38400);
}
void loop() {
if(Serial.available()){
t = Serial.read();
Serial.println(t);
}
if(t == 'F'){ //move forward all motors to rotate in forward direction
digitalWrite(12,HIGH);
digitalWrite(10,HIGH);
}
else if(t == 'B'){ //move reverse all motors rotate in reverse direction
digitalWrite(11,HIGH);
digitalWrite(9,HIGH);
}
else if(t == 'L'){ //turn right left side motors rotate in forward direction, right side motors doesn't rotate
digitalWrite(12,HIGH);
}
else if(t == 'R'){ //turn left right side motors rotate in forward direction, left side motors doesn't rotate
digitalWrite(10,HIGH);
}
else if(t == 'S'){ //stop all motors deactivated
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
}
delay(100);
}
and for only one servo control with slide bar that I tested and build app for it was:
// servo bluetooth controled slider control
#include <Servo.h>
Servo myservo;
const int Pin = 2; //
char Text;
String Spilt;
String angle;
int pos = 0; // variable to store the servo position
int k1;
void setup() {
Serial.begin(38400);
pinMode (Pin, OUTPUT);
myservo.attach(Pin);
}
void loop() {
if(Serial.available())
{
Text = Serial.read();
Spilt = Spilt + Text;
if (Text == '*') {
Serial.println(Spilt);
Spilt = Spilt.substring(0, Spilt.length() - 1); // Delete last char *
k1 = Spilt.indexOf('*');
angle = Spilt.substring(0, k1);
myservo.write(angle.toInt());
delay(15);
Spilt = "";
}
}
}
Everything was working and the apps I build was good too but the project is for 6 servos and one app controlling servos and car platform on one screen. So far I have two separate apps controlling one servo and the other to control car platform.
Hardware serial ports I use like below:
8-13 to connect and control L298N motor drive
7-2 to connect 6 servo signal pins
1-0 is for TX/RX for HC-05
That's all. I still have ''Digital Pins'' left but I'm not going to use them because its only 6 servo project.
I'm not familiar with servos. Ingeneral you should be creating an array of 6 servo objects.
For the servo, it looks like you're receiving an angle followed by a '*'. To control individual servos, you can send something like
num,angle*
where num is the number of the servo to control, angle is the required angle and '*' is the end of the command. The comma between num and angle separates the two numbers which makes it easier to parse.
Life is easy ( ) if you use 0..5 for num so it can act as the index into the array of servos.