Go Down

Topic: Servo testing using bluetooth. Servo won't turn all the way!!! Please help! (Read 560 times) previous topic - next topic

Daniel5815

So I'm using bluetooth on serialport 9600 and sending signals to alter the position of the servo. The code seems to be running well and the bluetooth connection is strong. Every time I send a signal trying to move the servo, it seems like the servo is literally trying to go the that directed position and immediately turn back to it's original spot. Like it seems like it needs like half a second to fully go to 180 degree from 90, but I feel like there is something in my code that makes the servo go straight back to 90 after one iteration behind the signal received iteration. Please read my code and let me know!

Delta_G

Code from the OP where everyone can see it. 

Code: [Select]
#include <SoftwareSerial.h>
#include <Servo.h>

// Define the data transmit/receive pins in Arduino

#define TxD 2

#define RxD 3

SoftwareSerial mySerial(RxD, TxD); // RX, TX for Bluetooth

Servo myServo;
Servo myServo1;

void setup() {

  mySerial.begin(9600); // For Bluetooth

  Serial.begin(9600); // For the IDE monitor Tools -> Serial Monitor

  // Any code that you want to run once....
  myServo.attach(8);
  myServo1.attach(9);
  myServo.write(0);
  myServo1.write(0);
}

void operateServo(boolean state) {
  Serial.println(state);
  if (state == 'a') {
    myServo.write(180);
    myServo1.write(180);
    delay(1000);
    mySerial.println("Servo is at 180");
  }
  else if (state == 'b'){
    myServo.write(0);
    myServo1.write(0);
    delay(1000);
    mySerial.println("Servo is at 0");
  }
  /*else{
    myServo.write(90);
    myServo1.write(90); 
    mySerial.println("Set to mid point");
  }*/
}

void loop() {

  // put your main code here, to run repeatedly:

  boolean isValidInput; do {
    byte c; // get the next character from the bluetooth serial port

    while ( !mySerial.available() ) ; // LOOP...

    c = mySerial.read(); // Execute the option based on the character recieved

    Serial.print(c); // Print the character received to the IDE serial monitor

    switch ( c ) {

      case 'a': // You've entered a

        // Do the code you need when 'a' is received.....

        mySerial.println( "You've entered an 'a'" );

        isValidInput = true;

        operateServo(c);
        delay(1000);

        break;

      case 'b': // You've entered b

        // Do the code you need when 'a' is received.....

        mySerial.println( "You've entered an 'b'" );

        isValidInput = true;

        operateServo(c);
        delay(1000);

        break;

      default:

        // Do the code you need when any other character is received.....

        mySerial.println( "Please enter 'a' or 'b'" );

        isValidInput = false;

        break;

    }

  } while ( isValidInput == true ); // Repeat the loop

}
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Delta_G

Code: [Select]
void operateServo(boolean state) {
  Serial.println(state);
  if (state == 'a') {


boolean is true/false.  boolean is NOT 'a'.  'a' is a char. 


Code: [Select]
boolean isValidInput; do {

Of all the messed up confusing formatting screw ups you could possibly imagine, I think starting a do while on a line with other code takes the cake. 

Please use Control-T to format code before you post it.  We don't format code the way we do just to make it pretty.  There's a real good purpose for doing it the way we do. 

And please clean up the comments while you are editing code.  The only thing worse than no comments is wrong comments. 

Code: [Select]
byte c; // get the next character from the bluetooth serial port
No it doesn't.

Code: [Select]
c = mySerial.read(); // Execute the option based on the character recieved
I think this comment was supposed to go with that last one instead.

IF you're not going to have the comment where they make any sense, then just delete them.  Don't add to your own confusion by getting them all messed up.  Remember, you're asking someone else to kindly read your code and check it for you, try not to make that unnecessarily hard on them. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

PaulS

Code: [Select]
SoftwareSerial mySerial(RxD, TxD); // RX, TX for Bluetooth
That's like naming your dog myDog. Stupid, in other words.

Name the instance based on what it is reading data from.

Code: [Select]
Servo myServo;
Servo myServo1;

More stupid names. Name the instances based on what the servo does, not the fact that it is a servo.

Code: [Select]
do {
Shit-can the do-while. It is almost never the right statement to use. Use a while statement. Make your code read the serial data when there is data to read, rather than doing nothing 99.9% of the time.

And, finally, how ARE the servos powered?

Robin2

Your title says "Servo testing using bluetooth. Servo won't turn all the way".  Does that mean that it does go all the way when you are NOT using bluetooth?

If you don't know the answer then IMHO the first thing to do is to get your servo moving the way you want on its own. When that is working properly write a separate short program to receive Bluetooth data reliably. When both work separately then you can develop a program that combines the feaures.

For receiving data have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Daniel5815

Your title says "Servo testing using bluetooth. Servo won't turn all the way".  Does that mean that it does go all the way when you are NOT using bluetooth?

If you don't know the answer then IMHO the first thing to do is to get your servo moving the way you want on its own. When that is working properly write a separate short program to receive Bluetooth data reliably. When both work separately then you can develop a program that combines the feaures.

For receiving data have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R
Thank for the guided link! I'll check it out and update you!

Daniel5815

Shit-can the do-while. It is almost never the right statement to use. Use a while statement. Make your code read the serial data when there is data to read, rather than doing nothing 99.9% of the time.

And, finally, how ARE the servos powered?
The servos are powered with an external power source that supplies up to 2A @ 5V.

I'll go ahead and clean up the code!

Could you possibly elaborate on the do-while and while statement? How do they differ in this case and why do you say that do-while is doing nothing 99.9% of the time? What would you recommend is the best way to make this current code efficient? This is my first time trying Bluetooth and serial ports, so I'm a little short handed. Thank you for your remarks though!!!

Daniel5815

Code: [Select]
void operateServo(boolean state) {
  Serial.println(state);
  if (state == 'a') {


boolean is true/false.  boolean is NOT 'a'.  'a' is a char.  


Code: [Select]
boolean isValidInput; do {

Of all the messed up confusing formatting screw ups you could possibly imagine, I think starting a do while on a line with other code takes the cake.  

Please use Control-T to format code before you post it.  We don't format code the way we do just to make it pretty.  There's a real good purpose for doing it the way we do.  

And please clean up the comments while you are editing code.  The only thing worse than no comments is wrong comments.  

Code: [Select]
byte c; // get the next character from the bluetooth serial port
No it doesn't.

Code: [Select]
c = mySerial.read(); // Execute the option based on the character recieved
I think this comment was supposed to go with that last one instead.

IF you're not going to have the comment where they make any sense, then just delete them.  Don't add to your own confusion by getting them all messed up.  Remember, you're asking someone else to kindly read your code and check it for you, try not to make that unnecessarily hard on them.  
Thank you for the remarks! This code is not entirely mine. Most of it is actually from an instructable online because I've never dealt with Bluetooth and serial ports before. Could you possibly guide me to the best Bluetooth communications tutorial? I'm kinda lost. Also, the Boolean part was a dumb mistake that I made because I was thinking of multiple things at once. Sorry! But thank you!!!

Delta_G

Thank you for the remarks! This code is not entirely mine. Most of it is actually from an instructable online because I've never dealt with Bluetooth and serial ports before.
If you want to learn the concepts you need then see the famous Serial Input Basics thread.  It talks about sending and receiving mostly over serial I think, but the same exact stuff applies to anything that uses the Stream and Print classes. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up