Go Down

Topic: Servo's controlled by serial (Read 632 times) previous topic - next topic

mrjd

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?)
Code: [Select]


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' - http://oreilly.com/catalog/9780596155520.

Cheers,

James

AWOL

#1
Jun 01, 2011, 10:24 pm Last Edit: Jun 01, 2011, 10:27 pm by AWOL Reason: 1
Code: [Select]
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?

Quote
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.

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

Shouldn't be a problem.

How are you powering the servo?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

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

Or, wait for all 5 of them to arrive, first:
Code: [Select]
if(Serial.available() >= 5)

mrjd

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

zoomkat

Quote
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.
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

mrjd

Thanks zoomcat, I'll check that out!

Go Up