Let's Make Robots!

Arduino multi-servo remote control through serial with Processing

Remote control a set of servos attached to an Arduino board from Processing through the serial port
AttachmentSize
RemoteServo.pde1.84 KB
RemoteServoTest.pde1.93 KB

This is based on http://www.arduino.cc/playground/Learning/SingleServoExample but extended to multiple servos and with a matching Processing class.

To try this out:

  1. Edit the number of servos and the pin numbers in RemoteServo.pde (read the comments) and upload to Arduino
  2. Edit the number of servos and the Arduino serial port index in RemoteServoTest.pde.
  3. Check the range of motion for the servos in the test (START_POSITION and END_POSITION) is OK for your servos
  4. Run RemoteServoTest in Processing

The baud rate is set to 115200 in both programs, if this doesn't work for you try lowering it in both programs.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

This is a tiny "interpreter" that lets you send decimal numbers through the serial port. Every time it gets a new digit it multiplies the previous value by 10 and adds the value of the new digit.

The (ch-'0') part subtracts the ASCII value for zero from the digit so that '0' will be converted to integer 0 and '9' will be integer 9, and so on.

For example, when it receives the character sequence '1' '2' it will end up with (1*10 + 2 = 12) in the pin number. 

Can someone explain to me the line:

  pinNumber = pinNumber * 10 + (ch -'0');

in the RemoteServo.pde code?

that is kind of a weird construction - I dont get the "(ch -'0') and I dont get why pinNumber gets multiplied by 10.

Any help out there?

    owen

See above reply. :)

I had been search better solutions for servo control for Arduino. I have been modified the SAMPLE from arduino but my quadbot seems like got STROKE attack. It just not working right. I am gonna try this tonight and thanks again!

You're welcome. One thing I found when using lots of servos is that they drain a lot of current when they're all working at once, and if your power supply and/or battery isn't up to it and the supply voltage on the servos sags too much the controllers in the servos will go crazy or stop working, then the voltage goes up with no load and they start working again, etc. So it will look like it's your software that's messing up, but really it's the hardware.

System works fine, meaning my Depron Project 5 Channel Radion Transmitter can progress further.

Plus Point is that even @9600 the system is pretty fast......... "Oh Yes"

Cool, glad it worked for you. If you need to speed it up you could send the servo index and position as bytes rather than ASCII and skip the "p/m/g" separators, they are only that way as per the original SingleServoExample to make it easy to test interactively from the serial terminal.

I have tested the code and it indeed it drives multiple servos. Mega Wow and Mega Thanks for posting.

I will write some code for the XBees in the next few days and see if its fast enough for my application. (fingers crossed).

It took me a while to workout how to drive the servos with the serial command - you may want to change the comment line

Qoute:-             // e.g. p1m135g moves servo 1 to position 135     .......adding servo 0 would be p0m135g

NB. Servo 0 is the first physical servo (it confused me for a bit, i assumed Servo 1 would be the first physical servo)