Go Down

Topic: Vex motors via Bluetooth using Android (Read 795 times) previous topic - next topic

Robotrats

Mar 15, 2016, 04:58 am Last Edit: Mar 15, 2016, 05:04 am by Robotrats Reason: forgot code
Hi, i'm quite new to the world of arduino, and honestly a bit of a noob, i know a lot about java programming so i should understand if ever you explain me my mistakes in programming terms. So here is the problem, I've been able to use the servo library in order to have the motor controllers work adequately. The only thing i have to change in order to vary their speed is one variable (maxSpeed). The idea behind that is that i would try and communicate ONE number between 0 and 180 via bluetooth with a dedicated app on android and by using the JY-MCU HC-06 bluetooth to serial module. Only now that i have included a software serial for my bluetooth module, the program never seems to enter the "if available" condition since the motors' speeds are never set to the default maxSpeed or even to the one i am transmitting while pressing on my app's buttons. App is made using MIT App Inventor as i am still learning how to use Android Studio. (I am 100% sure the bluetooth module and my phone communicate as it writes within my app if both are paired and connected. Also the led light stopped blinking on the bluetooth module)
Code: [Select]
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myservo1; // creating both servo's for both motors
Servo myservo2;
int bluetoothTx=10; //pins for the bluetooth module's connections
int bluetoothRx=11;
SoftwareSerial bluetooth(bluetoothTx,bluetoothRx);
int maxSpeed=90; //Speed the motor will achieve
void setup()
{
  bluetooth.begin(9600);
  myservo1.attach(5);
  myservo2.attach(6);  // attaches the servos to the apropriate pins
}


void loop() {
  if(bluetooth.available()>0){  //this seem's to be the problem since below
                                //code never seems to take effect
    maxSpeed=bluetooth.read();  //no idea if this is the way to read a 1byte number
                                //sent via bluetooth from app or if it is an int
    //if(maxSpeed<=90){                   //acceleration algorithm
    //  for(pos; pos>=maxSpeed; pos-=1){
    //    myservo1.write(pos);
    //    myservo2.write(pos);
    //    delay(300);
    //  }
    //}
    //if(maxSpeed>90){                    //acceleration algorithm
    //  for(pos; pos<=maxSpeed; pos+=1){
    //    myservo1.write(pos);
    //    myservo2.write(pos);
    //    delay(300);
    //  }
    //}
    myservo1.write(maxSpeed); //maxSpeed is aplied to each motor's servo
    myservo2.write(maxSpeed);               
  }
}

PaulS

Some information about the bluetooth module would be interesting.
Some information about how the Bluetooth module is connected to the Arduino would be interesting.
The art of getting good answers lies in asking good questions.

Robotrats

Some information about the bluetooth module would be interesting.
Some information about how the Bluetooth module is connected to the Arduino would be interesting.
I linked pictures of my setup as well as schematics and the code of my app. All i wish for is the arduino to receive information and be able to treat it as a number (is it receiving an int? string? double?) and then send it to the servo as a speed.

PaulS

Since you are not using the hardware serial port for the bluetooth device, it is available for debugging. Why not use it for that?
The art of getting good answers lies in asking good questions.

Robotrats

Thanks Paul! turns out i had the bluetooth module plugged in the wrong way so no information was making its way to the board :P (TX and RX were inverted)

Go Up