Arduino bluetooth control only makes the servo jitter.

I’m going through a tutorial from this website,

and it doesn’t appear to work, all it does is make the servos jitter. I’ve tried eliminating everything down to just the arduino, a servo and the HC-05 bluetooth module connected to a 5v 2a power adapter. Nothing helps including switching between a genuine and clone arduino and the mega 2560.

I’ve pretty much given up on that, however. I’m using the code from this website,

So far, it worked, then I tried for 6 servos, the first two worked, the other 4 didn’t. Then I somehow got the third one to work (probably on a fluke), but that’s it. Same setup, nothing but an Arduino, SG90 servo and a bluetooth module connected to a 5v 2a power adapter. Only three servos work, the other three just jitter.

the code I’m using is

#include <SoftwareSerial.h> 
#include <Servo.h> 

Servo servo1; 
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
int TX = 3; 
int RX = 4; 

SoftwareSerial HC_05(TX, RX);  //Bluetooth TX to 3 and Bluetooth RX to 4.

void setup()
{
 servo1.attach(5); 
 servo2.attach(6);
 servo3.attach(7);
 servo4.attach( 8 );
 servo5.attach(9);
 servo6.attach(10);
 Serial.begin(9600);
 HC_05.begin(9600);
}

void loop()
{
 if(HC_05.available()> 0 ) 
 {
   int value = HC_05.read(); 
   if(value>0 && value<90)
   {
     Serial.println(value); 
     int servo1_position = map(value, 0, 90, 0, 180);
     servo1.write(servo1_position);
   }
  if(value>90 && value<180)
  {
     Serial.println(value); 
     int servo2_position = map(value, 91, 180, 0, 180);
     servo2.write(servo2_position);
   }
   if(value>180 && value<270)
   {
     Serial.println(value); 
     int servo3_position = map(value, 181, 270, 0, 180);
     servo3.write(servo3_position);
   }
   if(value>270 && value<360)
   {
     Serial.println(value); 
     int servo4_position = map(value, 271, 360, 0, 180);
     servo4.write(servo4_position);
   }
   if(value>360 && value<450)
   {
     Serial.println(value); 
     int servo5_position = map(value, 361, 450, 0, 180);
     servo5.write(servo5_position);
   }
   if(value>450 && value<540)
   {
     Serial.println(value); 
     int servo6_position = map(value, 451, 540, 0, 180);
     servo6.write(servo6_position);
   }
 }
}

Only the first three servos work, changing pins and isolating so it’s only looking for values above 270 makes the servos jitter.

Thank you for any help I can get.

I don't really know what "changing pins and isolating" means. When you say the first 3 work do you mean servo1, servo2 and servo3 work whatever pins you associate them with and whatever physical servos are connected to them? Or what exactly do you mean?

Does Serial monitor show that values are coming cleanly through as expected? I.e. are you sure it's not the values that are jittering?

Of course most servo problems end up being power or wiring related so how exactly is everything connected. A wiring diagram or a clear picture showing all the wiring would be useful.

Steve

Comment out the entire if(value … ) stuff and leave only one, using /* and */. Change the if statment to the following. You do not need the 1,2,3, the variable is only valid inside the {}

if(value>0 && value<90)
{
  Serial.println(value);
  int servo_position = map(value, 0, 90, 0, 180);
  servo1.write(servo_position);
}

Now compile and download the code and test it. Then change the number (servo1 to servo2) and repeat. This way you will know whether each individual servo works fine.

Next move the Serial.println(value); directly after you receive it. The Arduino reference says it returns a byte or -1. So the value will never be larger than 255. See if it is true.

https://www.arduino.cc/reference/en/language/functions/communication/serial/read/

Note: Please modify your post and add code tags, so your code appears in a box. Just mark the code and click </>

If you use a Mega, then you shouldn't use SoftwareSerial. A Mega has three spare hardware serial ports. Leo..

slipstick: I don't really know what "changing pins and isolating" means. When you say the first 3 work do you mean servo1, servo2 and servo3 work whatever pins you associate them with and whatever physical servos are connected to them? Or what exactly do you mean?

Does Serial monitor show that values are coming cleanly through as expected? I.e. are you sure it's not the values that are jittering?

Of course most servo problems end up being power or wiring related so how exactly is everything connected. A wiring diagram or a clear picture showing all the wiring would be useful.

Steve

i mean that I'm physically removing the jumper cable and moving it to pin 5,6,7,etc on the arduino board.

Klaus sorta showed me how to use the serial monitor, the values are coming in but never above 255, i believe that is where i'm having a problem, so now i need to get the board to read higher than 255....i do not know how to accomplish this

Klaus_K:
Comment out the entire if(value … ) stuff and leave only one, using /* and */. Change the if statment to the following. You do not need the 1,2,3, the variable is only valid inside the {}

if(value>0 && value<90)

{
 Serial.println(value);
 int servo_position = map(value, 0, 90, 0, 180);
 servo1.write(servo_position);
}




Now compile and download the code and test it. Then change the number (servo1 to servo2) and repeat. This way you will know whether each individual servo works fine.


Next move the Serial.println(value); directly after you receive it. The Arduino reference says it returns a byte or -1. So the value will never be larger than 255. See if it is true.

https://www.arduino.cc/reference/en/language/functions/communication/serial/read/

Note: Please modify your post and add code tags, so your code appears in a box. Just mark the code and click </>

Thanks for your help, i’ve narrowed down the problem to the board not reading values above 255 and this is why the other servos are not working. I’ve figured out a way to fix it but it’s essentially similar to removing frames from a video and making the animation look choppy.

Can you help me fix this original problem? The values have to go up to 540 to follow along with the app i have already created.

thank you so much for helping me figure out the core problem i was having

so now i need to get the board to read higher than 255....i do not know how to accomplish this

Bluetooth is just Serial without wires, so a review of Robin2's tutorial on Serial Input Basics will be helpful.

You will need to understand how the phone app is sending the numerical data. How is a number larger than 255 being sent? It's quite possible given what you have presented so far that it is sending the two bytes of numerical integer.

The serial port transmits and receives bytes. You will need to collect a few bytes and combine them into a number. Check out this tutorial for inspiration.

https://forum.arduino.cc/index.php?topic=396450.0

Edit: Looks like cattledog beat me to it while I was looking for the link and typing. :)

Thanks, you guys are the best!!