Go Down

Topic: Controlling servos using serial commands (Read 984 times) previous topic - next topic

kemartin

I'm looking to control the position of some servo's using serial communication between my laptop and an arduino Mega 2560.

My code is shown below:
Code: [Select]


#include <Servo.h>
Servo serv; // define latching mechanism servo
Servo act; // define latching mechanism linear act

char incomingByte = 0;
int numBytes;

// the setup routine runs once when you press reset:
void setup()
{
  Serial.begin(9600);
  serv.attach(12);
  act.attach(13);
}
void loop() {
  if(Serial.available() > 0 ) { // wait for something on the serial port.
// get incoming byte:
// P - Lift latch
// L - Lower Latch
// O - Open Latch
// K - Close Latch

incomingByte = Serial.read(); // read the incoming byte
    Serial.println((char)incomingByte);
    if(incomingByte == 'p'){
      serv.write(50);
    }
    if(incomingByte == 'l'){
      serv.write(150);
    }
    if(incomingByte == 'o'){
      act.write(40);
    }
    if(incomingByte == 'k'){
      act.write(180);
    }   
}
}


I'm using the Arduino Serial Monitor to send the characters for commands and the Arduino is receiving the commands because the RX light is flashing, but the servo won't move to the described positions.

Any ideas/suggestions with my code?

wildbill

Do you see the characters being echoed back to the terminal?

How are the servos powered? Hopefully not from the Arduino - it's marginal for one, let alone two.

AlxDroidDev

Is anything else using the same RX/TX pins (you should keep those reserved for serial comms)  ?
Learn to live: Live to learn.
Showing off my work: http://arduino.cc/forum/index.php/topic,126197.0.html

kemartin

Thanks for the quick response.

At first the characters we're not showing up in the serial monitor, however, after unplugging everything and trying my set-up and code again, everything worked.

And the servo's were powered by the Arduino, they are small hobby servo's that run on 4.8v or 6v. Eventually once the project is complete I will be running off external power.


zoomkat

Servo test code.

Code: [Select]

//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added

String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

JimboZA

#5
Apr 02, 2013, 06:29 am Last Edit: Apr 02, 2013, 06:31 am by JimboZA Reason: 1
Quote
And the servo's were powered by the Arduino, they are small hobby servo's that run on 4.8v or 6v. Eventually once the project is complete I will be running off external power.


Were?... not any more?- do you have external power now, do you mean?

It's not the voltage that's an issue, it's the current. Even if you have no mechanical load on them, those servos could be drawing 100mA or so each, up to say 500mA if you load them.... it's risking damaging your Arduino, for sure.

As a rule of thumb, any servo "funnies" are best treated by making sure power related problems are off the table by having an external supply, even if it's just a 4xAA pack for testing. Sometimes the problem goes away when you provide external power; if it doesn't, well at least you've eliminated one possible cause.

Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Go Up