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)
#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);
}
}
sketch_mar06a.ino (1.28 KB)