Controlling DC motor over bluetooth joystick.

Hello. Im new in this forum, but really intrested coding arduino. (sorry for my bad english,but i hope i can get help from here).
The hardware i have.
Arduino mega
Adafruit motor driver shield (4dc motors,2servos)
HC 05 bluetooth module (connected trough softwareserial( 50,51)
Im building a bluetooth based robot, i was able to control my robot with ,, keyboard,,.
If i pressed A then robot went forward, B backward etc.
But now im planning to run it with bluetooth joystick, i struggeled like ten hours,but i wastn able to get it work.
Can smarters give me a hint or something to start with.I was able to get x y cordinates to arduino serial monitor, but how to calculate these values to dc motor speeds ?

Please post the code that you have written so far.

//Mootori parameetrid.
//Skript mootori tootamiseks
#include <AFMotor.h>
#include <Servo.h>
AF_DCMotor motor(3,MOTOR12_64KHZ);
AF_DCMotor motor1(4,MOTOR12_64KHZ);
//led
int ledpin=53;
int LED = 53;
int incomingByte = 0;

//////////////////////////////
//bluetooth yhenduse kood, kasutab softwareserialit, sest moto shield on juba pin 0 ja 1
#include <SoftwareSerial.h>// impordin softwareseriali
SoftwareSerial Genotronex(50, 51); // RX, TX
int BluetoothData;

void setup(){

//(deffineerib moto kiirused
motor.setSpeed(255);
motor1.setSpeed(255);
//yhenduse loomine
Genotronex.begin(9600);
Genotronex.println(“Bluetooth olemas, v]id kirjutada sisendeid”);
delay(500);

}

////////////////////////////////////

void loop()
//kood hakkab jooksma, kui bluetoothilt tuleb signaal siis
{
if (Genotronex.available()){
BluetoothData=Genotronex.read();
if(BluetoothData==‘1’){ // if number 1 pressed …
digitalWrite(ledpin,1);
Genotronex.println("LED On D13 ON ! ");
}
if (BluetoothData==‘0’){// if number 0 pressed …
digitalWrite(ledpin,0);
Genotronex.println("LED On D13 Off ! ");
}
}

if (Genotronex.available() > 0) {
incomingByte = Genotronex.read();
digitalWrite(LED, HIGH);
Genotronex.print("I received: ");
Genotronex.println(incomingByte);
delay(10);}
// if byte is equal to “105” or “i”, go forward
else if (incomingByte == 105){
motor.run(FORWARD);
motor1.run(FORWARD);
delay(25);
}
// if byte is equal to “106” or “j”, go left
else if (incomingByte == 106){
motor.run(RELEASE);
motor1.run(BACKWARD);
delay(25);
}
// if byte is equal to “108” or “l”, go right
else if (incomingByte == 108){
motor.run(FORWARD);
motor1.run(RELEASE);
delay(25);
}
// if byte is equal to “107” or “k”, go reverse
else if (incomingByte == 65){
motor.run(BACKWARD);
motor1.run(BACKWARD);
delay(25);
}

// otherwise, stop both motors
else {

motor.run(BRAKE);
motor1.run(BRAKE);
delay(25);

}

}

ATM this code is working as a “keyboard”.
I think i have to read the cordinates from bluetooth joystick. Get them to the arduino and then set the motor speeds according to the cordinates ? Am I right ?
Questions:
How to get the bluetooth sent cordinates to arduino scketch ?
To set motor speeds the code is motor.setspeed (thespeedvalue, must depent on x and y)?

#include <SoftwareSerial.h>// impordin softwareseriali
SoftwareSerial Genotronex(50, 51); // RX, TX

Why? The Mega has 4 hardware serial ports. It does not make sense to not use them.

But now im planning to run it with bluetooth joystick

A link to the hardware would be useful.

I think i have to read the cordinates from bluetooth joystick. Get them to the arduino and then set the motor speeds according to the cordinates ? Am I right ?

Probably. Whether you can actually do any of that depends on what hardware you actually have.

How to get the bluetooth sent cordinates to arduino scketch ?

Use Genotronex.read(). Exactly how depends on what is being sent.

To set motor speeds the code is motor.setspeed (thespeedvalue, must depent on x and y)?

Or some random number, but the joystick position(s) seem more useful.

What you need to define is the relationship between the stick position (in X and Y) and the speed of the left and right motors.

PaulS:

#include <SoftwareSerial.h>// impordin softwareseriali

SoftwareSerial Genotronex(50, 51); // RX, TX



Why? The Mega has 4 hardware serial ports. It does not make sense to not use them.

A link to the hardware would be useful.
Probably. Whether you can actually do any of that depends on what hardware you actually have.
Use Genotronex.read(). Exactly how depends on what is being sent.
Or some random number, but the joystick position(s) seem more useful.

What you need to define is the relationship between the stick position (in X and Y) and the speed of the left and right motors.

My plan is use this hardware.
Hmm where is should connect then RX/TX? I cant connect them to RX0,TX0 because the motor driver shield takes these ports.

Can you write me a code that reads x and y from bluetooth?

Just started from beginning and writed this code with joystick and motor.#include <AFMotor.h>

AF_DCMotor motor(3,MOTOR12_64KHZ);
const int joyH= A8;
const int joyV=A9;
int motorSpeed;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {

motorSpeed = analogRead(joyV);
motorSpeed = map(motorSpeed, 513,0,0,255);
motor.setSpeed(motorSpeed);
motor.run(FORWARD);
}

With this i was able to control motor speed, but only forward :frowning:

Hmm where is should connect then RX/TX? I cant connect them to RX0,TX0 because the motor driver shield takes these ports.

Why does the motor driver shield use the main hardware serial port? Post a link to the motor driver shield.

Can you write me a code that reads x and y from bluetooth?

Not until we know what the data looks like. You may need to dump the motor driver shield for the time being, so you can use the hardware serial port for debugging.

With this i was able to control motor speed, but only forward

Since that code won't even compile, that seems unlikely.

You need to use an if statement to compare the value read from the joystick to the middle of the range. If the value is above the midpoint, you use one map() call to get a speed. If the value is below the midpoint, you use a different map() call to get a speed AND you enable the pin(s) needed to make the motor go the other way.

Thanks im starting to undestand how it might be work.
Now i can turn it with knob forward and backward,and also the knob position sets speed of motor.
Little bit thinking and i think i can turn it and so on :wink:
code for backward and forward :wink:
void loop() {
center = analogRead(joyV);
if (center<513){
motorSpeed = analogRead(joyV);
motorSpeed = map(motorSpeed, 513,0,0,255);
motor.setSpeed(motorSpeed);
motor.run(FORWARD);
}
else if (center>513){
motorSpeed= analogRead(joyV);
motorSpeed=map(motorSpeed,513,1023,0,255);
motor.setSpeed(motorSpeed);
motor.run(BACKWARD);
}
}

Data coming from android.
Android knob all left:
Joystick:Left:100:90
Knob all right;
Joystick:Left:100:270
Knob all up;
Joystick:Left:100:000
All down:
Joystick:Left:100:180
How i must code the Genotronex.read ?
Like this ?
Genotronex.read(Joystick:Left) `?

center = analogRead(joyV);
if (center<513){
motorSpeed = analogRead(joyV);

It’s not necessary to read the joystick position again. It is unlikely to have moved much in the few nanoseconds it took to evaluate the if statement.

motor.setSpeed(motorSpeed);
motor.run(FORWARD);

Stomp the gas. Then put it in gear. Hope you don’t drive like that.

Found this in app.
Device Connect: “DEVICE:XT1060:CONNECTED\n”

Joystick Data (angle/power): “Joystick:Left:095:277\n”

SeekBar Data (power): “SeekBar:3:034\n”

Button Press: “Button:C:pressed\n”

Toggle Changed: “Toggle:2:true\n”

Wich line i should use ?
Genotronex.read (angle);
and
Genotronex.read(power);
Or something else?

Wich line i should use ?
Genotronex.read (angle);
and
Genotronex.read(power);
Or something else?

Something else, obviously, since read() is a method of the SoftwareSerial class, and, as such, does not take any arguments.

What the app (claims to) send(s) and what the Arduino receives have not proven to be the same. Until you have proven that, I’m out of here.