Author Topic: DIY bluetooth controlled car  (Read 1516 times)

0 Members and 1 Guest are viewing this topic.

Offline AdhithTopic starter

  • Frequent Contributor
  • **
  • Posts: 323
  • Country: in
DIY bluetooth controlled car
« on: August 04, 2019, 07:32:35 pm »
Hello there,
I'm a newbie in Arduino and coding sort of things. I was doing an automobile related hobby project and building a Bluetooth controlled car was part of the project. I found some resources online and I'm sharing the schemamtic and the arduino codes of it. The movements are controlled through a mobile app.
However this was only a part of my project and the real intention is to turn on and off a separate motor(apart from the 4 geared motors used in the project) through the same mobile app. So my question is, could I change the code to form a new ON and OFF signal from one of the outputs of the motor driver to run the seperate motor ??  Also the controls from the app should be any combination keys or something. So is it possible?
Any help would be highly appreciated. Thank you in advance.

code

char t;
 
void setup() {
pinMode(13,OUTPUT);   //left motors forward
pinMode(12,OUTPUT);   //left motors reverse
pinMode(11,OUTPUT);   //right motors forward
pinMode(10,OUTPUT);   //right motors reverse
pinMode(9,OUTPUT);   //Led
Serial.begin(9600);
 
}
 
void loop() {
if(Serial.available()){
  t = Serial.read();
  Serial.println(t);
}
 
if(t == 'F'){            //move forward(all motors rotate in forward direction)
  digitalWrite(13,HIGH);
  digitalWrite(11,HIGH);
}
 
else if(t == 'B'){      //move reverse (all motors rotate in reverse direction)
  digitalWrite(12,HIGH);
  digitalWrite(10,HIGH);
}
 
else if(t == 'L'){      //turn right (left side motors rotate in forward direction, right side motors doesn't rotate)
  digitalWrite(11,HIGH);
}
 
else if(t == 'R'){      //turn left (right side motors rotate in forward direction, left side motors doesn't rotate)
  digitalWrite(13,HIGH);
}

else if(t == 'W'){    //turn led on or off)
  digitalWrite(9,HIGH);
}
else if(t == 'w'){
  digitalWrite(9,LOW);
}
 
else if(t == 'S'){      //STOP (all motors stop)
  digitalWrite(13,LOW);
  digitalWrite(12,LOW);
  digitalWrite(11,LOW);
  digitalWrite(10,LOW);
}
delay(100);
}

« Last Edit: August 04, 2019, 07:36:37 pm by Adhith »
 

Offline admiralk

  • Regular Contributor
  • *
  • Posts: 178
  • Country: us
Re: DIY bluetooth controlled car
« Reply #1 on: August 05, 2019, 01:36:04 am »
I am not sure I understand what you are asking or how your interface works, but I can make a couple suggestions on your code.

First, using a switch statement makes the code easier to read instead of all the if elses, imo.
Code: [Select]
switch(Serial.read())
{
case 'F':
    digitalWrite(13, HIGH);
    digitalWrite(11, HIGH);
    break;
//...
   

As you have it, you would need to hit stop between every action. If you are moving forward and then try to turn right or left, without stopping first, what do you think will happen? I am not sure what would happen if you tried reversing, but it probably would not be good.

Adding more motors, or anything else, should not be a problem as long as you have free pins to connect to. Key combinations would be a little trickier as they could not be reprecented by a single char. Arduino does have readString() and readByteArray() functions, they might be called something a little different(?), but how well they work depends on what the app sends. I ran into this problem were the micro was expecting a '/n' that was never sent. It was easy enough to fix by changing the delimiter, but something to be aware of especially if you are not writing the code for the app also.
 
The following users thanked this post: Adhith

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3032
  • Country: us
Re: DIY bluetooth controlled car
« Reply #2 on: August 05, 2019, 01:40:02 am »
To independently control a motor you need an H-bridge dedicated to that motor.

That board you are using has two H-bridges. This video should help explain how it works:



Whether it is a good idea to place motors in parallel like they are doing is another question.

So if you want to add another motor and be able to control it independently of the others you'll need another L289N board and allocate another two or three digital IO pins to control it.

Quote
  Also the controls from the app should be any combination keys or something.

I have no idea how to interpret this. How about explaining it in terms of a usage scenario: "if the user does X, then one thing happens; if the user does Y then Z happens; if user does W then ..."

This video which came out last week might be of interest:



« Last Edit: August 05, 2019, 01:42:10 am by ledtester »
 
The following users thanked this post: Adhith

Offline AdhithTopic starter

  • Frequent Contributor
  • **
  • Posts: 323
  • Country: in
Re: DIY bluetooth controlled car
« Reply #3 on: August 05, 2019, 07:57:27 pm »
I am not sure I understand what you are asking or how your interface works, but I can make a couple suggestions on your code.

First, using a switch statement makes the code easier to read instead of all the if elses, imo.
Code: [Select]
switch(Serial.read())
{
case 'F':
    digitalWrite(13, HIGH);
    digitalWrite(11, HIGH);
    break;
//...
   

As you have it, you would need to hit stop between every action. If you are moving forward and then try to turn right or left, without stopping first, what do you think will happen? I am not sure what would happen if you tried reversing, but it probably would not be good.

Adding more motors, or anything else, should not be a problem as long as you have free pins to connect to. Key combinations would be a little trickier as they could not be reprecented by a single char. Arduino does have readString() and readByteArray() functions, they might be called something a little different(?), but how well they work depends on what the app sends. I ran into this problem were the micro was expecting a '/n' that was never sent. It was easy enough to fix by changing the delimiter, but something to be aware of especially if you are not writing the code for the app also.
Thank you very much for your detailed answer. I think I was not that good in explaining my need through my previous question. Also I have made some alteration to my project. So I'll try to explain things in a better way.
I'm driving 4 geared motor through the motor driver for my project. The movement is controlled through an mobile app connected to the Bluetooth module on the car. Apart from this, I also need a relay switch from the Arduino via the same app. I'm planing to use a transistor or mechanical relay for this.
This relay is used to drive a separate geared motor which serves a different function. I'm directly driving this motor with the relay and without any driver.
It should be capable of rotating the motor in clockwise and anti clockwise directions.  Since All the keys of the app are already coded for the movement of the car, I was wondering If by the using a pair of combination keys, could I drive the separate motor in clockwise and anticlockwise directions.
 

Offline AdhithTopic starter

  • Frequent Contributor
  • **
  • Posts: 323
  • Country: in
Re: DIY bluetooth controlled car
« Reply #4 on: August 05, 2019, 08:00:34 pm »
To independently control a motor you need an H-bridge dedicated to that motor.

That board you are using has two H-bridges. This video should help explain how it works:



Whether it is a good idea to place motors in parallel like they are doing is another question.

So if you want to add another motor and be able to control it independently of the others you'll need another L289N board and allocate another two or three digital IO pins to control it.

Quote
  Also the controls from the app should be any combination keys or something.

I have no idea how to interpret this. How about explaining it in terms of a usage scenario: "if the user does X, then one thing happens; if the user does Y then Z happens; if user does W then ..."

This video which came out last week might be of interest:


Thank you very much for you help and link to the video. I had a quick look and the video and got some basic idea. I'll fully watch it within a day. I have updated my question in the quote above. Could you please have a look.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14301
  • Country: fr
Re: DIY bluetooth controlled car
« Reply #5 on: August 05, 2019, 08:26:33 pm »
Just to be reassured: you're talking about a model car here, not a real car? ;D
 

Offline admiralk

  • Regular Contributor
  • *
  • Posts: 178
  • Country: us
Re: DIY bluetooth controlled car
« Reply #6 on: August 05, 2019, 11:54:56 pm »
That second video should give you just about all you need to get a good jump on things.
 
The following users thanked this post: Adhith

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: nz
Re: DIY bluetooth controlled car
« Reply #7 on: August 06, 2019, 12:04:19 am »
Just to be reassured: you're talking about a model car here, not a real car? ;D

Maybe he's been hired to work on Tesla summon mode.  :-DD
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3032
  • Country: us
Re: DIY bluetooth controlled car
« Reply #8 on: August 06, 2019, 12:43:44 am »
Quote
It should be capable of rotating the motor in clockwise and anti clockwise directions.

You need an H-bridge to do that.

Quote
Since All the keys of the app are already coded for the movement of the car, I was wondering If by the using a pair of combination keys, could I drive the separate motor in clockwise and anticlockwise directions.

From your code it appears that the phone app sends signals which are transmitted to the Arduino via the serial interface. If the code in your original post works, then this is what I expect happends:

If you press the forward button, then the character 'F' is transmitted to the Arduino.
If you press the turn right button then the character 'R' is transmitted to the Arduino.

If you press the forward and turn right buttons "at the same time" the Arduino will either see the character 'F' followed by the character 'R' or it might see the character 'R' followed by the character 'F' depending on how the phone app works. In any case, you'd get the same result if the user hit the forward button and then hit the turn right button a millisecond later. In other words, I doubt the Arduino can tell if a bunch of keys were pressed at the same time versus pressed within a very small timespan of each other.

You can record the time between key presses, so you could do something special if, say, the user hit the forward button three times real fast. If you receive three 'F' characters within, say, a 10th of a second you could interpret that as some other command.

But I would just use the RC Car control app for Android phones that I linked to -- it has extra buttons you can bind your own custom commands to.
 
The following users thanked this post: Adhith

Offline AdhithTopic starter

  • Frequent Contributor
  • **
  • Posts: 323
  • Country: in
Re: DIY bluetooth controlled car
« Reply #9 on: August 08, 2019, 06:50:56 pm »
Just to be reassured: you're talking about a model car here, not a real car? ;D
Haa :) yeah a model car. but it is capable of re configuring the shape of the wheels. i.e from normal tire to a caterpillar track.
 

Offline AdhithTopic starter

  • Frequent Contributor
  • **
  • Posts: 323
  • Country: in
Re: DIY bluetooth controlled car
« Reply #10 on: August 08, 2019, 06:52:18 pm »
That second video should give you just about all you need to get a good jump on things.
Yeah, I saw it. I got a lot idea about. I think I could use the additional buttons on it to individually allocate a different function for the project.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14301
  • Country: fr
Re: DIY bluetooth controlled car
« Reply #11 on: August 08, 2019, 06:58:54 pm »
Just to be reassured: you're talking about a model car here, not a real car? ;D
Haa :) yeah a model car. but it is capable of re configuring the shape of the wheels. i.e from normal tire to a caterpillar track.

It's a transformer?
 

Offline AdhithTopic starter

  • Frequent Contributor
  • **
  • Posts: 323
  • Country: in
Re: DIY bluetooth controlled car
« Reply #12 on: August 08, 2019, 07:01:50 pm »
Quote
It should be capable of rotating the motor in clockwise and anti clockwise directions.

You need an H-bridge to do that.

Quote
Since All the keys of the app are already coded for the movement of the car, I was wondering If by the using a pair of combination keys, could I drive the separate motor in clockwise and anticlockwise directions.

From your code it appears that the phone app sends signals which are transmitted to the Arduino via the serial interface. If the code in your original post works, then this is what I expect happends:

If you press the forward button, then the character 'F' is transmitted to the Arduino.
If you press the turn right button then the character 'R' is transmitted to the Arduino.

If you press the forward and turn right buttons "at the same time" the Arduino will either see the character 'F' followed by the character 'R' or it might see the character 'R' followed by the character 'F' depending on how the phone app works. In any case, you'd get the same result if the user hit the forward button and then hit the turn right button a millisecond later. In other words, I doubt the Arduino can tell if a bunch of keys were pressed at the same time versus pressed within a very small timespan of each other.

You can record the time between key presses, so you could do something special if, say, the user hit the forward button three times real fast. If you receive three 'F' characters within, say, a 10th of a second you could interpret that as some other command.

But I would just use the RC Car control app for Android phones that I linked to -- it has extra buttons you can bind your own custom commands to.
Yeah. by reading your reply I got a better understanding about it. Thank you for that. So for a clockwise and anticlockwise rotation I definitely need another H bridge module right ? could I create a sort of H bridge with one or two mechanical relay ? The reason is, I have a lot of relays with me so that I could cut down the cost of project without buying an additional driver.
Since the app contain two individual switch for turning the front and back light of the car, I was hoping that I could code it for clockwise and anti clock wise rotation by two seperated relay connected to the Arduino. could I do that?
« Last Edit: August 08, 2019, 07:05:09 pm by Adhith »
 

Offline AdhithTopic starter

  • Frequent Contributor
  • **
  • Posts: 323
  • Country: in
Re: DIY bluetooth controlled car
« Reply #13 on: August 08, 2019, 07:03:56 pm »
Just to be reassured: you're talking about a model car here, not a real car? ;D
Haa :) yeah a model car. but it is capable of re configuring the shape of the wheels. i.e from normal tire to a caterpillar track.

It's a transformer?
No, just though to do a hobby project around this concept of reconfigurable wheel.
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3032
  • Country: us
Re: DIY bluetooth controlled car
« Reply #14 on: August 08, 2019, 08:19:47 pm »
Quote
So for a clockwise and anticlockwise rotation I definitely need another H bridge module right ? could I create a sort of H bridge with one or two mechanical relay ?

Have a look at this:

https://www.instructables.com/id/Two-Relay-DC-Motor-Control-Simple-H-bridge/
 
The following users thanked this post: Adhith

Offline AdhithTopic starter

  • Frequent Contributor
  • **
  • Posts: 323
  • Country: in
Re: DIY bluetooth controlled car
« Reply #15 on: August 09, 2019, 07:39:34 pm »
Quote
So for a clockwise and anticlockwise rotation I definitely need another H bridge module right ? could I create a sort of H bridge with one or two mechanical relay ?

Have a look at this:

https://www.instructables.com/id/Two-Relay-DC-Motor-Control-Simple-H-bridge/
Thank you. I got a clear picture now. I'll work on it and let you guys know about the progress.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf