Servo's controlled by serial

Hi guys,

I'm working on a little project. Basically I've got two small servos which I'm trying to control via serial.

I'm using HEX to send down my values to the Arduino.

I've got two little problems / queries though...

  1. It works, however, it can be sporatic. I.e. the servos are moving but all of a sudden one of them wildly swings the wrong way.

  2. This happens on other project but when I open the serial console in the Arduino IDE (or access the serial in other ways) it seems to effect the Arduino. Almost as if it's resetting it?

It doesn't seem to be the servos themselves as I've created a test sketch which cycles through the different directions. This works fine.

This is a snippet of the code which handles the Serial. Serial is set at 9600 baud (could this be causing it?)

if (Serial.available() > 0) {
      
      inByte = Serial.read();
      
      if (inByte == '#') {
      
      while (pointer < 4) {
         buffer[pointer] = Serial.read( );  // add the character to the buffer
         pointer++;  // increment the pointer
      }
 
      x = hex2dec(buffer[1]) + hex2dec(buffer[0]) * 16;
      y = hex2dec(buffer[3]) + hex2dec(buffer[2]) * 16;
      
      pointer = 0; // reset pointer;
      
      }
    
  }

If you're wondering what the hex2dec function does it's building on similar code from 'Getting Started with Arduino' - Getting Started with Arduino [Book].

Cheers,

James

if (Serial.available() > 0) {
      
      inByte = Serial.read();
      
      if (inByte == '#') {
      
      while (pointer < 4) {
         buffer[pointer] = Serial.read( );  // add the character to the buffer

Check to see if at least one character is available, then read all five of them.
Not good.
Why not check to see if you've got a character before reading it?

it seems to effect the Arduino. Almost as if it's resetting it?

Yes, it is resetting - that is what it is designed to do.

Serial is set at 9600 baud (could this be causing it?)

Shouldn't be a problem.

How are you powering the servo?

Why not check to see if you've got a character before reading it?

Or, wait for all 5 of them to arrive, first:

if(Serial.available() >= 5)

That's a good point...

I'll change that, thanks for the replies guys!

The servos are powered from the board... I realise I'm pushing it power wise. I've checked, it looks like they peak around 100mA's each when in use. Would this be about right?

Cheers,

James

The servos are powered from the board... I realise I'm pushing it power wise. I've checked, it looks like they peak around 100mA's each when in use. Would this be about right?

When under a load, a standard servo can draw over 1a in current. If you are powering the arduino from the computer, you might trip the fuse in the USB port as it is usually only rated for ~500ma.

Thanks zoomcat, I'll check that out!