Go Down

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


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.

Code: [Select]
#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()
 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.


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


Sep 03, 2009, 05:01 pm Last 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.


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.

Go Up