0

Serial Servo Control
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Serial Servo Control

by spyguy99 on Sun May 10, 2009 12:32 am

I'm trying to make sketch that will let me control a servo I have (Futaba S148) via serial. The code I have thus far is as follows:
Code: Select all | TOGGLE FULL SIZE
#include <ServoTimer1.h>

ServoTimer1 servo1;

int var;

void setup() {
  Serial.begin(9600);           
  servo1.attach(10);

}
void loop() {
  if (Serial.available()) {
    var = Serial.read();
  Serial.println(var);
  Serial.println(var, BYTE);
  servo1.write(var);
 
  }
}


I put in the Serial.println() parts to try and debug why the sketch wasn't working and when I type in an ange such as 155 the servo does not move and I receive this
Code: Select all | TOGGLE FULL SIZE
49

1

53

5

53

5
from the serial monitor. So I am assuming that it is taking each part of what I put in (155) and breaking it up into separate parts. Is there a way to fix this? Also, the basic ServoTimer1 code works fine and the servo sweeps back and forth. Let me know if you need any additional information.
spyguy99
 
Posts: 7
Joined: Sun May 10, 2009 12:05 am

Re: Serial Servo Control

by franklin97355 on Sun May 10, 2009 8:21 pm

Serial.read is reading each byte (character) as it is sent as you can see from your output. What you need to do is get the whole number in a varable before sending it. Remember a byte can only go to 255.

franklin97355
 
Posts: 21488
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Serial Servo Control

by spyguy99 on Sun May 10, 2009 9:12 pm

How would I go about doing that?
spyguy99
 
Posts: 7
Joined: Sun May 10, 2009 12:05 am

Re: Serial Servo Control

by adafruit on Mon May 11, 2009 11:48 am

add the numbers together until you get a '\n' newline character

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Serial Servo Control

by spyguy99 on Mon May 11, 2009 11:42 pm

ladyada wrote:add the numbers together until you get a '\n' newline character


Can you please elaborate a bit?
spyguy99
 
Posts: 7
Joined: Sun May 10, 2009 12:05 am

Re: Serial Servo Control

by adafruit on Tue May 12, 2009 12:30 am

how many digits can serial.read() read at a time?

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Serial Servo Control

by spyguy99 on Tue May 12, 2009 7:55 pm

From what I can tell it only reads one at a time.
spyguy99
 
Posts: 7
Joined: Sun May 10, 2009 12:05 am

Re: Serial Servo Control

by adafruit on Tue May 12, 2009 10:07 pm

correct! so if you want to read the number "123" you will really read "1", "2" and then "3"
you can convert multiple digits into one number by always multiplying the previous number by 10 and then adding the new digit

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: Serial Servo Control

by spyguy99 on Wed May 13, 2009 12:17 am

Ahh, ok I got it. Thanks for all the help!

I think I have some of the code down, I know there are changes I have to make, but here is is anyway.

Code: Select all | TOGGLE FULL SIZE
#include <ServoTimer1.h>

ServoTimer1 Servo;

int ServoArray[3];
int var;
int angle;


void setup() {
  Serial.begin(9600);           
  Servo.attach(9);
}
void loop() {
  if (Serial.available()) {
  var = Serial.read();
   
  do {int x = 0;
      ++x;       
 
   ServoArray[x] = var;
   if (ServoArray[2] == 43)
   { angle = (ServoArray[0] *10) + (ServoArray[1]);}
     else
   {angle = (ServoArray[0] *100) + (ServoArray[1] *10) + (ServoArray[2]);}
     
   } while (var != 43);
   
   Serial.print(angle);
   Serial.print(angle, BYTE);
   Serial.write(angle);
   Servo.write(angle);
   delay(1000);
  }
 }
spyguy99
 
Posts: 7
Joined: Sun May 10, 2009 12:05 am

Re: Serial Servo Control

by adafruit on Wed May 13, 2009 12:04 pm

yes you'll have to adjust because the ascii code for '0' is not 0
google for ascii chart

adafruit
 
Posts: 12151
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Please be positive and constructive with your questions and comments.