Issue with Serial.Read and Serial.parseInt using L293D shield and Drone Motor

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!

Use serial monitor and Serial.println for key data. Verify that the code receives and uses the data the way You would like.
How is data sent? ABCD+Cr+Lf or what? Cr + Lf often tricks people.

To control the drone motor, it will send a value between 0-10 setting its speed.

If you can use one less esc speed, you can send 0-9 as an ascii char and add to the switch case list.

char data= Serial.read();
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 '0': ESC.writeMicroseconds(0);break; 
        case '1': ESC.writeMicroseconds(1);break;
         .
         .
        case '9': ESC.writeMicroseconds(9);break;

        default : break;
       
      }

If this simple scheme doesn't meet your needs, you can review the Serial Input Basics Tutorialabout how to send multiple values in a single message and sort them out at the receiver.

The L293 is not the best choice for a driver. Post an annotated schematic as to how it is wired including all connections and power supply. You would be much further ahead if you used a motor driver bridge with MOSFET outputs, you would no longer lose about 3V when driving the motor.

Thank you so much!
This was exactly the quick solution I was looking for!

void loop (){

 if(Serial.available()>0)
   {     
  char data= Serial.read();
  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 '0': ESC.writeMicroseconds(0);break; 
        case '1': ESC.writeMicroseconds(1100);break;
        case '2': ESC.writeMicroseconds(1200);break;
        case '3': ESC.writeMicroseconds(1300);break;
        case '4': ESC.writeMicroseconds(1400);break;
        case '5': ESC.writeMicroseconds(1500);break;
        case '6': ESC.writeMicroseconds(1600);break;
        case '7': ESC.writeMicroseconds(1700);break;
        case '8': ESC.writeMicroseconds(1800);break;
        case '9': ESC.writeMicroseconds(1900);break;

        default : break;
     
      }
        Serial.println(data);
   }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.