Go Down

### Topic: Information about Servo (Read 4905 times)previous topic - next topic

#### JohnDoe

##### Sep 01, 2009, 10:24 pm
Good evening
I am an Italian student, so excuse me for my bad English
I need help about a program with a Servo
I don't know if this is the right section for this topic, please tell me if it's a problem
I found this instruction:
pulseWidth = (myAngle * 11) + 500;
it convert the angle in millisecond... but how can this instruction do it?
then there are the instructions to set the Servo high and then low... but I don't undestend the convertion from angle to millisecon
Is there another way to move the Servo? Or can you explain me the meaning of that instruction?

Thank you very much and sorry if I was unclear

#### ahdavidson

#1
##### Sep 01, 2009, 11:59 pm
caro JohnDoe,

La tua inglese è perfetta!

But more information about your specific question is needed. Can you post the code you are inquiring about and describe the application in more detail? Some context is needed.
.andy

#### mem

#2
##### Sep 02, 2009, 12:12 am
You don't need to convert from angle to microseconds when using the Arduino servo library. The read and write functions use angles in degrees.

If you prefer to work in microseconds you can use the undocumented functions:   writeMicroseconds and readMicroseconds

#### JohnDoe

#3
##### Sep 02, 2009, 12:27 pm
I find another program that uses the instruction Write of a Servo object, so I solved the problem

But now I have another problem: I would like to move the Servo with the keybord's arrows: can I do it? can you help me?

Thank you very much

#### mem

#4
##### Sep 02, 2009, 12:41 pm

Quote
I find another program that uses the instruction Write of a Servo object

What  servo program are you using?

Do you want to control the servo from your computers keyboard?

What programming experience do you have?

#### JohnDoe

#5
##### Sep 02, 2009, 01:12 pm
Hi mem, thank you very much

I use the slides from the site http://todbot.com/blog/spookyarduino/ to study, and I find the instruction to move the servo with the conversion from angle to microseconds
Today i found the Sweep program to move the Servo without conversion

But these are only examples to learn somethign about Arduino.
Now I can move the Servo with the number of the keyboard, but I need to move it with the keyboard's arrows

I have some programming experience from university, with C and Java

Please tell me if some sentence is wrong

#### mem

#6
##### Sep 02, 2009, 02:21 pm

The sketch below will move a servo connected on pin 9 in response to serial commands.

Code: [Select]
`#include <Servo.h> Servo myServo; const int servoPin = 9; // the pin the servo is connected toint val = 0;    // a value accumulated from data on the serial portint angle = 90; // the current angle of the servovoid setup() {  Serial.begin(9600);  myServo.attach(servoPin);    myServo.write(angle);  // center the servo} void loop(){  if ( Serial.available())  {    char ch = Serial.read();    if(ch >= '0' && ch <= '9')              // is ch a number?        val = val * 10 + ch - '0';           // yes, accumulate the value    else if(ch == '-') // is this the minus sign?    {      angle = angle - val;      if(angle < 0)         angle = 0;              myServo.write(angle);         // write the new angle      val = 0;    }    else if(ch == '+') // is this the plus sign?    {      angle = angle + val;      if(angle > 180)         angle = 180;              myServo.write(angle);         // write the new angle      val = 0;    }  }} `
Sending a digit from 1 to 9 followed by a + (the plus character) will step the servo forward by one to nine degrees. 1 to 9 followed by a - (the minus character) will step the servo in the other direction.  This assumes you are using the Arduino servo library.

You can test this using the Arduino serial monitor to send the characters.

#### JohnDoe

#7
##### Sep 03, 2009, 04:56 pm
Hi mem, I tried you program this mornign, it works very well with all numbers from 0 to 180

But if you digit a number of two digit, for example 20, the program reads it in two cicle clock, until you write the sign ("+" or "-")
Is there a way for read a number in a single clock?
I know that's not a problem of a servo
Do you understend the term "cicle clock"?
I'm sorry for my English

thank you

#### mem

#8
##### Sep 03, 2009, 05:01 pmLast Edit: Sep 03, 2009, 05:01 pm by mem Reason: 1
I don't understand what you are asking.
Perhaps if you say more about your application and what you want your program to do it will be easier to see what you mean.

#### AWOL

#9
##### Sep 03, 2009, 05:32 pm
Quote
Is there a way for read a number in a single clock?

I think you're asking if it is possible to read both characters without having to wait for loop to repeat, and to read both at the same time (or at least in the same pass through "loop)?

Sure, just wait for "Serial.available" to return two or more.
"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.
I speak for myself, not Arduino.

#### mem

#10
##### Sep 03, 2009, 05:39 pmLast Edit: Sep 03, 2009, 05:39 pm by mem Reason: 1
even if you know that you will always send two characters plus the terminating sign there is no advantage to changing the code. The code as posted won't do anything anyway until it gets the sign character.

I was guessing that the OP wanted to send a single character to control the servos and although that is possible, the code to do it would be less clear than the posted code.

#### bhagman

#11
##### Sep 03, 2009, 09:34 pm
I'm not sure, but maybe this is what the OP was trying to achieve.

- Use keyboard arrow keys to control the angle of rotation on the servo.

I don't know what the key values are from the Mac, but they can be easily determined.  Also, you'll probably have to use a different serial terminal (I use Tera Term on a PC) to send control characters to the Arduino.

On a PC, arrow keys (and other special function keys) send multiple bytes per press.  You will need to use a state machine to catch the keys.

Here's some code:

Code: [Select]
`#include <Servo.h>Servo myServo;const int servoPin = 9; // the pin the servo is connected toint angle = 90; // the current angle of the servochar keystate = 0;// 5 degree increments#define ANGLE_STEP 5void setup(){  Serial.begin(9600);  myServo.attach(servoPin);  myServo.write(angle);  // center the servo}void loop(){  char ch;  // For a PC:  // Left Arrow key = 0x1B, 0x5B, 0x44  // Right Arrow key = 0x1B, 0x5B, 0x43  // For a Mac:  // ?? - help me find these values?  if(Serial.available())  {    ch = Serial.read();    if(ch == 0x1B)    {      keystate = 0x1B;    }    else if(ch == 0x5B)    {      if(keystate == 0x1B)        keystate = 0x5B;      else        keystate = 0;    }    else if(keystate == 0x5B)    {      if(ch == 0x44) // Left arrow key      {        Serial.println("LEFT");        if(angle > ANGLE_STEP)        {          angle = angle - ANGLE_STEP;  // step left        }        else        {          angle = 0;        }        myServo.write(angle);      }      else if(ch == 0x43) // Right arrow key      {        Serial.println("RIGHT");        if(angle < 180 - ANGLE_STEP)        {          angle = angle + ANGLE_STEP; // step right        }        else        {          angle = 180;        }        myServo.write(angle);      }      else      {        keystate = 0;      }    }    // to find values for the arrow keys:    Serial.println(ch, HEX);  }}`

b
http://wiring.org.co/ - Wiring - Where Arduino came from. (Wiring begat Arduino)

http://loftypremises.com/ - Everything you wanted to know about me.... and less

http://roguerobotics.com/ - #1 Canadian Arduino distributor

#### JohnDoe

#12
##### Sep 03, 2009, 10:41 pm
x mem
this is some instructions from your post:
"  if ( Serial.available())
{

if(ch >= '0' && ch <= '9')              // is ch a number?
val = val * 10 + ch - '0';           // yes, accumulate the value
else if(ch == '-') // is this the minus sign?
{
angle = angle - val;
if(angle < 0)
angle = 0;
myServo.write(angle);         // write the new angle
val = 0;
}
else if(ch == '+') // is this the plus sign?
{
angle = angle + val;
if(angle > 180)
angle = 180;
myServo.write(angle);         // write the new angle
val = 0;
}
}"
if I write "90+" the program enter in the loop three times, the first to read the character "9", the second to read the character "0" and convert "9" like "90", and the last lo read the character "+" that is, in this example, like a character of the end of the line (such as for example "\n" in C)
I would like to know if there is a method that can read the number (for example "90") in a single operation

x AWOL:
I think I understand your post, but I'm not sure. I'm Italian and my English isn't very good
I write it in the part of message for mem: I would like to read a number > 9 without a loop for each digit

x bhagman:
maybe your program is really what I need: move the servo with arrow keys. But now I'm very tired, so I will try it tomorrow

ps what does "OP" mean?  :-?

#### ahdavidson

#13
##### Sep 03, 2009, 11:50 pm
Quote
ps what does "OP" mean?

OP = original poster (del messagio)

Come si dice questa frase in Italiano? [trasmettitore originale?]
.andy

#### JohnDoe

#14
##### Sep 04, 2009, 01:02 pm
Thank you Andy
In Italian they say "Autore originale del messaggio" or something like

for bhagman:
I tired your program, but it doens't work on my PC  :-?

Go Up