Bluetooth with 2 servo

Hello,

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);
      }
    }
}

What is the meaning of 'doesn't work'? Don't you get any data? wrong data? anything else?

You can print to the serial monitor what you received from the HC05 to check this.

If you receive "OFF" the sketch will stuck forever. Is that really what you want?

What I mean is that my servos don't receive the signal of the function ON and OFF.

I have a communication by AT commands from PC to HC-05, what I need to know to check? I have already checked the NAME, PSWD and UART.

Yes, the OFF function is that the servos stuck. But if I press the function ON, the servos work.

?? Do they work or not? It cannot be both.

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?

Thanks a lot btw!!!

is an endless loop that will never be left. So the Arduino cannot do anything further ( besides executing some interrupts ... :wink: )

But what do you really want the OFF function to do? If you receive "ON" the servos do their moving once and then stop.

The next code will fit?

if (String(s_ordre).equals(String("OFF"))) {
  			while(!((String(s_ordre).equals(String("ON")))));```

No, that's in fact the same.
Please tell what you really want your sketch to do. Not in terms of programming - explain in simple words :wink:.

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?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.