MY Bluetooth control RC project

I just combined the code for a servo and a DC motor, but I found the dc not working.
I think the code is correct but not 100% sure. can somebody tell me if there are any mistakes in my code? thank you. when I send the values from my android and I can see the values arrive in Arduino. and I also measured the voltage has changed on the Nano board pins. I could not figure it out why the dc motor not working.

I use nano board. HC06 Bluetooth module, L298N motor driver. 12V power supply.

#include <SoftwareSerial.h>
//#include <ServoTimer2.h>
#include <Servo.h>



const int MAX_char = 80;
char dataBuff[MAX_char];
boolean newData = false;
//get values to servo and motor
String ctrlGrp;
String ctrlVal;
String inpMsg;

boolean motorData = false;
//Bluetooth device
SoftwareSerial BT(4, 12);  // RX, TX
//servo motor
Servo myservo;
int pwm;
int Position;

int servoAngle;
//motor driver L298N
int enA = 5;  //motor speed control pin
int in1 = 6;   //reverse or forward pins
int in2 = 7;
//int motorspeed = 0;

//speed control
//int a = 250;



void setup() {
  // put your setup code here, to run once:
  BT.begin(9600);
  Serial.begin(9600);
  myservo.attach(9);
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  //set motor stops
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  //analogWrite(enA, 0);
}

void loop() {


  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '?';
  char endMarker = '#';
  char rc;


  //motor_control();


  while (BT.available() > 0 && newData == false) {
    rc = BT.read();


    //Serial.println(rc);  //print char to check

    if (recvInProgress == true) {
      if (rc != endMarker) {
        dataBuff[ndx] = rc;
        ndx++;
        if (ndx >= MAX_char) {
          ndx = MAX_char - 1;
        }
      } else {
        dataBuff[ndx] = '\0';  // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
  if (newData == true) {

    inpMsg = (String)dataBuff;
    /*char* strtokIndx;
    strtokIndx = strtok(dataBuff, ',');
    Position = atoi(strtokIndx);*/

    //Serial.println(inpMsg);
    ctrlGrp = inpMsg.substring(0, inpMsg.indexOf(":"));   // Get the control component
    ctrlVal = inpMsg.substring(inpMsg.indexOf(":") + 1);  // Get the value of the control
    Serial.println(ctrlGrp + " :" + ctrlVal);
    if (ctrlGrp == "servo") {

      //Serial.println(Position);
      Position = ctrlVal.toInt();
      pwm = map(Position, 0, 180, 45, 135);

      myservo.write(pwm);
      delay(5);
    }

    if (ctrlGrp == "motor") {


      if (ctrlVal == "run") {
        digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);
        analogWrite(enA, 0); 
        delay(100);

        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        analogWrite(enA, 255);  // Default PWM Pulse
      } else if (ctrlVal == "stop") {

        digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);
        analogWrite(enA, 0);  // Default PWM Pulse
      }
    }





    newData = false;
  }
}

Please post a wiring diagram

As already told, please post schematics, not a bird nest Fritzing picture. No obvious mistake found in the code so far.

Have You tried using Serial.print inside the code, and serial monitor in the IDE? Print outs like "Start executed", "Stop executed etc. That's a powerful tool for debugging code.

Hi, Rail. I just upload a schematic. and if I just make a code block for Motor, it's working well. but if I combine the servo and motor, the motor will not show any sign of spinning. I double-checked the wirings and all connections are good. also, I check the values in Serial.println() coming correctly. I have no idea what causes the problem. I'm gonna use a mega2560 to try the same code and see if they work or not.

That's not schematics. It's a Fritzing, a colorful birdnest, as useful as a road map without names.
Guesses: The 5 volt converter on the L298 board is overloaded by the servo and the BT circuit. Give the servo and the BT a real power supply. Never use controllers and similar for supplying power.

Thank you , Rail. That’s a good guess. I’ll try it soon!

I just tried to power up the servo and Bluetooth with a external battery. And still getting no luck to get the motor running :worried:

Strange. Could You draw the wiring for that? Pen and paper often works well.

Rail. That’s strange. I just used the mega2560. The exactly same code on it running smoothly! But the nano board has been no any luck yet. :thinking:


I get the 5V from the L298n and give this to servo ,Bluetooth and nano board. This set did give any miracle to me.

Do You get thecurrent needed? Check the datasheet for the L298 board regarding the current capacity of that 5 volt converter. I really can't believe it is powerful enough.
That schematics makes no change. It's still the same 5 volt converter.
Mega works.... i find it hard to believe.
Last guess. The Nano pins, timers,.... are different than You think. Improper library used?

for sure, Rail. mega 2560 works perfectly. and I did not use any external power to servo and Bluetooth. I currently disable the code block to servo and the motor still doesn't work. another guess is that the Nano board is fried. :face_with_raised_eyebrow:

Try and get another Nano board.
There will be a 10 hour brake here. Real railroading today....

tried another Nano bord...still nothing happened

I probably have the answer. There must be a conflict there. I could not use the dc motor and servo at the same time. when I use any one of those devices individually, I get no issues.

That was good news. We can rule out colliding libraries. Then remains: this is a powering problem!
Get hold of one solid 5 volt power capable of some amps, say 5 - 10 amps and feed the dc motor, the servo and BT.

I give up on nano :melting_face: I use a mega2560 and I found there is a mega2560 mini.

Hrmm. Fine You found a way out of this, but....
Now reading the datasheets for the L298D I say You're out on deep water. The spec says this:
What does the l298n module consist of?
This module consists of an L298 motor driver IC and a 78M05 5V regulator.
The 78M05 supplies 1/2 amp according to its datasheet.
Please note this!
As I said earlier, give the external devices a proper power supply. Running Your setup for long it's a question of time before it will fail.

Thank you so much, Rail! The 78M05 5V regulator offering a half amp may not be enough for a servo and Bluetooth. I could give a better external power supply to them. But there's another reason that I don't want the Nano. The thing you mentioned earlier in the post. It might be an improper Libraries! As I said the code is working on a Mega2560 running smoothly, but Nano. :face_with_raised_eyebrow: I searched the whole forum and I do find out some clues about the conflict in running several things on a Nano at the same time. there must be a solution, but It takes too long to learn by myself :sweat_smile: So I think taking the Mega2560 will be a better choice instead of keeping using the Nano.

I've been working with electronics and programming for almost 50 years but only a few years with Arduinos, UNOs. Not much knowledge about the speciallity of the different types of controllers. Forum now and then solves issues where incompatble libraries and controller type was the reason. That's what I've picked in forum.
Yes, go for a more powerful supply of 5 volt. Good choice to use a Mega for the moment.
Good luck!