I have the next code but the bluetooth doesn't work. The movement of the code is OK because I have checked but the bluetooth code maybe ther is a misstake and I can't find it anywhere. I have already programmed the HC-05 slave function, 38400.
There are two functions: ON and OFF.
Can anyone help me with this?
#include <MobaTools.h>
#include <SoftwareSerial.h>
MoToServo servo1;
MoToServo servo2;
String s_ordre;
SoftwareSerial bt_serial(3,11);
int Pos1 = 0;
int Pos2 = 0;
void setup()
{
servo1.attach(9);
servo2.attach(10);
servo1.write(0);
servo2.write(85);
servo1.setSpeedTime(10000);
servo2.setSpeedTime(10000);
bt_serial.begin(38400);
s_ordre = String("");
}
void loop()
{
if ((bt_serial.available()>0)) {
s_ordre = bt_serial.readString();
if (String(s_ordre).equals(String("ON"))) {
delay(2000);
servo1.write(30);
servo2.write(65);
while (servo1.moving() || servo2.moving() );
delay(10);
servo2.write(75);
while (servo2.moving());
delay(10);
servo1.write(0);
servo2.write(85);
while (servo1.moving() || servo2.moving() );
delay(2000);
}
if (String(s_ordre).equals(String("OFF"))) {
while(true);
}
}
}
Your sketch will stuck the whole Arduino forever when receiving "OFF". Not only the servos. You have to press reset to restart. I don't think that is what you want.
Really? I don't want this...
Maybe the problem comes from here. I may check another code for the OFF funtion because it doesn't fit. What I should to write?
What I want is send the message ON to do the movements of the servos. When I send the message OFF, turn off the movements. But when I send another time the message ON, do the movements of the servos.
In your sketch, the movement is only executed once, then the servos stop by themselves. While they move the sketch does't check if it received another command. As it is now, "OFF" is completely useless.
Do you want to interrupt the movement by sending "OFF"? Then you must get rid of all the blocking code in your sketch.
Or do you want the servo movement looping until you send OFF?