Hello,
I have done a project with a bluetooth module HC-05, 4 servos and an android mobile. The project should works like this:
First at all, you connect the HC-05 to your mobile and then you have to select a button in an app (Roboremo). If you select the first button, the servo1 moves, if you push the second button, the servo 2 moves, and so on. The servos should move one turn but it doesn't matter at all.
The problem is that I'm not sure wether the code is right or not, please help me.
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myServo1, myServo2, myServo3, myServo4;
int TxD = 10;
int RxD = 11;
int servoposition;
int servopos;
int new1;
SoftwareSerial bluetooth(TxD, RxD);
void setup()
{
int pos=0;
myServo1.attach(4);
myServo2.attach(5);
myServo3.attach(6);
myServo4.attach(7);
Serial.begin(9600);
bluetooth.begin(9600);
}
void loop()
{
if (bluetooth.available()){
String value = bluetooth.readString();
servoposition = value.toInt();
int servopos = bluetooth.read();
Serial.println(servopos);
myServo1.write(servopos);
myServo2.write(servopos);
myServo3.write(servopos);
myServo4.write(servopos);
if (value.toInt() == 1){
Serial.println(servoposition);
myServo1.write(360);
}
if (value.toInt() == 2){
Serial.println(servoposition);
myServo2.write(360);
}
if (value.toInt() == 3){
Serial.println(servoposition);
myServo3.write(360);
}
if (value.toInt() == 4){
Serial.println(servoposition);
myServo4.write(360);
}
}
servopos++;
delay(30);
Serial.println(servopos);
myServo1.write(servopos);
myServo2.write(servopos);
myServo3.write(servopos);
myServo4.write(servopos);
}
I don't got any erros while compiling the program but the project doesn't work.
Thank you for help me
Rail roder, what it happends is that the servos start to move and no more, it just doesn't work.
And the SerialprintIn() transform the analogic characters to our characters. However, I found it in a web so I'm not sure how it works exactly
RailRoader, the toInt() is to convert strings in normal numbers. I've had a mistake, printIn() it's used to print the characters on the screen.
In the code, value is equal to bluetooth.readString() (that it "reads" what the bluetooth send in strings (characters). Then, it converts the string in normal numbers with .toInt (in this case, it converts the value of 'value' variable in numbers with putting value.toInt())
if (bluetooth.available()){
String value = bluetooth.readString();
servoposition = value.toInt();
int servopos = bluetooth.read();
This is no way to read serial data.
I recommend that you go back to basics.
1)Initially, don't use bluetooth and Roboremo. Develop the code with Serial input from the monitor. Adding bluetooth to a working sketch with Serial monitor input is very easy.
Thanks a lot cattledog, but I want to use a bluetooth module to the project because I want to can take the project wherever and if I have to use the serial monitor I can't.
If you know how to fix my project without changing nothing but the code, please help me and replay.
I want to use a bluetooth module to the project because I want to can take the project wherever and if I have to use the serial monitor I can't.
I understand that. The path to bluetooth is through the serial monitor. Bluetooth is just serial without wires. It's trivial to convert a working serial sketch to bluetooth.
Your code is a mess, and you need to start over. I'm not even certain you are able to move the servos as you want without the added complication of input from Serial or bluetooth.