Dc motor control

Hello.

I'm quite new in learning Arduino and writing a sketch. So forgive me as I might not use the right terms as I have still not learned them. But I will do my best to explain.

I have made my own little project converting a RC car with broken IR remote control. Instead I have connected a HC-06 Bluetooth module to an Arduino Micro to the hardware serial ports. After following several tutorials I did manage to get it to work so I can now control the motor turning direction and the speed from an app on my cell phone. So far so good. But I still miss a bit of knowledge to make if work as I like to but I have now unsuccessful searched the Internet for answers.

I can get the the car to move by sending a 1 or 2 from my phone to the Arduino using reading the serial1 input and using the if code. The result is that the motors start running but the motor never stop. So I have searched to find a solution to get the motor to stop running but have not found it. My idea is that the motor should stop running if there is not stored a 1 or 2 in the buffer. But how do I write the code, if it is possible at all? If its not possible to do it this way how would you then do it?

Edit: Sketch added if anyone would like to see it.

// connect BT module TX to Arduino micro
// connect BT module RX to Arduino micro via a voltage divider
// connect BT Vcc to 5V, GND to GND
// connect motor controller pins to Arduino digital pins

// motor A, drive
int enA = 10;
int in1 = 11;
int in2 = 12;

//Motor B, steering
int enB = 4;
int in3 = 5;
int in4 = 6;

// stores incoming character from cell phone in the buffer
char a;

void setup()
{
  // set all the motor control pins to outputs
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  // set the data rate for the Serial1 port
  Serial1.begin(9600);
}

void loop()
{
  if (Serial1.available())
    // if number arrived in from serial1...
  {
    a = (Serial1.read());
    if (a == '1')
    {
      // this function will run the motor in forward direction at a fixed speed
      // turn on motor A
      // set speed to 128 out of possible range 128~255
      analogWrite(enA, 128);
      digitalWrite(11, HIGH);
      digitalWrite(12, LOW);
      delay(100);
    }
    if (a == '2')
    {
      // This function will run the motor in reverse direction at a fixed speed
      // turn on motor A
      // set speed to 128 out of possible range 128~255
      analogWrite(enA, 128);
      digitalWrite(11, LOW);
      digitalWrite(12, HIGH);
      delay(100);
    }
    if (a == '3')
    {
      // This function will turn the DC steering motor in right direction
      // set speed to 128 out of possible range 128~255
      analogWrite(enB, 128);
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      delay(100);
    }
    if (a == '4')
    {
      // This function will turn the DC steering motor in left direction
      // set speed to 128 out of possible range 128~255
      analogWrite(enB, 128);
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);
      delay(100);
    }
  }
}

The problem is in line 42 of the code that you forgot to post.

Please post using code tags and the Arduino IDE autoformat feature.

Thanks!

vaj4088: The problem is in line 42 of the code that you forgot to post.

Please post using code tags and the Arduino IDE autoformat feature.

Thanks!

I'm very sorry that I do not understand what you mean by line 42. But you are right, I did not post the code which also is the reason for me not to understand how you can state that line 42 contain a problem.

The line 42 thing was a joke that apparently failed. If you want to get it, try reading Douglas Adams some time, but it really doesn't matter. The point is, I can't be helpful without the code. Now, you have editted your post to add the code. Thanks!

Once started, this code will keep the motors going. Add another number (say, 9) and the relevant code to turn the motors off.

Why would you think that the motors would turn off once started? Once you do an analogWrite(...) or a digitalWrite(...), nothing changes until you do another analogWrite(...) or digitalWrite(...) (or something drastic such as a reset).

Not that it is relevant here, but the variable "a" still has the last character in it. In this program, putting something else into "a" at the end won't change anything.

Hi. No hard feelings from my side. It's OK with a joke even I did not understood it. :)

I will try to explain one more time what I would like to archive. I'm not looking for using another button. I have no problem making this. This also means that I'm fully aware about that as long as I have only send a high signal turning the motor on it will continue running until I will send a low signal. What I'm looking for is a way to get the button to work basically like a joystick. So if I continue pressing the same button the motor will continue to run. But as soon as I release the button it should automatically send a low signal stopping the motor in running. So the button basically works like the potentiometer in a joystick.

How would you know that a button is still pressed? How would you know that a button is released? If you hold a button down does the character it sends get repeated? How often?

Could a '1' start a motor moving forward and another '1' stop it? Similarly for 2, 3, and 4 ?

You know if the button is pressed by looking at your finger or simply feeling it. It also depends om how the app is working. ArduinoBlueControl is one of those apps which has the ability to send data while pressing. How often I don't know as this is not specified anywhere.

I'm a bit surprised as I'm the only one giving answers however I'm the one asking if something is possible to code for a specific function. Until now I have only got questions but no answers Do you actually know if it is possible to make a code which can do as I request?

It will not be possible if the Arduino must see your finger or feel the button, and I do not know what ArduinoBlueControl does.

AdaFruit BlueFruit is much more documented... and more recent too. I just don't know if it is compatible with your hardware.

I don't usually do Bluetooth, so I cannot be of further help. Sorry.

So the Arduino Micro is maybe not fully compatible with c++ if code? Or do you mean that the standard libraries in Arduino IDE is not that good as those from AdaFruit or BlueFruit. Please explain what you mean?

Axident: What I'm looking for is a way to get the button to work basically like a joystick. So if I continue pressing the same button the motor will continue to run. But as soon as I release the button it should automatically send a low signal stopping the motor in running.

The functionality that you're describing needs to be in your phone app. Unless it communicates button pressed AND button released to the Arduino then there's no way the Arduino can do what you want.

So if you know what your app sends when button1 or button2 are RELEASED then it's easy. If the app doesn't send anything then it's impossible. The only way you can stop anything will be to use another different button to mean "Stop whatever you're doing now".

Steve

Hi Steve.

Thank you for your answer.

I did monitor the input received on serial1 port. But if I have done it the correct way I don't know. In the app I'm using I can define which character or number or longer words if I want to. In this case I use 1 for forward and 2 for reverse. However I don't know if this is just a part of a more complex string send from the app. Is there a specific way to monitor if anything else than a 1 or 2 has been sent, as 1 and two is the only input I have seen being sent to the Arduino.

I have been writing a mail to the developer of the app to have some more information about how the functions in the app works. I do also start testing other apps with more buttons than four only to get the fifth stop button.

Now this was my first project learning Arduino coding. I found this was the most logic way for me to start. But is there another way making a Bluetooth remote control for a RC car using other coding?

Kim