Serial Servo Control

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
spyguy99
 
Posts: 7
Joined: Sun May 10, 2009 12:05 am

Serial Servo Control

Post by spyguy99 »

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

#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

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.

User avatar
Franklin97355
 
Posts: 23910
Joined: Mon Apr 21, 2008 2:33 pm

Re: Serial Servo Control

Post by Franklin97355 »

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.

spyguy99
 
Posts: 7
Joined: Sun May 10, 2009 12:05 am

Re: Serial Servo Control

Post by spyguy99 »

How would I go about doing that?

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

Re: Serial Servo Control

Post by adafruit »

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

spyguy99
 
Posts: 7
Joined: Sun May 10, 2009 12:05 am

Re: Serial Servo Control

Post by spyguy99 »

ladyada wrote:add the numbers together until you get a '\n' newline character
Can you please elaborate a bit?

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

Re: Serial Servo Control

Post by adafruit »

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

spyguy99
 
Posts: 7
Joined: Sun May 10, 2009 12:05 am

Re: Serial Servo Control

Post by spyguy99 »

From what I can tell it only reads one at a time.

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

Re: Serial Servo Control

Post by adafruit »

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

spyguy99
 
Posts: 7
Joined: Sun May 10, 2009 12:05 am

Re: Serial Servo Control

Post by spyguy99 »

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

#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);
  }
 }

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

Re: Serial Servo Control

Post by adafruit »

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

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”