Hi everyone,
For a plastic weight combat bot, I am using an L293D Motor Shield atop an Arduino Uno to run 2 small motors as a drive train and an ESC to run a drone motor, which hooks to the first servo port on the shield. I'm using the Servo library to help with the ESC and the AFMotor library to run the motor shield.
To control them, I am using an HC-06 Bluetooth module, which connects to my phone and will send instructions to the Arduino. To run the drive motors, it will send either F, L, R, B, or S to drive them in various directions. To control the drone motor, it will send a value between 0-10 setting its speed.
Currently, it will only spin the drone motor or use drive motors, and will not do both at the same time. I also have to send the instructions several times when trying to move in-between the two systems.
Both of these systems work perfectly fine separate, but I am struggling to combine them. My most recent code is below, as well as the two codes individually.
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!
#include <AFMotor.h>
#include <Servo.h>
Servo ESC; // create servo object to control the ESC
int val = 0;
// DC motor on M2
AF_DCMotor motor1(1);
AF_DCMotor motor2(3);
// DC hobby servo
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor party!");
// turn on motor #2
motor1.setSpeed(200);
motor2.setSpeed(200);
motor1.run(RELEASE);
motor2.run(RELEASE);
ESC.attach(10,1000,2000); // (pin, min pulse width, max pulse width in microseconds)
delay(1);
ESC.write(10); // This is supposed to arm the ESC
delay(5000);
Serial.println("Ready!");
}
void loop (){
if(Serial.available()>0)
{
char data= Serial.read(); // reading the data received from the bluetooth module
switch(data)
{
case 'F': motor1.run(FORWARD);motor2.run(FORWARD);break;
case 'B': motor1.run(BACKWARD);motor2.run(BACKWARD);break;
case 'L': motor1.run(BACKWARD);motor2.run(FORWARD);break;
case 'R': motor1.run(FORWARD);motor2.run(BACKWARD);break;
case 'S': motor1.run(RELEASE);motor2.run(RELEASE);break;
//case 'A': val = 9;
// case 'Q': val = 0;
default : break;
}
Serial.println(data);
}
while (Serial.available() > 0)
{
int val = Serial.parseInt();
Serial.println(val);
ESC.writeMicroseconds(val); //using val as the signal to esc
}
}
The two separate codes are below.
First is the code for the drive train. It is built from the Motor Party example code.
// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!
#include <AFMotor.h>
#include <Servo.h>
// DC motor on M2
AF_DCMotor motor1(1);
AF_DCMotor motor2(3);
// DC hobby servo
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor party!");
// turn on motor #2
motor1.setSpeed(200);
motor2.setSpeed(200);
motor1.run(RELEASE);
motor2.run(RELEASE);
void loop (){
if(Serial.available()>0)
{
char data= Serial.read(); // reading the data received from the bluetooth module
switch(data)
{
case 'F': motor1.run(FORWARD);motor2.run(FORWARD);break;
case 'B': motor1.run(BACKWARD);motor2.run(BACKWARD);break;
case 'L': motor1.run(BACKWARD);motor2.run(FORWARD);break;
case 'R': motor1.run(FORWARD);motor2.run(BACKWARD);break;
case 'S': motor1.run(RELEASE);motor2.run(RELEASE);break;
default : break;
}
Serial.println(data);
}
And here is the code for the ESC and brushless drone motor.
#include <Servo.h>
Servo ESC; // create servo object to control the ESC
int val = 10;
void setup() {
Serial.begin(9600);
ESC.attach(9,1000,2000); // (pin, min pulse width, max pulse width in microseconds)
delay(1);
ESC.write(10); // This is supposed to arm the ESC
delay(5000);
Serial.println("Ready!");
}
void loop() {
while (Serial.available() > 0)
{
int val = Serial.parseInt();
Serial.println(val);
ESC.writeMicroseconds(val); //using val as the signal to esc
}
}
I believe the issue comes down to the Serial.Read not being able to read the numbers I send, and the Serial.parseInt not being able to read the letters I send. I think this is why it gets hung up between the two, and is unable to seamlessly function.
Thank you for any help, I am lost!