Loading...
Pages: [1]   Go Down
Author Topic: Servo's controlled by serial  (Read 322 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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
Logged

Global Moderator
UK
Online Online
Brattain Member
*****
Karma: 143
Posts: 19368
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
« Last Edit: June 01, 2011, 03:27:34 pm by AWOL » Logged

Pete, it's a fool looks for logic in the chambers of the human heart.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 334
Posts: 36443
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if(Serial.available() >= 5)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Tesla Member
***
Karma: 58
Posts: 6774
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Why I like my 2005 rio yellow Honda S2000 with the top down, and more!
GOOGLE ADVANCED FORUM SEARCH BELOW!  
Go to:  http://www.google.com/advanced_search?hl=en
put in key search words,
use site or domain:  http://arduino.cc/forum
or in a google search box put key words site:http://arduino.cc/forum

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks zoomcat, I'll check that out!
Logged

Pages: [1]   Go Up
Print
 
Jump to: