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

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.

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

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

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?

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

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

``````#include <Servo.h>

Servo myServo;

const int servoPin = 9; // the pin the servo is connected to

int val = 0;    // a value accumulated from data on the serial port
int angle = 90; // the current angle of the servo

void setup()
{
Serial.begin(9600);
myServo.attach(servoPin);
myServo.write(angle);  // center the servo
}

void loop()
{
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;
}
}
}
``````

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.

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 :P

thank you

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.

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.

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.

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:

``````#include <Servo.h>

Servo myServo;

const int servoPin = 9; // the pin the servo is connected to

int angle = 90; // the current angle of the servo
char keystate = 0;

// 5 degree increments
#define ANGLE_STEP 5

void 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())
{

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

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 ;D

ps what does “OP” mean? :-?

ps what does "OP" mean?

OP = original poster (del messagio)

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

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 :-?

Here, try this instead - uses “A” and “D” keys instead of arrow keys:

``````#include <Servo.h>

Servo myServo;

const int servoPin = 9; // the pin the servo is connected to

int angle = 90; // the current angle of the servo

// 5 degree increments
#define ANGLE_STEP 5

void setup()
{
Serial.begin(9600);
myServo.attach(servoPin);
myServo.write(angle);  // center the servo
}

void loop()
{
char ch;

if(Serial.available())
{

// to find values for the arrow keys:
// Serial.println(ch, HEX);

if(ch == 'a' || ch == 'A')
{
Serial.println("LEFT");
if(angle > ANGLE_STEP)
{
angle = angle - ANGLE_STEP;  // step left
}
else
{
angle = 0;
}
myServo.write(angle);
}
else if(ch == 'd' || ch == 'D')
{
Serial.println("RIGHT");
if(angle < 180 - ANGLE_STEP)
{
angle = angle + ANGLE_STEP; // step right
}
else
{
angle = 180;
}
myServo.write(angle);
}
}
}
``````

This will work in the Arduino IDE as well. You just have to click “Send” or press Enter/Return after each key.

b

bhagman you are very kind I will try this version of program But does the other version work on your pc? I need to move the servo with the arrow keys

PS: I have another problem. The program Arduino 0017 is very very very slow on my pc :( I tried to use also the version 0012, 0013 and 0014 but they don't start :-? What can I do? Change some settings? Please help me :-[

I explain my last question: does the software need minimal system requirement? I have the version 0017

The arrow key version works on my PC - but you MUST use a different serial terminal, like Tera Term:

I’m going to guess that you have a bluetooth dongle/card on your computer. FAQ here:

http://www.arduino.cc/en/Guide/Troubleshooting#toc12

b

Hi bhagman, I solved my problem with the software with your help. I disabled ALL the COM port on my PC :P Now the software needs few seconds to upload the program on the board

I also use Tera Term and now the program works perfectly